Class: Infuser::Models::Base
- Inherits:
-
Object
- Object
- Infuser::Models::Base
show all
- Includes:
- Helpers::Hashie
- Defined in:
- lib/infuser/models/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#camelize_hash, #safe_classify
Constructor Details
#initialize(data = {}) ⇒ Base
Returns a new instance of Base.
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/infuser/models/base.rb', line 82
def initialize data = {}
data.each do |key, value|
send("#{key}=", value)
end
@collections = self.class.collection_names.each_with_object({}) do |name, hash|
hash[name] = Infuser::Collections::Proxy.new(name)
end
@children = self.class.children_names.each_with_object({}) do |name, hash|
hash[name] = Infuser::Collections::ChildProxy.new(self, name)
end
return self
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
80
81
82
|
# File 'lib/infuser/models/base.rb', line 80
def id
@id
end
|
Class Method Details
.belongs_to(type) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/infuser/models/base.rb', line 45
def belongs_to type
type = type.to_s.underscore
define_method "#{type}=" do |arg|
update_association(type, arg)
end
define_method "clear_#{type}" do
clear_association(type)
end
define_method "#{type}" do
fetch_association(type)
end
end
|
.children_names ⇒ Object
16
17
18
|
# File 'lib/infuser/models/base.rb', line 16
def children_names
@children_names || []
end
|
.collection_names ⇒ Object
12
13
14
|
# File 'lib/infuser/models/base.rb', line 12
def collection_names
@collection_names || []
end
|
.define_schema(*cols) ⇒ Object
20
21
22
23
|
# File 'lib/infuser/models/base.rb', line 20
def define_schema *cols
@schema = *cols
attr_accessor *cols
end
|
.fieldset ⇒ Object
61
62
63
64
65
66
67
68
|
# File 'lib/infuser/models/base.rb', line 61
def fieldset
core = schema.dup
coll = collection_names.map { |name| Infuser.const_get(name.classify).schema.dup }
(core + coll).flatten.compact.map do |f|
f.to_s.split('_').map { |w| w[0] = w[0].upcase; w }.join
end
end
|
.has_collection(type) ⇒ Object
25
26
27
28
29
30
31
32
33
|
# File 'lib/infuser/models/base.rb', line 25
def has_collection type
type = type.to_s.underscore
@collection_names ||= []
@collection_names << type
define_method type.to_s.underscore do
collections[type]
end
end
|
.has_many(type) ⇒ Object
35
36
37
38
39
40
41
42
43
|
# File 'lib/infuser/models/base.rb', line 35
def has_many type
type = type.to_s.underscore
@children_names ||= []
@children_names << type
define_method type.to_s.underscore do
children[type]
end
end
|
.klass_name ⇒ Object
70
71
72
|
# File 'lib/infuser/models/base.rb', line 70
def klass_name
name.split('::').last
end
|
.schema ⇒ Object
8
9
10
|
# File 'lib/infuser/models/base.rb', line 8
def schema
@schema || []
end
|
.service_name ⇒ Object
74
75
76
|
# File 'lib/infuser/models/base.rb', line 74
def service_name
"#{klass_name.underscore}_service".classify
end
|
Instance Method Details
#attributes ⇒ Object
102
103
104
105
106
107
108
|
# File 'lib/infuser/models/base.rb', line 102
def attributes
data = self.class.schema.dup.each_with_object({}) { |key, hash| hash[key] = send(key) }
collections.each do |name, proxy|
data[name] = proxy.map(&:attributes)
end
data
end
|
#data ⇒ Object
110
111
112
113
114
115
116
|
# File 'lib/infuser/models/base.rb', line 110
def data
data = self.class.schema.dup.each_with_object({}) { |key, hash| hash[key] = send(key) }
collections.each do |name, proxy|
data.merge!(proxy.data)
end
camelize_hash(data).select { |k, v| !v.nil? }
end
|
#destroy ⇒ Object
131
132
133
|
# File 'lib/infuser/models/base.rb', line 131
def destroy
client.get("DataService.delete", klass_name, id)
end
|
#fieldset ⇒ Object
151
152
153
|
# File 'lib/infuser/models/base.rb', line 151
def fieldset
self.class.fieldset
end
|
#inspect ⇒ Object
98
99
100
|
# File 'lib/infuser/models/base.rb', line 98
def inspect
"#<#{self.class.name} #{attributes.map { |k, v| "#{k}: #{v || 'nil'}" }.join(', ')}>"
end
|
#klass_name ⇒ Object
155
156
157
|
# File 'lib/infuser/models/base.rb', line 155
def klass_name
self.class.klass_name
end
|
#load ⇒ Object
135
136
137
|
# File 'lib/infuser/models/base.rb', line 135
def load
populate client.get("DataService.load", klass_name, id, fieldset)
end
|
#new_record? ⇒ Boolean
118
119
120
|
# File 'lib/infuser/models/base.rb', line 118
def new_record?
id.nil?
end
|
#persisted? ⇒ Boolean
122
123
124
|
# File 'lib/infuser/models/base.rb', line 122
def persisted?
!new_record?
end
|
#populate(hash) ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/infuser/models/base.rb', line 139
def populate hash
hash.each do |key, value|
send("#{key.underscore}=", value) if respond_to?(key.underscore.to_sym)
end
collections.each do |name, proxy|
proxy.parse(hash)
end
return self
end
|
#save ⇒ Object
126
127
128
129
|
# File 'lib/infuser/models/base.rb', line 126
def save
new_record? ? add : update
return self
end
|
#service_name ⇒ Object
159
160
161
|
# File 'lib/infuser/models/base.rb', line 159
def service_name
self.class.service_name
end
|