Class: ModelBase
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Searchable
find_by, joins, left_joins, select, where
assoc_options, belongs_to, has_many, has_many_through, has_one
#after_init, #before_valid, included
#errors, #valid?
Constructor Details
#initialize(params = {}) ⇒ ModelBase
Returns a new instance of ModelBase.
85
86
87
88
89
90
91
92
93
|
# File 'lib/scaffold/lib/model/model_base.rb', line 85
def initialize(params = {})
self.class.finalize!
params.each do |attribute, val|
setter = attribute.to_s + "="
raise "Unknown attribute '#{k}'" unless self.class.method_defined?(setter.to_sym)
self.send(setter, val)
end
end
|
Class Method Details
.all ⇒ Object
47
48
49
50
|
# File 'lib/scaffold/lib/model/model_base.rb', line 47
def self.all
results = DBConnection.execute("Select * FROM #{self.table_name}")
parse_all(results)
end
|
.columns ⇒ Object
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/scaffold/lib/model/model_base.rb', line 15
def self.columns
@columns ||= DBConnection.execute2(<<-SQL)
Select
*
FROM
'#{self.table_name}'
LIMIT 0
SQL
.first.map!(&:to_sym)
end
|
.finalize! ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/scaffold/lib/model/model_base.rb', line 26
def self.finalize!
columns.each do |column|
define_method(column) do
attributes[column]
end
setter_name = column.to_s + "="
define_method(setter_name) do |val|
attributes[column] = val
end
end
end
|
.find(id) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/scaffold/lib/model/model_base.rb', line 72
def self.find(id)
results = DBConnection.execute(<<-SQL, id)
Select
*
FROM
#{table_name}
WHERE
id = ?
SQL
parse_all(results).first
end
|
.first ⇒ Object
52
53
54
55
|
# File 'lib/scaffold/lib/model/model_base.rb', line 52
def self.first
results = DBConnection.execute("Select * FROM #{self.table_name} LIMIT 1")
parse_all(results).first
end
|
.last ⇒ Object
57
58
59
|
# File 'lib/scaffold/lib/model/model_base.rb', line 57
def self.last
all.last
end
|
.parse_all(results) ⇒ Object
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/scaffold/lib/model/model_base.rb', line 61
def self.parse_all(results)
results.map do |arr|
params = {}
columns.each_with_index do |column, idx|
params[column] = arr[idx]
end
self.new(params)
end
end
|
.table_name ⇒ Object
43
44
45
|
# File 'lib/scaffold/lib/model/model_base.rb', line 43
def self.table_name
@table_name ||= self.name.underscore.pluralize
end
|
.table_name=(table_name) ⇒ Object
39
40
41
|
# File 'lib/scaffold/lib/model/model_base.rb', line 39
def self.table_name=(table_name)
@table_name = table_name
end
|
Instance Method Details
#attribute_values ⇒ Object
114
115
116
|
# File 'lib/scaffold/lib/model/model_base.rb', line 114
def attribute_values
attributes.values
end
|
#attributes ⇒ Object
110
111
112
|
# File 'lib/scaffold/lib/model/model_base.rb', line 110
def attributes
@attributes ||= {}
end
|
#destroy ⇒ Object
136
137
138
139
140
141
142
143
144
|
# File 'lib/scaffold/lib/model/model_base.rb', line 136
def destroy
DBConnection.instance.execute(<<-SQL, id)
DELETE
FROM
#{self.class.table_name}
WHERE
id = ?
SQL
end
|
#save ⇒ Object
118
119
120
121
122
123
124
125
|
# File 'lib/scaffold/lib/model/model_base.rb', line 118
def save
if self.valid?
id ? update_database : insert
true
else
false
end
end
|
#save! ⇒ Object
127
128
129
130
131
132
133
134
|
# File 'lib/scaffold/lib/model/model_base.rb', line 127
def save!
if self.valid?
id ? update_database : insert
self
else
raise self.errors.join(", ")
end
end
|
#update(params = {}) ⇒ Object
95
96
97
98
99
100
101
102
103
|
# File 'lib/scaffold/lib/model/model_base.rb', line 95
def update(params = {})
params.each do |attribute, val|
setter = attribute.to_s + "="
raise "Unknown attribute '#{k}'" unless self.class.method_defined?(setter.to_sym)
self.send(setter, val)
end
self
end
|
#update_attributes(params = {}) ⇒ Object
105
106
107
108
|
# File 'lib/scaffold/lib/model/model_base.rb', line 105
def update_attributes(params = {})
update(params)
save ? true : false
end
|