Module: Bitty::ActiveRecordExtensions
- Defined in:
- lib/bitty.rb
Instance Method Summary collapse
Instance Method Details
#bitty(field, *args) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/bitty.rb', line 5 def bitty(field, *args) field = field.to_sym opts = args. proxy = Class.new(Bitty::BitProxy) proxy.bit_names = args proxy.column = opts[:column] || field # TODO: opts[:default] # TODO: opts[:named_scope] # TODO: check if field has already been defined in superclass write_inheritable_hash(:_bitty_fields, { field => proxy } ) class_eval <<-RUBY def #{field} @_bitty_#{field} ||= self.class.read_inheritable_attribute(:_bitty_fields)[:#{field}].new(self) end def #{field}=(value) #{field}.set!(value) end RUBY end |
#bitty_named_scope(name, bitty_field, *args) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/bitty.rb', line 31 def bitty_named_scope(name, bitty_field, *args) proxy_class = read_inheritable_attribute(:_bitty_fields)[bitty_field] if proxy_class named_scope name, proxy_class.named_scope(*args) else raise ArgumentError, "There's no bitfield '#{bitty_field}' defined!" end end |