Class: WORMY::Base
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Searchable
where
association_options, belongs_to, has_many, has_many_through, has_one_through
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
99
100
101
102
103
104
|
# File 'lib/wormy.rb', line 99
def initialize(params = {})
params.each do |key, value|
raise "unknown attribute '#{key}'" unless self.class.columns.include?(key.to_sym)
send("#{key}=", value)
end
end
|
Class Method Details
.all ⇒ Object
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/wormy.rb', line 11
def self.all
results = DBConnection.execute(<<-SQL)
SELECT
*
FROM
#{table_name}
SQL
parse_all(results)
end
|
.columns ⇒ Object
22
23
24
25
26
27
28
29
30
|
# File 'lib/wormy.rb', line 22
def self.columns
@columns ||= DBConnection.execute2(<<-SQL)
SELECT
*
FROM
#{self.table_name}
SQL
.first.map(&:to_sym)
end
|
.count(params) ⇒ Object
32
33
34
|
# File 'lib/wormy.rb', line 32
def self.count(params)
where(params).count
end
|
.destroy_all(params) ⇒ Object
36
37
38
|
# File 'lib/wormy.rb', line 36
def self.destroy_all(params)
self.where(params).each(&:destroy)
end
|
.finalize! ⇒ Object
40
41
42
43
44
45
46
|
# File 'lib/wormy.rb', line 40
def self.finalize!
columns.each do |col|
define_method(col) { attributes[col] }
define_method("#{col}=") { |new_attr| attributes[col] = new_attr }
end
end
|
.find(id) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/wormy.rb', line 48
def self.find(id)
result = DBConnection.execute(<<-SQL, id)
SELECT
*
FROM
#{table_name}
WHERE
#{table_name}.id = ?
SQL
parse_all(result).first
end
|
.first ⇒ Object
61
62
63
|
# File 'lib/wormy.rb', line 61
def self.first
all.first
end
|
.last ⇒ Object
65
66
67
|
# File 'lib/wormy.rb', line 65
def self.last
all.last
end
|
.parse_all(results) ⇒ Object
69
70
71
72
|
# File 'lib/wormy.rb', line 69
def self.parse_all(results)
results.map { |options| self.new(options) }
end
|
.table_name ⇒ Object
74
75
76
|
# File 'lib/wormy.rb', line 74
def self.table_name
@table_name ||= self.to_s.tableize
end
|
.table_name=(table_name) ⇒ Object
78
79
80
|
# File 'lib/wormy.rb', line 78
def self.table_name=(table_name)
@table_name = table_name
end
|
.validates(*methods) ⇒ Object
82
83
84
|
# File 'lib/wormy.rb', line 82
def self.validates(*methods)
@validations = methods
end
|
.validations ⇒ Object
86
87
88
|
# File 'lib/wormy.rb', line 86
def self.validations
@validations ||= []
end
|
Instance Method Details
#attribute_values ⇒ Object
94
95
96
97
|
# File 'lib/wormy.rb', line 94
def attribute_values
self.class.columns.map { |col| self.send(col) }
end
|
#attributes ⇒ Object
90
91
92
|
# File 'lib/wormy.rb', line 90
def attributes
@attributes ||= {}
end
|
#destroy ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/wormy.rb', line 106
def destroy
if self.class.find(id)
DBConnection.execute(<<-SQL)
DELETE
FROM
#{self.class.table_name}
WHERE
id = #{id}
SQL
self
end
end
|
#insert ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/wormy.rb', line 120
def insert
col_names = self.class.columns.join(", ")
question_marks = ["?"] * self.class.columns.count
DBConnection.execute(<<-SQL, *attribute_values)
INSERT INTO
#{self.class.table_name} (#{col_names})
VALUES
(#{question_marks.join(',')})
SQL
self.id = DBConnection.last_insert_row_id
end
|
#save ⇒ Object
134
135
136
137
138
139
140
141
|
# File 'lib/wormy.rb', line 134
def save
self.class.validations.each do |method|
raise "Validation error" unless self.send(method)
end
id ? update : insert
end
|
#update ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/wormy.rb', line 143
def update
set = self.class.columns.map { |col_name| "#{col_name} = ?" }.drop(1).join(", ")
DBConnection.execute(<<-SQL, *attribute_values.rotate)
UPDATE
#{self.class.table_name}
SET
#{set}
WHERE
id = ?
SQL
end
|
#valid? ⇒ Boolean
156
157
158
159
160
161
162
|
# File 'lib/wormy.rb', line 156
def valid?
self.class.validations.each do |method|
return false unless self.send(method)
end
true
end
|