Module: Bitfields

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

Defined Under Namespace

Modules: ClassMethods, InstanceMethods Classes: DuplicateBitNameError, InvalidBitError, PositionalBitsError

Constant Summary collapse

TRUE_VALUES =

taken from ActiveRecord::ConnectionAdapters::Column

[true, 1, '1', 't', 'T', 'true', 'TRUE'].freeze
POSITIONAL_PLACEHOLDERS =

Symbols/values that reserve a positional bit without naming it, so removing a bit leaves a gap instead of silently shifting every later bit down.

[nil, :_skip].freeze
POSITIONAL_MODES =
%i[allow warn forbid].freeze
VERSION =
'1.0.2'
Version =
VERSION

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.positional_bitsObject



24
25
26
# File 'lib/bitfields.rb', line 24

def positional_bits
  @positional_bits ||= :warn
end

Class Method Details

.extract_bits(options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bitfields.rb', line 49

def extract_bits(options)
  options.keys.grep(Numeric).each_with_object({}) do |bit, bitfields|
    unless bit.is_a?(Integer) && bit.positive? && bit.nobits?(bit - 1)
      raise InvalidBitError, "#{bit} is not a power of 2 !!"
    end

    bit_name = options.delete(bit).to_sym
    raise DuplicateBitNameError if bitfields.key?(bit_name)

    bitfields[bit_name] = bit
  end
end

.included(base) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bitfields.rb', line 28

def 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(subclass)
      super
      subclass.bitfield_args = bitfield_args.dup
      subclass.bitfield_args.each do |column, options|
        subclass.send :store_bitfield_values, column, options.dup
      end
    end
  end

  base.extend Bitfields::ClassMethods
end