Class: DohDb::AbstractSmartRow

Inherits:
AbstractRow show all
Defined in:
lib/dohmysql/smart_row.rb

Direct Known Subclasses

SmartRow

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?, #size, #to_a, #to_h

Constructor Details

#initialize(keys, values) ⇒ AbstractSmartRow

Returns a new instance of AbstractSmartRow.



9
10
11
12
13
# File 'lib/dohmysql/smart_row.rb', line 9

def initialize(keys, values)
  @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
# File 'lib/dohmysql/smart_row.rb', line 41

def method_missing(sym, *args)
  name = sym.to_s
  if name.end_with?('=')
    guess_missing_set(name[0..-2], args.first)
  else
    guess_missing_get(name)
  end
end

Instance Attribute Details

#changed_keysObject (readonly)

Returns the value of attribute changed_keys.



7
8
9
# File 'lib/dohmysql/smart_row.rb', line 7

def changed_keys
  @changed_keys
end

#tableObject

Returns the value of attribute table.



6
7
8
# File 'lib/dohmysql/smart_row.rb', line 6

def table
  @table
end

Instance Method Details

#clear_changed_keysObject



37
38
39
# File 'lib/dohmysql/smart_row.rb', line 37

def clear_changed_keys
  @changed_keys.clear
end

#delete(key) ⇒ Object



54
55
56
57
58
59
# File 'lib/dohmysql/smart_row.rb', line 54

def delete(key)
  index = @keys.index(key)
  return unless index
  @keys.delete_at(index)
  @values.delete_at(index)
end

#guess_missing_get(key) ⇒ Object



61
62
63
64
# File 'lib/dohmysql/smart_row.rb', line 61

def guess_missing_get(key)
  return get(key) if key?(key)
  raise "unknown field: #{key}"
end

#guess_missing_set(key, value) ⇒ Object



66
67
68
# File 'lib/dohmysql/smart_row.rb', line 66

def guess_missing_set(key, value)
  set(key, value)
end

#initialize_copy(orig) ⇒ Object



15
16
17
18
19
# File 'lib/dohmysql/smart_row.rb', line 15

def initialize_copy(orig)
  @keys = @keys.dup
  @values = @values.dup
  @changed_keys = @changed_keys.dup
end

#merge!(hash) ⇒ Object



50
51
52
# File 'lib/dohmysql/smart_row.rb', line 50

def merge!(hash)
  hash.each_pair { |key, value| set(key, value) }
end

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dohmysql/smart_row.rb', line 21

def set(key, value, flag_changed = true)
  index = @keys.index(key)
  if index
    if @values[index] != value
      @values[index] = value
      @changed_keys.add(key)
    end
  else
    @keys.push(key)
    @values.push(value)
    @changed_keys.add(key)
  end
  value
end