Class: LarisrecordBase
Class Method Summary
collapse
Instance Method Summary
collapse
assoc_options, belongs_to, has_many, has_one_through
Methods included from Searchable
all, find, find_by, find_by_sql, first, last, method_missing, parse_all
Constructor Details
Returns a new instance of LarisrecordBase.
37
38
39
40
41
42
43
44
45
|
# File 'lib/laris/larisrecord/larisrecord_base.rb', line 37
def initialize(params = {})
params.each do |attr_name, value|
if self.class.columns.include?(attr_name.to_sym)
send("#{attr_name}=", value)
else
raise "unknown attribute '#{attr_name}'"
end
end
end
|
Class Method Details
2
3
4
5
6
|
# File 'lib/laris/larisrecord/larisrecord_base.rb', line 2
def self.columns
return @columns if @columns
@columns = DBConnection.columns(table_name)
end
|
.destroy(row) ⇒ Object
12
13
14
15
|
# File 'lib/laris/larisrecord/larisrecord_base.rb', line 12
def self.destroy(row)
row = find(row) if row.is_a?(Integer)
row.destroy
end
|
.destroy_all ⇒ Object
8
9
10
|
# File 'lib/laris/larisrecord/larisrecord_base.rb', line 8
def self.destroy_all
all.each { |row| row.destroy }
end
|
.laris_finalize! ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/laris/larisrecord/larisrecord_base.rb', line 17
def self.laris_finalize!
columns.each do |attr_name|
define_method(attr_name) do
attributes[attr_name]
end
define_method("#{attr_name}=") do |value|
attributes[attr_name] = value
end
end
end
|
.table_name ⇒ Object
33
34
35
|
# File 'lib/laris/larisrecord/larisrecord_base.rb', line 33
def self.table_name
@table_name ||= self.to_s.tableize
end
|
.table_name=(table_name) ⇒ Object
29
30
31
|
# File 'lib/laris/larisrecord/larisrecord_base.rb', line 29
def self.table_name=(table_name)
@table_name = table_name
end
|
Instance Method Details
#attribute_values ⇒ Object
51
52
53
|
# File 'lib/laris/larisrecord/larisrecord_base.rb', line 51
def attribute_values
columns.map { |attr_name| send(attr_name) }
end
|
#attributes ⇒ Object
47
48
49
|
# File 'lib/laris/larisrecord/larisrecord_base.rb', line 47
def attributes
@attributes ||= {}
end
|
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/laris/larisrecord/larisrecord_base.rb', line 55
def destroy
DBConnection.execute(<<-SQL, [id])
DELETE FROM
#{table_name}
WHERE
#{table_name}.id = ?
SQL
self
end
|
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/laris/larisrecord/larisrecord_base.rb', line 66
def insert
cols = columns.reject { |col| col == :id }
col_values = cols.map { |attr_name| send(attr_name) }
col_names = cols.join(", ")
question_marks = (["?"] * cols.size).join(", ")
result = DBConnection.execute(<<-SQL, col_values)
INSERT INTO
#{table_name} (#{col_names})
VALUES
(#{question_marks})
RETURNING id
SQL
self.id = result.first['id']
true
end
|
86
87
88
|
# File 'lib/laris/larisrecord/larisrecord_base.rb', line 86
def save
id ? update : insert
end
|
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/laris/larisrecord/larisrecord_base.rb', line 90
def update
set_sql = columns.map { |attr_name| "#{attr_name} = ?" }.join(", ")
result = DBConnection.execute(<<-SQL, attribute_values << id)
UPDATE
#{table_name}
SET
#{set_sql}
WHERE
#{table_name}.id = ?
SQL
true
end
|