Class: MonoRM::Base
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Searchable
where
assoc_options, belongs_to, has_many, has_one_through
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
68
69
70
71
72
73
74
|
# File 'lib/monorm/base.rb', line 68
def initialize(params = {})
params.each do |attr_name, val|
raise "unknown attribute '#{attr_name}'" unless self.class.columns.include?(attr_name.to_sym)
send(:"#{attr_name}=", val)
end
end
|
Class Method Details
.all ⇒ Object
39
40
41
42
43
44
45
46
47
|
# File 'lib/monorm/base.rb', line 39
def self.all
data = MonoRM::DBConnection.execute(<<-SQL)
SELECT
*
FROM
#{self.table_name}
SQL
parse_all(data)
end
|
.columns ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/monorm/base.rb', line 8
def self.columns
@cols ||= MonoRM::DBConnection.cols_exec(<<-SQL)
SELECT
*
FROM
#{self.table_name}
LIMIT
1
SQL
@cols.map{|col| col.to_sym}
end
|
.finalize! ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/monorm/base.rb', line 22
def self.finalize!
self.columns.each do |col|
define_method(:"#{col}=") { |arg| self.attributes[col] = arg }
define_method(:"#{col}") {self.attributes[col]}
end
end
|
.find(id) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/monorm/base.rb', line 55
def self.find(id)
record = MonoRM::DBConnection.execute(<<-SQL, id)
SELECT
*
FROM
#{self.table_name}
WHERE
id = INTERPOLATOR_MARK
SQL
self.parse_all(record).first
end
|
.parse_all(results) ⇒ Object
49
50
51
52
53
|
# File 'lib/monorm/base.rb', line 49
def self.parse_all(results)
results.map do |result|
self.new(result)
end
end
|
.table_name ⇒ Object
34
35
36
37
|
# File 'lib/monorm/base.rb', line 34
def self.table_name
return @table_name unless @table_name.nil?
@table_name = self.to_s.tableize
end
|
.table_name=(table_name = nil) ⇒ Object
30
31
32
|
# File 'lib/monorm/base.rb', line 30
def self.table_name=(table_name = nil)
@table_name = table_name
end
|
Instance Method Details
#attribute_values ⇒ Object
80
81
82
83
|
# File 'lib/monorm/base.rb', line 80
def attribute_values
self.attributes.values.map {|attribute| attribute}
end
|
#attributes ⇒ Object
76
77
78
|
# File 'lib/monorm/base.rb', line 76
def attributes
@attributes ||= {}
end
|
#destroy ⇒ Object
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/monorm/base.rb', line 130
def destroy
id = self.id
MonoRM::DBConnection.execute(<<-SQL, id)
DELETE
FROM
#{self.class.table_name}
WHERE
id = INTERPOLATOR_MARK
SQL
end
|
#insert ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/monorm/base.rb', line 85
def insert
col_names = self.class.columns.drop(1).join(', ')
question_marks = ['INTERPOLATOR_MARK'] * attribute_values.length
question_marks = question_marks.join(', ')
MonoRM::DBConnection.execute(<<-SQL, *attribute_values)
INSERT INTO
#{self.class.table_name}(#{col_names})
VALUES
(#{question_marks})
SQL
send(:id=, MonoRM::DBConnection.last_insert_row_id)
end
|
#save ⇒ Object
122
123
124
125
126
127
128
|
# File 'lib/monorm/base.rb', line 122
def save
if self.class.find(self.id)
update
else
insert
end
end
|
#update ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/monorm/base.rb', line 99
def update
attribs = attribute_values.drop(1)
id = self.id
cols = self.class.columns.drop(1)
set_lines = cols.map do |col|
"#{col} = INTERPOLATOR_MARK"
end
set_lines = set_lines.join(', ')
MonoRM::DBConnection.execute(<<-SQL, *attribs, id)
UPDATE
#{self.class.table_name}
SET
#{set_lines}
WHERE
id = INTERPOLATOR_MARK
SQL
end
|