Class: Bartleby::Objectifier
Class Method Summary
collapse
Instance Method Summary
collapse
assoc_options, belongs_to, has_many, has_one_through
Methods included from Searchable
where
Constructor Details
#initialize(params = {}) ⇒ Objectifier
Returns a new instance of Objectifier.
66
67
68
69
70
71
|
# File 'lib/bartleby/objectifier.rb', line 66
def initialize(params = {})
params.each do |attr_name, value|
raise "unknown attribute '#{attr_name}'" unless valid_attribute? attr_name
send("#{attr_name}=".to_sym, value)
end
end
|
Class Method Details
.all ⇒ Object
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/bartleby/objectifier.rb', line 38
def self.all
results = Connection.execute(<<-SQL)
SELECT
*
FROM
'#{table_name}'
SQL
parse_all(results)
end
|
.columns ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/bartleby/objectifier.rb', line 11
def self.columns
return @columns.dup if @columns
@columns = Connection.execute2(<<-SQL).first.map(&:to_sym)
SELECT
*
FROM
'#{table_name}'
SQL
@columns.dup
end
|
.finalize! ⇒ Object
23
24
25
26
27
28
|
# File 'lib/bartleby/objectifier.rb', line 23
def self.finalize!
columns.each do |column|
create_getter!(column)
create_setter!(column)
end
end
|
.find(id) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/bartleby/objectifier.rb', line 53
def self.find(id)
result = Connection.execute(<<-SQL, id)
SELECT
*
FROM
'#{table_name}'
WHERE
id = ?
SQL
result.empty? ? nil : self.new(result.first)
end
|
.parse_all(results) ⇒ Object
49
50
51
|
# File 'lib/bartleby/objectifier.rb', line 49
def self.parse_all(results)
results.map { |result| self.new(result) }
end
|
.table_name ⇒ Object
34
35
36
|
# File 'lib/bartleby/objectifier.rb', line 34
def self.table_name
@table_name || self.name.tableize
end
|
.table_name=(table_name) ⇒ Object
30
31
32
|
# File 'lib/bartleby/objectifier.rb', line 30
def self.table_name=(table_name)
@table_name = table_name
end
|
Instance Method Details
#attribute_values ⇒ Object
77
78
79
|
# File 'lib/bartleby/objectifier.rb', line 77
def attribute_values
self.class.columns.map { |col| send(col) }
end
|
#attributes ⇒ Object
73
74
75
|
# File 'lib/bartleby/objectifier.rb', line 73
def attributes
@attributes ||= {}
end
|
#insert ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/bartleby/objectifier.rb', line 81
def insert
cols = self.class.columns
cols.delete(:id)
col_names = cols.join(", ")
question_marks = (["?"] * (self.class.columns.count - 1)).join(", ")
id_idx = self.class.columns.index(:id)
values = attribute_values
values.delete_at(id_idx)
query_string = <<-SQL
INSERT INTO
#{self.class.table_name} (#{col_names})
VALUES
(#{question_marks})
SQL
Connection.execute(query_string, *values)
self.id = Connection.last_insert_row_id
end
|
#save ⇒ Object
118
119
120
|
# File 'lib/bartleby/objectifier.rb', line 118
def save
id.nil? ? insert : update
end
|
#update ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/bartleby/objectifier.rb', line 101
def update
cols = self.class.columns
cols.delete(:id)
set_string = cols.map { |col| "#{col} = :#{col}"}.join(", ")
query_string = <<-SQL
UPDATE
#{self.class.table_name}
SET
#{set_string}
WHERE
id = :id
SQL
Connection.execute(query_string, attributes)
end
|