Module: BitmaskAttribute::ClassMethods

Defined in:
lib/bitmask_attribute.rb

Instance Method Summary collapse

Instance Method Details

#bitmask_attribute(attribute_name, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bitmask_attribute.rb', line 9

def bitmask_attribute ( attribute_name, options = {} )
  default_options = {
    :bitmask_object => attribute_name.to_s + '_bitmask',
    :bit_ids => []
  }
  options = default_options.merge(options)

  bitmask_obj = options[:bitmask_object]
  class_eval <<-ADD_METHOD
    def #{options[:bitmask_object]}
      @_#{options[:bitmask_object]} ||= Bitmask.new({
        :bit_ids => #{options[:bit_ids].inspect},
        :value => @#{attribute_name},
        :after_change => Proc.new { |bitmask|
          @#{attribute_name} = bitmask.value
        }
      })
    end
  ADD_METHOD

end