class Record::Base
Attributes
values[R]
Public Class Methods
define_field( field )
click to toggle source
# File lib/records/record.rb, line 52 def self.define_field( field ) key = field.key ## note: always assumes a "cleaned-up" (symbol) name index = field.index define_method( key ) do instance_variable_get( "@values" )[index] end end
field( key, type )
click to toggle source
# File lib/records/record.rb, line 44 def self.field( key, type ) index = fields.size ## auto-calc num(ber) / position index - always gets added at the end field = Field.new( key, index, type ) fields << field define_field( field ) ## auto-add getter,setter,parse/typecast end
fields()
click to toggle source
# File lib/records/record.rb, line 27 def self.fields ## note: use class instance variable (@fields and NOT @@fields)!!!! (derived classes get its own copy!!!) @fields ||= [] end
index( key )
click to toggle source
# File lib/records/record.rb, line 35 def self.index( key ) ## indef of key (0,1,2,etc.) ## note: returns nil now for unknown keys ## use/raise IndexError or something - why? why not? @index_by_key ||= Hash[ keys.zip( (0...fields.size).to_a ) ].freeze @index_by_key[key] end
keys()
click to toggle source
# File lib/records/record.rb, line 31 def self.keys @keys ||= fields.map {|field| field.key }.freeze end
new( *args )
click to toggle source
note: “skip” overloaded new Record.new
(and use old_new version)
# File lib/records/record.rb, line 62 def self.new( *args ) old_new( *args ); end
new( *args )
click to toggle source
# File lib/records/record.rb, line 68 def initialize( *args ) ##### ## todo/fix: add allow keyword init too ### note: ### if init( 1, {} ) assumes last {} is a kwargs!!!!! ## and NOT a "plain" arg in args!!! ## puts "[#{self.class.name}] Record::Base.initialize:" ## pp args ## ## fix/todo: check that number of args are equal fields.size !!! ## check types too :-) @values = args @values.freeze self.freeze ## freeze self too - why? why not? self end
Public Instance Methods
<<( hash )
click to toggle source
“convenience” shortcut for update e.g.
<< { balance: 5 } equals .update( balance: 5 )
# File lib/records/record.rb, line 102 def <<( hash ) update( hash ); end
==(other)
click to toggle source
note: compare by value for now (and NOT object id)
# File lib/records/record.rb, line 107 def ==(other) if other.instance_of?( self.class ) values == other.values else false end end
Also aliased as: eql?
update( **kwargs )
click to toggle source
# File lib/records/record.rb, line 89 def update( **kwargs ) new_values = @values.dup ## note: use dup NOT clone (will "undo" frozen state?) kwargs.each do |key,value| index = self.class.index( key ) new_values[ index ] = value end self.class.new( *new_values ) end