Class: DohDb::WritableRow
- Inherits:
-
AbstractRow
- Object
- AbstractRow
- DohDb::WritableRow
- Defined in:
- lib/dohmysql/writable_row.rb
Instance Attribute Summary collapse
-
#changed_keys ⇒ Object
readonly
Returns the value of attribute changed_keys.
Attributes inherited from AbstractRow
Instance Method Summary collapse
- #clear_changed_keys ⇒ Object
-
#initialize(*args) ⇒ WritableRow
constructor
can accept 2 arguments: keys array, values array or 1 argument: a hash.
- #initialize_copy(orig) ⇒ Object
- #method_missing(sym, *args) ⇒ Object
- #set(key, value) ⇒ Object (also: #[]=)
Methods inherited from AbstractRow
#at, #each_pair, #empty_field?, #get, #inspect, #key?, #size, #to_a, #to_h
Constructor Details
#initialize(*args) ⇒ WritableRow
can accept 2 arguments: keys array, values array or 1 argument: a hash
12 13 14 15 16 17 |
# File 'lib/dohmysql/writable_row.rb', line 12 def initialize(*args) keys, values = parse_initialize_args(*args) @keys = keys.dup @values = values.dup @changed_keys = Set.new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dohmysql/writable_row.rb', line 41 def method_missing(sym, *args) name = sym.to_s if name.lastn(1) == '=' key = name[0..-2] assign = true else key = name end raise RuntimeError.new("unknown field: " + name) unless key?(key) if assign set(key, args.first) else get(key) end end |
Instance Attribute Details
#changed_keys ⇒ Object (readonly)
Returns the value of attribute changed_keys.
8 9 10 |
# File 'lib/dohmysql/writable_row.rb', line 8 def changed_keys @changed_keys end |
Instance Method Details
#clear_changed_keys ⇒ Object
37 38 39 |
# File 'lib/dohmysql/writable_row.rb', line 37 def clear_changed_keys @changed_keys.clear end |
#initialize_copy(orig) ⇒ Object
19 20 21 22 |
# File 'lib/dohmysql/writable_row.rb', line 19 def initialize_copy(orig) super(orig) @changed_keys = Set.new end |
#set(key, value) ⇒ Object Also known as: []=
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/dohmysql/writable_row.rb', line 24 def set(key, value) index = @keys.index(key) if index @values[index] = value else @keys.push(key) @values.push(value) end @changed_keys.add(key) value end |