Class: DohDb::AbstractSmartRow
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
Returns a new instance of AbstractSmartRow.
10
11
12
13
14
|
# File 'lib/dohmysql/smart_row.rb', line 10
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
42
43
44
45
46
47
48
49
|
# File 'lib/dohmysql/smart_row.rb', line 42
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_keys ⇒ Object
Returns the value of attribute changed_keys.
8
9
10
|
# File 'lib/dohmysql/smart_row.rb', line 8
def changed_keys
@changed_keys
end
|
#table ⇒ Object
Returns the value of attribute table.
7
8
9
|
# File 'lib/dohmysql/smart_row.rb', line 7
def table
@table
end
|
Instance Method Details
#clear_changed_keys ⇒ Object
38
39
40
|
# File 'lib/dohmysql/smart_row.rb', line 38
def clear_changed_keys
@changed_keys.clear
end
|
#db_update ⇒ Object
71
72
73
74
75
76
77
78
|
# File 'lib/dohmysql/smart_row.rb', line 71
def db_update
return if @changed_keys.empty?
sqlb = SqlStmt::Update.new.table(@table)
sqlb.where("#{primary_key} = #{get(primary_key).to_sql}")
@changed_keys.each {|key| sqlb.field(key, get(key).to_sql)}
Doh.db.query(sqlb)
@changed_keys.clear
end
|
#delete(key) ⇒ Object
55
56
57
58
59
60
|
# File 'lib/dohmysql/smart_row.rb', line 55
def delete(key)
index = @keys.index(key)
return unless index
@keys.delete_at(index)
@values.delete_at(index)
end
|
#guess_missing_get(key) ⇒ Object
62
63
64
65
|
# File 'lib/dohmysql/smart_row.rb', line 62
def guess_missing_get(key)
return get(key) if key?(key)
raise "unknown field: #{key}"
end
|
#guess_missing_set(key, value) ⇒ Object
67
68
69
|
# File 'lib/dohmysql/smart_row.rb', line 67
def guess_missing_set(key, value)
set(key, value)
end
|
#initialize_copy(orig) ⇒ Object
16
17
18
19
20
|
# File 'lib/dohmysql/smart_row.rb', line 16
def initialize_copy(orig)
@keys = @keys.dup
@values = @values.dup
@changed_keys = @changed_keys.dup
end
|
#merge!(hash) ⇒ Object
51
52
53
|
# File 'lib/dohmysql/smart_row.rb', line 51
def merge!(hash)
hash.each_pair { |key, value| set(key, value) }
end
|
#primary_key ⇒ Object
80
81
82
|
# File 'lib/dohmysql/smart_row.rb', line 80
def primary_key
@primary_key ||= "#{@table}_id"
end
|
#set(key, value, flag_changed = true) ⇒ Object
Also known as:
[]=
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/dohmysql/smart_row.rb', line 22
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
|