Class: Puffs::SQLObject
Overview
Base Model class for Puffs Orm.
Constant Summary
Constants included
from Searchable
Searchable::RELATION_METHODS
Class Method Summary
collapse
Instance Method Summary
collapse
assoc_options, belongs_to, has_many, has_many_through, has_one_through
Methods included from Searchable
all, count, find, first, last
Constructor Details
#initialize(params = {}) ⇒ SQLObject
Returns a new instance of SQLObject.
59
60
61
62
63
64
65
66
67
|
# File 'lib/sql_object/sql_object.rb', line 59
def initialize(params = {})
params.each do |attr_name, value|
unless self.class.columns.include?(attr_name.to_sym)
raise "unknown attribute '#{attr_name}'"
end
send("#{attr_name}=", value)
end
end
|
Class Method Details
.define_singleton_method_by_proc(obj, name, block) ⇒ Object
17
18
19
20
|
# File 'lib/sql_object/sql_object.rb', line 17
def self.define_singleton_method_by_proc(obj, name, block)
metaclass = class << obj; self; end
metaclass.send(:define_method, name, block)
end
|
.destroy_all! ⇒ Object
22
23
24
|
# File 'lib/sql_object/sql_object.rb', line 22
def self.destroy_all!
all.each(&:destroy!)
end
|
.finalize! ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/sql_object/sql_object.rb', line 26
def self.finalize!
columns.each do |column|
define_method(column) do
attributes[column]
end
define_method("#{column}=") do |new_value|
attributes[column] = new_value
end
end
end
|
.has_association?(association) ⇒ Boolean
38
39
40
|
# File 'lib/sql_object/sql_object.rb', line 38
def self.has_association?(association)
assoc_options.keys.include?(association)
end
|
.parse_all(results) ⇒ Object
42
43
44
45
46
47
48
49
|
# File 'lib/sql_object/sql_object.rb', line 42
def self.parse_all(results)
relation = Puffs::SQLRelation.new(klass: self, loaded: true)
results.each do |result|
relation << new(result)
end
relation
end
|
.table_name ⇒ Object
55
56
57
|
# File 'lib/sql_object/sql_object.rb', line 55
def self.table_name
@table_name ||= to_s.downcase.tableize
end
|
.table_name=(table_name) ⇒ Object
51
52
53
|
# File 'lib/sql_object/sql_object.rb', line 51
def self.table_name=(table_name)
@table_name = table_name
end
|
Instance Method Details
#attribute_values ⇒ Object
73
74
75
76
77
|
# File 'lib/sql_object/sql_object.rb', line 73
def attribute_values
self.class.columns.map do |column|
send(column)
end
end
|
#attributes ⇒ Object
69
70
71
|
# File 'lib/sql_object/sql_object.rb', line 69
def attributes
@attributes ||= {}
end
|
#destroy! ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/sql_object/sql_object.rb', line 79
def destroy!
if self.class.find(id)
Puffs::DBConnection.execute(<<-SQL)
DELETE
FROM
#{self.class.table_name}
WHERE
id = #{id}
SQL
return self
end
end
|
#insert ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/sql_object/sql_object.rb', line 92
def insert
columns = self.class.columns.reject { |col| col == :id }
column_values = columns.map { |attr_name| send(attr_name) }
column_names = columns.join(', ')
bind_params = (1..columns.length).map { |n| "$#{n}" }.join(', ')
result = Puffs::DBConnection.execute(<<-SQL, column_values)
INSERT INTO
#{self.class.table_name} (#{column_names})
VALUES
(#{bind_params})
RETURNING id;
SQL
self.id = result.first['id']
self
end
|
#save ⇒ Object
108
109
110
|
# File 'lib/sql_object/sql_object.rb', line 108
def save
self.class.find(id) ? update : insert
end
|
#update ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/sql_object/sql_object.rb', line 112
def update
set_line = self.class.columns.map do |column|
"#{column} = \'#{send(column)}\'"
end.join(', ')
Puffs::DBConnection.execute(<<-SQL)
UPDATE
#{self.class.table_name}
SET
#{set_line}
WHERE
id = #{id}
SQL
self
end
|