Module: ModelManage::Bitfield

Defined in:
lib/model_manage/bitfield.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/model_manage/bitfield.rb', line 3

def self.included(base)
  def base.bitfield field, bits
    all = (1 << bits.size) - 1
    const_set "ALL_BITS", all

    bits.each_with_index do |key, index|
      mask = 1 << index 
      const_set "#{key}_BIT".upcase, mask

      define_method "#{key}?" do
        self[:status][index] != 0
      end

      define_method "#{key}=" do |bit|
        case bit
        when nil, false, 0
          self[:status] &= all - mask
        else
          self[:status] |= mask
        end
      end
    end
  end
end