Class: OpalORM::SQLObject
- Inherits:
-
Object
- Object
- OpalORM::SQLObject
show all
- Extended by:
- Searchable
- Defined in:
- lib/opal_orm/searchable.rb,
lib/opal_orm/sql_object.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Searchable
where
Constructor Details
#initialize(params = {}) ⇒ SQLObject
Returns a new instance of SQLObject.
6
7
8
9
10
11
12
13
14
15
|
# File 'lib/opal_orm/sql_object.rb', line 6
def initialize(params = {})
params.each do |attribute,val|
col_sym = attribute.to_sym
if self.class.columns.include?(col_sym)
self.send("#{col_sym}=",val)
else
raise "unknown attribute '#{attribute}'"
end
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *args) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/opal_orm/sql_object.rb', line 131
def method_missing(method_sym, *args)
if self.class.is_column?(method_sym)
self.class.define_getter(method_sym)
return send(method_sym)
end
setter_name = /(.*)=/.match(method_sym)
if setter_name && self.class.is_column?(setter_name[1].to_sym)
self.class.define_setter(setter_name[1].to_sym)
p method_sym
send(method_sym, args[0])
else
super(method_sym, args)
end
end
|
Class Method Details
.all ⇒ Object
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/opal_orm/sql_object.rb', line 66
def self.all
query = <<-SQL
SELECT
*
FROM
#{self.table_name}
SQL
objs = DBConnection.execute(query)
parse_all(objs)
end
|
.columns ⇒ Object
17
18
19
20
21
22
23
24
|
# File 'lib/opal_orm/sql_object.rb', line 17
def self.columns
@cols ||= DBConnection.execute2(<<-SQL).first.map(&:to_sym)
SELECT
*
FROM
#{table_name}
SQL
end
|
.define_getter(attr_name) ⇒ Object
46
47
48
49
50
|
# File 'lib/opal_orm/sql_object.rb', line 46
def self.define_getter(attr_name)
define_method(attr_name) do
attributes[attr_name]
end
end
|
.define_setter(attr_name) ⇒ Object
52
53
54
55
56
|
# File 'lib/opal_orm/sql_object.rb', line 52
def self.define_setter(attr_name)
define_method("#{attr_name}=") do |new_val|
attributes[attr_name] = new_val
end
end
|
.finalize! ⇒ Object
39
40
41
42
43
44
|
# File 'lib/opal_orm/sql_object.rb', line 39
def self.finalize!
columns.each do |col_sym|
define_getter(col_sym)
define_setter(col_sym)
end
end
|
.find(id) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/opal_orm/sql_object.rb', line 85
def self.find(id)
result = DBConnection.execute(<<-SQL,id)
SELECT
*
FROM
#{table_name}
WHERE
id = ?
SQL
return nil if result.empty?
new(result.first)
end
|
.is_column?(col_sym) ⇒ Boolean
127
128
129
|
# File 'lib/opal_orm/sql_object.rb', line 127
def self.is_column?(col_sym)
self.columns.include?(col_sym)
end
|
.parse_all(results) ⇒ Object
77
78
79
80
81
82
83
|
# File 'lib/opal_orm/sql_object.rb', line 77
def self.parse_all(results)
objs = []
results.each do |obj|
objs << new(obj)
end
objs
end
|
.table_name ⇒ Object
62
63
64
|
# File 'lib/opal_orm/sql_object.rb', line 62
def self.table_name
@table_name ||= self.to_s.downcase.pluralize
end
|
.table_name=(table_name) ⇒ Object
58
59
60
|
# File 'lib/opal_orm/sql_object.rb', line 58
def self.table_name=(table_name)
@table_name = table_name
end
|
Instance Method Details
#attribute_values ⇒ Object
102
103
104
105
106
|
# File 'lib/opal_orm/sql_object.rb', line 102
def attribute_values
self.class.columns.map do |col|
attributes[col]
end
end
|
#attributes ⇒ Object
98
99
100
|
# File 'lib/opal_orm/sql_object.rb', line 98
def attributes
@attributes ||= {}
end
|
#insert ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/opal_orm/sql_object.rb', line 26
def insert
columns = self.class.columns cols = columns.join(", ")
placeholders = (["?"] * columns.length).join(", ")
DBConnection.execute(<<-SQL,*attribute_values)
INSERT INTO
#{self.class.table_name}(#{cols})
VALUES
(#{placeholders})
SQL
attributes[:id] = DBConnection.last_insert_row_id
end
|
#save ⇒ Object
123
124
125
|
# File 'lib/opal_orm/sql_object.rb', line 123
def save
id.nil? ? insert : update
end
|
#update ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/opal_orm/sql_object.rb', line 108
def update
cols = self.class
.columns
.map { |col_name| "#{col_name} = ?"}
.join(", ")
DBConnection.execute(<<-SQL,*attribute_values - [:id], id)
UPDATE
#{self.class.table_name}
SET
#{cols}
WHERE
id = ?
SQL
end
|