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?, #record_id, #size, #to_a, #to_h
Constructor Details
Returns a new instance of AbstractSmartRow.
21
22
23
24
25
26
|
# File 'lib/doh/mysql/smart_row.rb', line 21
def initialize(keys, values)
@keys = keys.dup
@values = values.dup
@changed_keys = Set.new
@cached_virtuals = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
64
65
66
67
68
69
70
71
|
# File 'lib/doh/mysql/smart_row.rb', line 64
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.
19
20
21
|
# File 'lib/doh/mysql/smart_row.rb', line 19
def changed_keys
@changed_keys
end
|
Returns the value of attribute table.
18
19
20
|
# File 'lib/doh/mysql/smart_row.rb', line 18
def table
@table
end
|
Instance Method Details
#clear_changed_keys ⇒ Object
60
61
62
|
# File 'lib/doh/mysql/smart_row.rb', line 60
def clear_changed_keys
@changed_keys.clear
end
|
#db_insert ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/doh/mysql/smart_row.rb', line 88
def db_insert
newid = DohDb::insert_hash(self, @table)
if newid != 0
set(primary_key, newid, false)
end
newid
end
|
#db_update ⇒ Object
96
97
98
99
100
101
102
103
104
|
# File 'lib/doh/mysql/smart_row.rb', line 96
def db_update
return if @changed_keys.empty?
before_db_update
builder = BuildSQL::SimpleUpdateRow.new(@table, nil, get(primary_key), primary_key)
@changed_keys.each {|key| builder.setc(key, get(key).to_sql)}
DohDb::query(builder)
after_db_update
@changed_keys.clear
end
|
#delete(key) ⇒ Object
77
78
79
80
81
82
|
# File 'lib/doh/mysql/smart_row.rb', line 77
def delete(key)
index = @keys.index(key)
return unless index
@keys.delete_at(index)
@values.delete_at(index)
end
|
#display(key = nil) ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/doh/mysql/smart_row.rb', line 36
def display(key = nil)
if key.nil?
@display_proxy ||= RowDisplayProxy.new(self)
else
get(key).to_display
end
end
|
#initialize_copy(orig) ⇒ Object
28
29
30
31
32
33
34
|
# File 'lib/doh/mysql/smart_row.rb', line 28
def initialize_copy(orig)
@keys = @keys.dup
@values = @values.dup
@changed_keys = @changed_keys.dup
@cached_virtuals = @cached_virtuals.dup
@display_proxy = nil
end
|
#merge!(hash) ⇒ Object
73
74
75
|
# File 'lib/doh/mysql/smart_row.rb', line 73
def merge!(hash)
hash.each_pair { |key, value| set(key, value) }
end
|
#record_id=(value) ⇒ Object
84
85
86
|
# File 'lib/doh/mysql/smart_row.rb', line 84
def record_id=(value)
set(primary_key, value)
end
|
#set(key, value, flag_changed = true) ⇒ Object
Also known as:
[]=
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/doh/mysql/smart_row.rb', line 44
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
|