Class: Domain::Model
- Inherits:
-
Object
show all
- Defined in:
- lib/domain/model.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Model
Returns a new instance of Model.
7
8
9
10
11
12
|
# File 'lib/domain/model.rb', line 7
def initialize(attributes = {})
attributes.each do |k, v|
key = "#{k}="
send(key, v) if respond_to?(key)
end
end
|
Class Method Details
.columns ⇒ Object
Ensure the columns method retrieves only valid attribute names
31
32
33
34
35
36
|
# File 'lib/domain/model.rb', line 31
def self.columns
instance_methods.select { |m| m.to_s.end_with?('=') }
.map { |setter| setter.to_s.chomp('=') }
.select { |attr| instance_methods.include?(attr.to_sym) && !Object.instance_methods.include?(attr.to_sym) }
.sort
end
|
.from_json(data) ⇒ Object
46
47
48
|
# File 'lib/domain/model.rb', line 46
def self.from_json(data)
new(data.transform_keys(&:to_sym)) end
|
38
39
40
|
# File 'lib/domain/model.rb', line 38
def self.
columns.join ','
end
|
.primary_key ⇒ Object
26
27
28
|
# File 'lib/domain/model.rb', line 26
def self.primary_key
['id']
end
|
.table_name ⇒ Object
42
43
44
|
# File 'lib/domain/model.rb', line 42
def self.table_name
name.underscore
end
|
Instance Method Details
#to_csv ⇒ Object
21
22
23
24
|
# File 'lib/domain/model.rb', line 21
def to_csv
self.class.columns
.map { |column| send(column.to_sym) }
end
|
#to_hash ⇒ Object
Convert instance attributes to a hash
15
16
17
18
19
|
# File 'lib/domain/model.rb', line 15
def to_hash
self.class.columns.each_with_object({}) do |attr, hash|
hash[attr] = send(attr.to_sym)
end
end
|
#to_s ⇒ Object
50
51
52
|
# File 'lib/domain/model.rb', line 50
def to_s
self.class.columns.map { |attr| send(attr) }.join(',')
end
|