Class: BrowSql::Record
- Inherits:
-
Object
show all
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations
- Defined in:
- app/model/brow_sql/record.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(params = nil, options = {}) ⇒ Record
Returns a new instance of Record.
23
24
25
26
27
|
# File 'app/model/brow_sql/record.rb', line 23
def initialize(params = nil, options = {})
params.each do |attr, value, type|
attributes.store(attr.to_s, type.type_cast(value))
end if params
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
53
54
55
56
57
58
59
60
61
|
# File 'app/model/brow_sql/record.rb', line 53
def method_missing(method, *args, &block)
if attributes.keys.include? method.to_s
attributes.fetch(method.to_s)
elsif attributes.keys.include? method.to_s.gsub("=", "")
attributes.store(method.to_s, *args)
else
super
end
end
|
Class Method Details
.all(table) ⇒ Object
42
43
44
45
46
|
# File 'app/model/brow_sql/record.rb', line 42
def all(table)
connection.execute("SELECT * FROM '#{table.name}'").map do |values|
self.new(values.each_with_index.map{|(k, v), i| [k, v, table.columns[i]] if table.columns[i] }.reject{|v| v.nil?})
end
end
|
Instance Method Details
#attributes ⇒ Object
29
30
31
|
# File 'app/model/brow_sql/record.rb', line 29
def attributes
@attributes ||= {}
end
|
#destroy(table) ⇒ Object
37
38
39
|
# File 'app/model/brow_sql/record.rb', line 37
def destroy(table)
connection.execute('DELETE FROM "?" WHERE "?"."id"=?', table.name, table.name, id)
end
|
#persisted? ⇒ Boolean
33
34
35
|
# File 'app/model/brow_sql/record.rb', line 33
def persisted?
false
end
|
#to_param ⇒ Object
49
50
51
|
# File 'app/model/brow_sql/record.rb', line 49
def to_param
id
end
|