Debian Codegraph @

lib/mrb/scripts/logger/flags.rb

raw symbols SCIP
1 module Groonga
2 class Logger
3 class Flags
4 VALUES = {
5 time: 1,
6 title: 2,
7 message: 4,
8 location: 8,
9 process_id: 16,
10 thread_id: 32,
11 }
12
13 attr_reader :value
14 def initialize(value)
15 @value = resolve_value(value)
16 end
17
18 def to_i
19 @value
20 end
21
22 def to_s
23 names = []
24 VALUES.each do |name, value|
25 names << name.to_s unless (value & @value) == 0
26 end
27 names.join("|")
28 end
29
30 def |(other)
31 other = self.class.new(other) unless other.is_a?(self.class)
32 self.class.new(@value | other.to_i)
33 end
34
35 def &(other)
36 other = self.class.new(other) unless other.is_a?(self.class)
37 self.class.new(@value & other.to_i)
38 end
39
40 private
41 def resolve_value(value)
42 case value
43 when String, Symbol
44 resolved_value = VALUES[value.to_sym]
45 if resolved_value.nil?
46 available_names = VALUES.keys.inspect
47 message = "unknown flag name: #{value.inspect}: #{available_names}"
48 raise ArgumentError, message
49 end
50 resolved_value
51 when ::Array
52 value.inject(0) do |resolved_value, v|
53 resolved_value | resolve_value(v)
54 end
55 else
56 value
57 end
58 end
59
60 NONE = new(0)
61 TIME = new(:time)
62 TITLE = new(:title)
63 MESSAGE = new(:message)
64 LOCATION = new(:location)
65 PROCESS_ID = new(:process_id)
66 PID = PROCESS_ID
67 THREAD_ID = new(:thread_id)
68 end
69 end
70 end