Class: Knj::Db_row
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(args) ⇒ Db_row
Returns a new instance of Db_row.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 6
def initialize(args)
@args = {}
args.each do |key, value|
@args[key.to_sym] = value
end
@args[:db] = $db if !@args[:db] and $db and $db.class.to_s == "Knj::Db"
@args[:objects] = $objects if !@args[:objects] and $objects and $objects.is_a?(Knj::Objects)
@args[:col_id] = :id if !@args[:col_id]
raise "No table given." if !@args[:table]
if @args[:data] and (@args[:data].is_a?(Integer) or @args[:data].is_a?(Fixnum) or @args[:data].is_a?(String))
@data = {@args[:col_id].to_sym => @args[:data].to_s}
self.reload
elsif @args[:data] and @args[:data].is_a?(Hash)
@data = {}
@args[:data].each do |key, value|
@data[key.to_sym] = value
end
elsif @args[:id]
@data = {}
@data[@args[:col_id].to_sym] = @args[:id]
self.reload
else
raise ArgumentError.new("Invalid data: #{@args[:data].to_s} (#{@args[:data].class.to_s})")
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 141
def method_missing(*args)
func_name = args[0].to_s
if match = func_name.match(/^(\S+)\?$/) and @data.key?(match[1].to_sym)
if @data[match[1].to_sym] == "1" or @data[match[1].to_sym] == "yes"
return true
elsif @data[match[1].to_sym] == "0" or @data[match[1].to_sym] == "no"
return false
end
end
raise sprintf("No such method: %s", func_name)
end
|
Instance Attribute Details
Returns the value of attribute args.
2
3
4
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 2
def args
@args
end
|
Returns the value of attribute data.
2
3
4
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 2
def data
@data
end
|
Instance Method Details
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 89
def [](key)
raise "No valid key given." if !key
raise "No data was loaded on the object? Maybe you are trying to call a deleted object?" if !@data
if @data.key?(key)
return @data[key]
elsif @data.key?(key.to_sym)
return @data[key.to_sym]
elsif @data.key?(key.to_s)
return @data[key.to_s]
end
raise "No such key: #{key.to_s}."
end
|
#[]=(key, value) ⇒ Object
104
105
106
107
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 104
def []=(key, value)
self.update(key.to_sym => value)
self.reload
end
|
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 34
def db
if !@args[:force_selfdb]
curthread = Thread.current
if curthread.is_a?(Knj::Thread) and curthread[:knjappserver] and curthread[:knjappserver][:db]
return curthread[:knjappserver][:db]
end
end
return @args[:db]
end
|
75
76
77
78
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 75
def delete
self.db.delete(@args[:table], {@args[:col_id] => self.id})
self.destroy
end
|
80
81
82
83
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 80
def destroy
@args = nil
@data = nil
end
|
#each(&args) ⇒ Object
129
130
131
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 129
def each(&args)
return @data.each(&args)
end
|
137
138
139
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 137
def esc(str)
return self.db.escape(str)
end
|
#has_key?(key) ⇒ Boolean
85
86
87
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 85
def has_key?(key)
return @data.key?(key.to_sym)
end
|
109
110
111
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 109
def id
return @data[@args[:col_id]]
end
|
#is_knj? ⇒ Boolean
4
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 4
def is_knj?; return true; end
|
#ob ⇒ Object
Also known as:
objects
45
46
47
48
49
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 45
def ob
return @args[:objects] if @args.key?(:objects)
return $ob if $ob and $ob.is_a?(Knj::Objects)
return false
end
|
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 53
def reload
last_id = self.id
data = self.db.single(@args[:table], {@args[:col_id] => self.id})
if !data
raise Errno::ENOENT.new("Could not find any data for the object with ID: '#{last_id}' in the table '#{@args[:table].to_s}'.")
end
@data = {}
data.each do |key, value|
@data[key.to_sym] = value
end
end
|
#title ⇒ Object
Also known as:
name
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 113
def title
if @args[:col_title]
return @data[@args[:col_title].to_sym]
end
if @data.key?(:title)
return @data[:title]
elsif @data.key?(:name)
return @data[:name]
end
raise "'col_title' has not been set for the class: '#{self.class.to_s}'."
end
|
133
134
135
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 133
def to_hash
return @data.clone
end
|
#update(newdata) ⇒ Object
66
67
68
69
70
71
72
73
|
# File 'lib/knj/knjdb/libknjdb_row.rb', line 66
def update(newdata)
self.db.update(@args[:table], newdata, {@args[:col_id] => self.id})
self.reload
if self.ob
self.ob.call("object" => self, "signal" => "update")
end
end
|