Class: Baza::Row
Instance Attribute Summary collapse
Instance Method Summary
collapse
#model_name, #to_model
Constructor Details
#initialize(args) ⇒ Row
Returns a new instance of Row.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/baza/row.rb', line 10
def initialize(args)
@args = {}
args.each do |key, value|
@args[key.to_sym] = value
end
@args[:col_id] ||= :id
raise "No table given." unless @args[:table]
if @args[:data] && (@args[:data].is_a?(Integer) || @args[:data].is_a?(Fixnum) || @args[:data].is_a?(String))
@data = {@args[:col_id].to_sym => @args[:data].to_s}
reload
elsif @args[:data] && @args.fetch(:data).is_a?(Hash)
@data = {}
@args.fetch(:data).each do |key, value|
key = key.to_sym unless key.is_a?(Fixnum)
@data[key] = value
end
elsif @args[:id]
@data = {}
@data[@args[:col_id].to_sym] = @args[:id]
reload
else
raise ArgumentError, "Invalid data: #{@args[:data]} (#{@args[:data].class})"
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(func_name, *args) ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/baza/row.rb', line 140
def method_missing(func_name, *args)
if (match = func_name.to_s.match(/^(\S+)\?$/)) && @data.key?(match[1].to_sym)
if @data.fetch(match[1].to_sym) == "1" || @data.fetch(match[1].to_sym) == "yes"
return true
elsif @data.fetch(match[1].to_sym) == "0" || @data.fetch(match[1].to_sym) == "no"
return false
end
end
super
end
|
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
4
5
6
|
# File 'lib/baza/row.rb', line 4
def args
@args
end
|
#data ⇒ Object
Returns the value of attribute data.
4
5
6
|
# File 'lib/baza/row.rb', line 4
def data
@data
end
|
Instance Method Details
#[](key) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/baza/row.rb', line 82
def [](key)
raise "No valid key given." unless key
raise "No data was loaded on the object? Maybe you are trying to call a deleted object?" unless @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}."
end
|
#[]=(key, value) ⇒ Object
97
98
99
100
|
# File 'lib/baza/row.rb', line 97
def []=(key, value)
update(key.to_sym => value)
reload
end
|
#db ⇒ Object
37
38
39
|
# File 'lib/baza/row.rb', line 37
def db
@args.fetch(:db)
end
|
#delete ⇒ Object
68
69
70
71
|
# File 'lib/baza/row.rb', line 68
def delete
db.delete(@args.fetch(:table), @args.fetch(:col_id) => id)
destroy
end
|
#destroy ⇒ Object
73
74
75
76
|
# File 'lib/baza/row.rb', line 73
def destroy
@args = nil
@data = nil
end
|
#each(*args, &blk) ⇒ Object
124
125
126
|
# File 'lib/baza/row.rb', line 124
def each(*args, &blk)
@data.each(*args, &blk)
end
|
#each_value(*args, &blk) ⇒ Object
128
129
130
|
# File 'lib/baza/row.rb', line 128
def each_value(*args, &blk)
@data.each_value(*args, &blk)
end
|
#esc(str) ⇒ Object
136
137
138
|
# File 'lib/baza/row.rb', line 136
def esc(str)
db.escape(str)
end
|
#id ⇒ Object
102
103
104
|
# File 'lib/baza/row.rb', line 102
def id
@data.fetch(@args.fetch(:col_id))
end
|
#key?(key) ⇒ Boolean
78
79
80
|
# File 'lib/baza/row.rb', line 78
def key?(key)
@data.key?(key.to_sym)
end
|
#knj? ⇒ Boolean
6
7
8
|
# File 'lib/baza/row.rb', line 6
def knj?
true
end
|
#ob ⇒ Object
Also known as:
objects
41
42
43
44
|
# File 'lib/baza/row.rb', line 41
def ob
return @args[:objects] if @args.key?(:objects)
false
end
|
#reload ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/baza/row.rb', line 48
def reload
last_id = id
data = db.single(@args[:table], @args[:col_id] => id)
unless data
raise Errno::ENOENT, "Could not find any data for the object with ID: '#{last_id}' in the table '#{@args[:table]}'."
end
@data = {}
data.each do |key, value|
@data[key.to_sym] = value
end
end
|
#title ⇒ Object
Also known as:
name
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/baza/row.rb', line 110
def title
return @data[@args.fetch(:col_title).to_sym] if @args[:col_title]
if @data.key?(:title)
return @data.fetch(:title)
elsif @data.key?(:name)
return @data.fetch(:name)
end
raise "'col_title' has not been set for the class: '#{self.class}'."
end
|
#to_hash ⇒ Object
132
133
134
|
# File 'lib/baza/row.rb', line 132
def to_hash
@data.clone
end
|
#to_param ⇒ Object
106
107
108
|
# File 'lib/baza/row.rb', line 106
def to_param
id
end
|
#update(newdata) ⇒ Object
61
62
63
64
65
66
|
# File 'lib/baza/row.rb', line 61
def update(newdata)
db.update(@args.fetch(:table), newdata, @args.fetch(:col_id) => id)
reload
ob.call("object" => self, "signal" => "update") if ob
end
|