Class: Funkr::Types::SimpleRecord
- Defined in:
- lib/funkr/types/simple_record.rb
Overview
Simple records are a simple way to create records with named AND positional fields. Records can be updated on a per-field basis and pattern-matched as arrays. SimpleRecord has the following advantages above plain Hash :
- fields are strict, you can't update an unexisting field by
mistyping it or by combining it with a different structure
- you have easy access to fields : named AND positional
usage :
r = SimpleRecord.new( name: "Paul", age: 27 )
r.name # => "Paul"
r.age # => 27
name, age = r # => [ "Paul", 27 ]
r.with(age: 29) # => [ "Paul", 29 ]
other usage :
class Person < SimpleRecord; fields :name, :age; end
Person.new( name: Paul ) => Error, missing :age
Class Attribute Summary collapse
-
.fields_list ⇒ Object
Returns the value of attribute fields_list.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(key_vals) ⇒ SimpleRecord
constructor
Create a new SimpleRecord.
- #to_hash ⇒ Object
- #to_s ⇒ Object
-
#update!(new_key_vals) ⇒ Object
Update a simple record destructively !!.
-
#with(new_key_vals) ⇒ Object
Update a simple record non-destructively.
Methods inherited from Array
#apply, #bind, box, #mplus, mzero, #or_else, #unbox, unit
Constructor Details
#initialize(key_vals) ⇒ SimpleRecord
Create a new SimpleRecord. Pass a Hash of keys and associated values if you want an inline record.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/funkr/types/simple_record.rb', line 42 def initialize(key_vals) fields = self.class.fields_list if not fields.nil? then # record paramétré if fields == key_vals.keys then # tout va bien @allowed_keys = fields @key_vals = key_vals rebuild_array else raise "#{self.class.to_s} wrong initialization, expected #{fields}" end else # record anonyme @allowed_keys = key_vals.keys @key_vals = key_vals key_vals.each do |k,v| getter = k.to_sym setter = format("%s=", k.to_s).to_sym define_singleton_method(getter){ @key_vals[k] } define_singleton_method(setter){|nv| @key_vals[k] = nv; rebuild_array} self.push(v) end end end |
Class Attribute Details
.fields_list ⇒ Object
Returns the value of attribute fields_list.
27 28 29 |
# File 'lib/funkr/types/simple_record.rb', line 27 def fields_list @fields_list end |
Class Method Details
.fields(*flds) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/funkr/types/simple_record.rb', line 30 def self.fields(*flds) self.fields_list = flds flds.each do |f| getter = f.to_sym setter = format("%s=", f.to_s).to_sym define_method(getter){ @key_vals[f] } define_method(setter){|nv| @key_vals[f] = nv; rebuild_array} end end |
Instance Method Details
#to_hash ⇒ Object
77 |
# File 'lib/funkr/types/simple_record.rb', line 77 def to_hash; @key_vals; end |
#to_s ⇒ Object
79 |
# File 'lib/funkr/types/simple_record.rb', line 79 def to_s; @key_vals.to_s; end |
#update!(new_key_vals) ⇒ Object
Update a simple record destructively !!
70 71 72 73 74 75 |
# File 'lib/funkr/types/simple_record.rb', line 70 def update!(new_key_vals) check_keys(new_key_vals.keys) @key_vals.merge!(new_key_vals) rebuild_array self end |
#with(new_key_vals) ⇒ Object
Update a simple record non-destructively
64 65 66 67 |
# File 'lib/funkr/types/simple_record.rb', line 64 def with(new_key_vals) check_keys(new_key_vals.keys) self.class.new(@key_vals.merge(new_key_vals)) end |