Module: Mongoid::StateBits::ClassMethods

Defined in:
lib/mongoid/state_bits.rb

Instance Method Summary collapse

Instance Method Details

#state_bits(bits) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mongoid/state_bits.rb', line 21

def state_bits(bits)
  bits.each_with_index do |bit, index|
    define_method "#{bit}=" do |bool|
      if bool
        self.state_bits |= 2**index
      else
        self.state_bits -= (self.state_bits & 2**index)
      end
    end
    define_method "#{bit}?" do
      self.state_bits & 2**index != 0
    end
    alias_method("#{bit}", "#{bit}?")
    # then define scopes
    scope bit, ->{ where("this.state_bits & #{2**index}")}
    scope "non#{bit}", ->{ where("this.state_bits & #{2**index} == 0")}
  end
end