Sha256: 5a9b41d9facf211cbadc11c5012af37a7f47a385e440907297aa31c01713e86e

Contents?: true

Size: 993 Bytes

Versions: 7

Compression:

Stored size: 993 Bytes

Contents

module Maintain
  class BitmaskValue < Value
    def set_value(value)
      @value = bitmask_for(value)
    end

    protected
    def bitmask_for(states)
      Array(states).map{|value| value_for(value) }.sort.inject(0) {|total, mask| total | mask }
    end

    def compare_value
      @value ||= 0
    end

    def compare_value_for(state)
      bitmask_for(state)
    end

    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 & #{compare.inspect} != 0
            end
          EOC
          @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
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
maintain-0.1.6 lib/maintain/bitmask_value.rb
maintain-0.1.5 lib/maintain/bitmask_value.rb
maintain-0.1.4 lib/maintain/bitmask_value.rb
maintain-0.1.3 lib/maintain/bitmask_value.rb
maintain-0.1.2 lib/maintain/bitmask_value.rb
maintain-0.1.1 lib/maintain/bitmask_value.rb
maintain-0.1.0 lib/maintain/bitmask_value.rb