Class: Proxima::Model
- Inherits:
-
Object
show all
- Extended by:
- ActiveModel::Naming, ActiveModel::Translation
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Dirty, ActiveModel::Model, ActiveModel::Serializers::JSON, ActiveModel::Validations, Attributes, Paths, Serialization, Validation
- Defined in:
- lib/proxima/model.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Validation
errors, included, #read_attribute_for_validation
#as_json, #from_json, included, #to_h
Methods included from Paths
included
Methods included from Attributes
#attributes, #attributes=, included
Constructor Details
#initialize(record = {}) ⇒ Model
Returns a new instance of Model.
107
108
109
110
|
# File 'lib/proxima/model.rb', line 107
def initialize(record = {})
self.new_record = true
self.attributes = record
end
|
Class Method Details
.api(api = nil) ⇒ Object
TODO: Implement callbacks extend ActiveModel::Callbacks define_model_callbacks :create, :update
30
31
32
33
|
# File 'lib/proxima/model.rb', line 30
def self.api(api = nil)
@api = api if api
@api
end
|
.count(query = {}, opts = {}) ⇒ Object
80
81
82
83
84
85
86
87
88
|
# File 'lib/proxima/model.rb', line 80
def self.count(query = {}, opts = {})
query['$limit'] = 0
opts[:query] = self.convert_query_or_delta_to_json query
@response = self.api.get self.find_path.call(query), opts
return nil unless @response.code == 200
@response.[:x_total_count] || 0
end
|
.create(record) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/proxima/model.rb', line 43
def self.create(record)
if record.is_a? Array
models = []
@responses = []
record.each do |record|
model = self.create record
@responses.push(model.response)
models.push(model) if model
end
return models
end
model = self.new(record)
save_ok = model.save
@response = model.response
return nil unless save_ok
model
end
|
.find(query = {}, opts = {}) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/proxima/model.rb', line 63
def self.find(query = {}, opts = {})
opts[:query] = self.convert_query_or_delta_to_json query
@response = self.api.get self.find_path.call(query), opts
if @response.code != 200
return []
end
models = self.from_json @response.body
models.each { |model| model.new_record = false }
end
|
.find_by_id(id, query = {}, opts = {}) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/proxima/model.rb', line 90
def self.find_by_id(id, query = {}, opts = {})
if opts == nil && query
opts = query
query = {}
end
query[:id] = id
@response = self.api.get self.find_by_id_path.call(query), opts
return nil unless @response.code == 200
model = self.new
model.from_json @response.body
model.new_record = false
model
end
|
.find_one(query = {}, opts = {}) ⇒ Object
75
76
77
78
|
# File 'lib/proxima/model.rb', line 75
def self.find_one(query = {}, opts = {})
query['$limit'] = 1
self.find(query, opts)[0]
end
|
.response ⇒ Object
35
36
37
|
# File 'lib/proxima/model.rb', line 35
def self.response
@response
end
|
.responses ⇒ Object
39
40
41
|
# File 'lib/proxima/model.rb', line 39
def self.responses
@responses || []
end
|
Instance Method Details
#destroy ⇒ Object
169
170
171
172
173
174
175
176
|
# File 'lib/proxima/model.rb', line 169
def destroy
return false if new_record?
@response = self.class.api.delete(self.class.delete_by_id_path.call(self.to_h))
return false unless @response.code == 204
self.persisted = true
end
|
#new_record=(val) ⇒ Object
125
126
127
128
129
|
# File 'lib/proxima/model.rb', line 125
def new_record=(val)
@new_record = !!val
@persisted = !val
clear_changes_information unless val
end
|
#new_record? ⇒ Boolean
121
122
123
|
# File 'lib/proxima/model.rb', line 121
def new_record?
@new_record
end
|
#persisted=(val) ⇒ Object
116
117
118
119
|
# File 'lib/proxima/model.rb', line 116
def persisted=(val)
@persisted = !!val
changes_applied if val
end
|
#persisted? ⇒ Boolean
112
113
114
|
# File 'lib/proxima/model.rb', line 112
def persisted?
@persisted
end
|
#reload! ⇒ Object
161
162
163
|
# File 'lib/proxima/model.rb', line 161
def reload!
self.clear_changes_information
end
|
#response ⇒ Object
131
132
133
|
# File 'lib/proxima/model.rb', line 131
def response
@response
end
|
#restore ⇒ Object
178
179
180
181
182
183
184
185
|
# File 'lib/proxima/model.rb', line 178
def restore
return false if new_record?
@response = self.class.api.post(self.class.restore_by_id_path.call(self.to_h))
return false unless @response.code == 204
self.persisted = true
end
|
#rollback! ⇒ Object
165
166
167
|
# File 'lib/proxima/model.rb', line 165
def rollback!
self.restore_attributes
end
|
#save(options = {}) ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/proxima/model.rb', line 135
def save(options = {})
return false unless self.valid?
if self.new_record?
path = self.class.create_path.call self.to_h
payload = { json: self.as_json(options) }
@response = self.class.api.post path, payload
return false unless @response.code == 201
self.from_json @response.body, options[:include_root]
self.new_record = false
return true
end
return true if self.persisted?
options[:flatten] = true if options[:flatten] == nil
path = self.class.update_by_id_path.call self.to_h
payload = { json: self.as_json(options) }
@response = self.class.api.put path, payload
return false unless @response.code == 204
self.persisted = true
end
|