Module: Bitfields::ClassMethods

Defined in:
lib/bitfields.rb

Instance Method Summary collapse

Instance Method Details

#bitfield(column, *args) ⇒ Object


42
43
44
45
46
47
48
49
# File 'lib/bitfields.rb', line 42

def bitfield(column, *args)
  column = column.to_sym
  options = extract_bitfield_options args
  bitfield_args << [column, options.dup]

  store_bitfield_values column, options
  add_bitfield_methods column, options
end

#bitfield_column(bit_name) ⇒ Object


51
52
53
54
55
# File 'lib/bitfields.rb', line 51

def bitfield_column(bit_name)
  found = bitfields.detect{|_, bits| bits.keys.include?(bit_name.to_sym) }
  raise "Unknown bitfield #{bit_name}" unless found
  found.first
end

#bitfield_sql(bit_values, options = {}) ⇒ Object


57
58
59
60
# File 'lib/bitfields.rb', line 57

def bitfield_sql(bit_values, options={})
  bits = group_bits_by_column(bit_values).sort_by{|c,_| c.to_s }
  bits.map{|column, bit_values| bitfield_sql_by_column(column, bit_values, options) } * ' AND '
end

#set_bitfield_sql(bit_values) ⇒ Object


62
63
64
65
# File 'lib/bitfields.rb', line 62

def set_bitfield_sql(bit_values)
  bits = group_bits_by_column(bit_values).sort_by{|c,_| c.to_s }
  bits.map{|column, bit_values| set_bitfield_sql_by_column(column, bit_values) } * ', '
end