Sha256: d88d69d30ffff8340b16d4badab7e478786960600961825703f7fdf65ae60ce9

Contents?: true

Size: 1.12 KB

Versions: 11

Compression:

Stored size: 1.12 KB

Contents

# encoding: UTF-8
module Maintain
  class BitmaskValue < Value
    def initialize(state, value = nil)
      @state = state
      @value = value
    end

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

    protected
    def bitmask_for(states)
      Array(states).compact.map{|value| value_for(value) }.compact.sort.inject(0) {|total, mask| total | mask.to_i }
    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_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
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
maintain-0.2.20 lib/maintain/bitmask_value.rb
maintain-0.2.19 lib/maintain/bitmask_value.rb
maintain-0.2.18 lib/maintain/bitmask_value.rb
maintain-0.2.17 lib/maintain/bitmask_value.rb
maintain-0.2.16 lib/maintain/bitmask_value.rb
maintain-0.2.15 lib/maintain/bitmask_value.rb
maintain-0.2.14 lib/maintain/bitmask_value.rb
maintain-0.2.13 lib/maintain/bitmask_value.rb
maintain-0.2.12 lib/maintain/bitmask_value.rb
maintain-0.2.11 lib/maintain/bitmask_value.rb
maintain-0.2.10 lib/maintain/bitmask_value.rb