Class: Maintain::BitmaskValue

Inherits:
Value
  • Object
show all
Includes:
Enumerable
Defined in:
lib/maintain/bitmask_value.rb

Instance Method Summary collapse

Methods inherited from Value

#<, #<=, #==, #===, #>, #>=, #as_json, #class, #inspect, #name, #nil?, #to_s, #value, #value_for

Constructor Details

#initialize(state, value = nil) ⇒ BitmaskValue

Returns a new instance of BitmaskValue.



16
17
18
19
# File 'lib/maintain/bitmask_value.rb', line 16

def initialize(state, value = nil)
  @state = state
  @value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (protected)



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/maintain/bitmask_value.rb', line 41

def method_missing(method, *args)
  if (method.to_s =~ /^(.+)(\?|!)$/) && @state.states.has_key?($1.to_sym)
    compare = value_for($1)
    if $2 == '?'
      self.class.class_eval <<-EOC
        def #{method}
          value_for(@value) & #{compare.inspect} != 0
        end
      EOC
      value_for(@value) & compare != 0
    else
      self.class.class_eval <<-EOC
        def #{method}
          @value = (@value || 0) | #{compare.inspect}
        end
      EOC
      @value = (@value || 0) | compare
    end
  else
    super
  end
end

Instance Method Details

#each(&block) ⇒ Object



12
13
14
# File 'lib/maintain/bitmask_value.rb', line 12

def each(&block)
  to_a.each {|state| yield state }
end

#set_value(value) ⇒ Object



21
22
23
# File 'lib/maintain/bitmask_value.rb', line 21

def set_value(value)
  @value = bitmask_for(value)
end

#to_aObject



6
7
8
9
10
# File 'lib/maintain/bitmask_value.rb', line 6

def to_a
  @state.states.select do |key, options|
    options[:value] & @value > 0
  end.map(&:first)
end