Class: DohDb::WritableRow

Inherits:
AbstractRow show all
Defined in:
lib/doh/mysql/writable_row.rb

Instance Attribute Summary collapse

Attributes inherited from AbstractRow

#keys, #values

Instance Method Summary collapse

Methods inherited from AbstractRow

#at, #each_pair, #empty_field?, #get, #inspect, #key?, #record_id, #size, #to_a, #to_h

Constructor Details

#initialize(*args) ⇒ WritableRow

can accept 2 arguments: keys array, values array or 1 argument: a hash



11
12
13
14
15
16
# File 'lib/doh/mysql/writable_row.rb', line 11

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

Raises:

  • (RuntimeError)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/doh/mysql/writable_row.rb', line 40

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_keysObject (readonly)

Returns the value of attribute changed_keys.



7
8
9
# File 'lib/doh/mysql/writable_row.rb', line 7

def changed_keys
  @changed_keys
end

Instance Method Details

#clear_changed_keysObject



36
37
38
# File 'lib/doh/mysql/writable_row.rb', line 36

def clear_changed_keys
  @changed_keys.clear
end

#initialize_copy(orig) ⇒ Object



18
19
20
21
# File 'lib/doh/mysql/writable_row.rb', line 18

def initialize_copy(orig)
  super(orig)
  @changed_keys = Set.new
end

#set(key, value) ⇒ Object Also known as: []=



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/doh/mysql/writable_row.rb', line 23

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