Class: ModelBase
- Inherits:
-
Object
show all
- Extended by:
- Associatable, Searchable
- Defined in:
- lib/reloj/orm/model_base.rb,
lib/reloj/orm/searchable.rb,
lib/reloj/orm/associatable.rb
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 = {}) ⇒ ModelBase
Returns a new instance of ModelBase.
76
77
78
79
80
81
82
83
|
# File 'lib/reloj/orm/model_base.rb', line 76
def initialize(params = {})
params.each do |column, value|
unless self.class.columns.include?(column.to_sym)
raise "unknown attribute '#{column}'"
end
self.send("#{column}=", value)
end
end
|
Class Method Details
.all ⇒ Object
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/reloj/orm/model_base.rb', line 43
def self.all
results_array = Database.execute(<<-SQL)
SELECT
*
FROM
#{table_name};
SQL
self.parse_all(results_array)
end
|
.columns ⇒ Object
def self.inherited(subclass)
attr_writer(*subclass.columns)
end
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/reloj/orm/model_base.rb', line 13
def self.columns
@columns ||= Database.execute(<<-SQL).fields.map(&:to_sym)
SELECT
*
FROM
#{self.table_name}
LIMIT
0;
SQL
end
|
.finalize! ⇒ Object
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/reloj/orm/model_base.rb', line 24
def self.finalize!
self.columns.each do |column|
define_method("#{column}=") do |val|
attributes[column] = val
end
define_method(column) do
attributes[column]
end
end
end
|
.find(id) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/reloj/orm/model_base.rb', line 63
def self.find(id)
res = Database.execute(<<-SQL, [id])
SELECT
*
FROM
#{table_name}
WHERE
id = $1;
SQL
res.num_tuples.zero? ? nil : self.new(res.first)
end
|
.parse_all(results) ⇒ Object
54
55
56
57
58
59
60
61
|
# File 'lib/reloj/orm/model_base.rb', line 54
def self.parse_all(results)
answer = []
results.each do |result|
answer << self.new(result)
end
answer
end
|
.table_name ⇒ Object
39
40
41
|
# File 'lib/reloj/orm/model_base.rb', line 39
def self.table_name
@table_name || self.to_s.tableize
end
|
.table_name=(table_name) ⇒ Object
35
36
37
|
# File 'lib/reloj/orm/model_base.rb', line 35
def self.table_name=(table_name)
@table_name = table_name
end
|
Instance Method Details
#attribute_values ⇒ Object
89
90
91
92
93
|
# File 'lib/reloj/orm/model_base.rb', line 89
def attribute_values
self.class.columns.map do |column|
self.send(column)
end
end
|
#attributes ⇒ Object
85
86
87
|
# File 'lib/reloj/orm/model_base.rb', line 85
def attributes
@attributes ||= {}
end
|
#destroy ⇒ Object
116
117
118
119
120
121
122
123
|
# File 'lib/reloj/orm/model_base.rb', line 116
def destroy
Database.execute(<<-SQL)
DELETE FROM
#{self.class.table_name}
WHERE
id = #{self.id};
SQL
end
|
#insert ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/reloj/orm/model_base.rb', line 99
def insert
my_columns = self.class.columns.drop(1)
col_names = my_columns.join(", ")
n = my_columns.length
placeholders = [*1..n].map { |num| "$" + num.to_s }.join(', ')
response = Database.execute(<<-SQL, self.not_id_attribute_values)
INSERT INTO
#{self.class.table_name} (#{col_names})
VALUES
(#{placeholders})
RETURNING
id;
SQL
self.id = response[0]['id'].to_i
end
|
#not_id_attribute_values ⇒ Object
95
96
97
|
# File 'lib/reloj/orm/model_base.rb', line 95
def not_id_attribute_values
self.class.columns.drop(1).map { |column| self.send(column) }
end
|
#save ⇒ Object
140
141
142
|
# File 'lib/reloj/orm/model_base.rb', line 140
def save
id.nil? ? insert : update
end
|
#update ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/reloj/orm/model_base.rb', line 125
def update
columns_line = self.class.columns.map.with_index do |col_name, i|
"#{col_name} = $#{i + 1}"
end.join(", ")
Database.execute(<<-SQL, self.attribute_values)
UPDATE
#{self.class.table_name}
SET
#{columns_line}
WHERE
id = #{self.id};
SQL
end
|