Module: Bitfields

Defined in:
lib/bitfields.rb,
lib/bitfields/version.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods Classes: DuplicateBitNameError

Constant Summary collapse

TRUE_VALUES =

taken from ActiveRecord::ConnectionAdapters::Column

[true, 1, '1', 't', 'T', 'true', 'TRUE']
Version =
VERSION = "0.11.0"

Class Method Summary collapse

Class Method Details

.extract_bits(options) ⇒ Object


30
31
32
33
34
35
36
37
38
39
# File 'lib/bitfields.rb', line 30

def self.extract_bits(options)
  bitfields = {}
  options.keys.select{|key| key.is_a?(Numeric) }.each do |bit|
    raise "#{bit} is not a power of 2 !!" unless bit & (bit - 1) == 0
    bit_name = options.delete(bit).to_sym
    raise DuplicateBitNameError if bitfields.include?(bit_name)
    bitfields[bit_name] = bit
  end
  bitfields
end

.included(base) ⇒ Object


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

def self.included(base)
  class << base
    attr_accessor :bitfields, :bitfield_options, :bitfield_args

    # all the args passed into .bitfield so children can initialize from parents
    def bitfield_args
      @bitfield_args ||= []
    end

    def inherited(klass)
      super
      klass.bitfield_args = bitfield_args.dup
      klass.bitfield_args.each do |column, options|
        klass.send :store_bitfield_values, column, options.dup
      end
    end
  end

  base.extend Bitfields::ClassMethods
end