Class: WhalesORM::Base
Class Method Summary
collapse
Instance Method Summary
collapse
includes, where
assoc_options, belongs_to, has_many, has_one_through
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
101
102
103
104
105
106
107
|
# File 'lib/base.rb', line 101
def initialize(params = {})
params.each do |k, v|
attr_name = k.to_sym
send("#{attr_name}=", v)
end
end
|
Class Method Details
.all ⇒ Object
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/base.rb', line 73
def self.all
results = DBConnection.execute(" SELECT\n \#{table_name}.*\n FROM\n \#{table_name}\n SQL\n\n parse_all(results)\nend\n")
|
.columns ⇒ Object
28
29
30
|
# File 'lib/base.rb', line 28
def self.columns
@columns ||= DBConnection.execute2("SELECT * FROM #{table_name} LIMIT 1")[0].map(&:to_sym)
end
|
.destroy_all(conditions = {}) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/base.rb', line 45
def self.destroy_all(conditions = {})
if conditions.empty?
deleted = self.all
where_line = nil
else
deleted = self.where(conditions)
where_line = conditions.keys.map do |key|
"#{ key } = :#{ key }"
end.join(" AND ").insert(0, "WHERE ")
end
DBConnection.execute(" DELETE FROM\n \#{ self.table_name }\n \#{ where_line }\n SQL\n\n deleted\nend\n", conditions)
|
.finalize! ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/base.rb', line 32
def self.finalize!
columns.each do |col|
define_method(col) do
attributes[col]
end
define_method("#{col}=".to_sym) do |value|
attributes[col] = value
end
end
end
|
.find(id) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/base.rb', line 88
def self.find(id)
result = DBConnection.instance.get_first_row(" SELECT\n \#{table_name}.*\n FROM\n \#{table_name}\n WHERE\n id = :id\n SQL\n\n result ? self.new(result) : nil\nend\n", id: id)
|
.method_missing(method, *args) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/base.rb', line 14
def self.method_missing(method, *args)
method = method.to_s
if method.start_with?("find_by")
columns = method[8..-1].split("_and_")
opts = {}
columns.each_with_index do |col, i|
opts[col] = args[i]
end
self.where(opts)
else
super
end
end
|
.parse_all(results) ⇒ Object
84
85
86
|
# File 'lib/base.rb', line 84
def self.parse_all(results)
results.map { |result| self.new(result) }
end
|
.table_name ⇒ Object
69
70
71
|
# File 'lib/base.rb', line 69
def self.table_name
@table_name ||= self.to_s.tableize
end
|
.table_name=(table_name) ⇒ Object
65
66
67
|
# File 'lib/base.rb', line 65
def self.table_name=(table_name)
@table_name = table_name
end
|
Instance Method Details
#attribute_values ⇒ Object
113
114
115
|
# File 'lib/base.rb', line 113
def attribute_values
self.class.columns.map { |attr_name| send(attr_name) }
end
|
#attributes ⇒ Object
109
110
111
|
# File 'lib/base.rb', line 109
def attributes
@attributes ||= {}
end
|
#destroy ⇒ Object
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/base.rb', line 117
def destroy
DBConnection.execute(" DELETE FROM\n \#{self.class.table_name}\n WHERE\n id = ?\n SQL\n\n self\nend\n", id)
|
#insert ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/base.rb', line 128
def insert
col_names = self.class.columns.drop(1).join(',')
question_marks = (["?"] * (self.class.columns.length - 1)).join(',')
DBConnection.execute(" INSERT INTO\n \#{self.class.table_name} (\#{col_names})\n VALUES\n (\#{question_marks});\n SQL\n\n self.id = DBConnection.last_insert_row_id\n self\nend\n", *(attribute_values.drop(1)))
|
#save ⇒ Object
143
144
145
|
# File 'lib/base.rb', line 143
def save
id.nil? ? insert : update
end
|
#update ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/base.rb', line 147
def update
set_line = self.class.columns.map { |attr_name| "#{attr_name} = ?" }
set_line = set_line.join(', ')
DBConnection.execute(" UPDATE\n \#{self.class.table_name}\n SET\n \#{set_line}\n WHERE\n id = ?\n SQL\n\n self\nend\n", *attribute_values, id)
|