Class: Record::Base

Inherits:
Record show all
Defined in:
lib/records/record.rb

Constant Summary

Constants inherited from Record

Types

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Record

build_class

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



66
67
68
# File 'lib/records/record.rb', line 66

def values
  @values
end

Class Method Details

.define_field(field) ⇒ Object



52
53
54
55
56
57
58
59
# 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) ⇒ Object



44
45
46
47
48
49
50
# 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

.fieldsObject

note: use class instance variable (@fields and NOT @@fields)!!!! (derived classes get its own copy!!!)



27
28
29
# 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) ⇒ Object

indef of key (0,1,2,etc.)



35
36
37
38
39
40
# 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

.keysObject



31
32
33
# File 'lib/records/record.rb', line 31

def self.keys
  @keys ||= fields.map {|field| field.key }.freeze
end

.new(*args) ⇒ Object

note: “skip” overloaded new Record.new (and use old_new version)



62
# File 'lib/records/record.rb', line 62

def self.new( *args ) old_new( *args ); end

Instance Method Details

#<<(hash) ⇒ Object

“convenience” shortcut for update e.g.

<< { balance: 5 }
     equals
.update( balance: 5 )


102
# File 'lib/records/record.rb', line 102

def <<( hash )  update( hash ); end

#==(other) ⇒ Object Also known as: eql?

note: compare by value for now (and NOT object id)



107
108
109
110
111
112
113
# File 'lib/records/record.rb', line 107

def ==(other)
  if other.instance_of?( self.class )
    values == other.values
  else
    false
  end
end

#update(**kwargs) ⇒ Object



89
90
91
92
93
94
95
96
# 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