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, HTTPMethods, Paths, Serialization, Validation
- Defined in:
- lib/proxima/model.rb
Class Method Summary
collapse
-
.api(api = nil) ⇒ Object
TODO: Implement callbacks extend ActiveModel::Callbacks define_model_callbacks :create, :update.
-
.count(query = {}, params = {}, opts = {}) ⇒ Object
-
.count_and_find(query = {}, params = {}, opts = {}) ⇒ Object
-
.create(record, params = {}, opts = {}) ⇒ Object
-
.find(query = {}, params = {}, opts = {}) ⇒ Object
-
.find_by_id(id, params = {}, opts = {}) ⇒ Object
-
.find_one(query = {}, params = {}, opts = {}) ⇒ Object
-
.response ⇒ Object
-
.responses ⇒ Object
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=, #eql?, #hash, included
included
Constructor Details
#initialize(record = {}) ⇒ Model
Returns a new instance of Model.
100
101
102
103
|
# File 'lib/proxima/model.rb', line 100
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
24
25
26
27
|
# File 'lib/proxima/model.rb', line 24
def self.api(api = nil)
@api = api if api
@api
end
|
.count(query = {}, params = {}, opts = {}) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/proxima/model.rb', line 78
def self.count(query = {}, params = {}, opts = {})
query['$limit'] = 0
opts[:query] = self.convert_query_or_delta_to_json query
@response = self.api.get self.find_path.call(params), opts
return nil unless @response.code == 200
total_count = @response.[:x_total_count]
total_count.present? ? total_count.to_i : 0
end
|
.count_and_find(query = {}, params = {}, opts = {}) ⇒ Object
66
67
68
69
70
71
|
# File 'lib/proxima/model.rb', line 66
def self.count_and_find(query = {}, params = {}, opts = {})
items = self.find query, params, opts
total_count = self.response.[:x_total_count].to_i || 0
Struct.new(:total_count, :items).new(total_count, items)
end
|
.create(record, params = {}, opts = {}) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/proxima/model.rb', line 37
def self.create(record, params = {}, opts = {})
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 params, opts
@response = model.response
return nil unless save_ok
model
end
|
.find(query = {}, params = {}, opts = {}) ⇒ Object
57
58
59
60
61
62
63
64
|
# File 'lib/proxima/model.rb', line 57
def self.find(query = {}, params = {}, opts = {})
opts[:query] = self.convert_query_or_delta_to_json query
@response = self.api.get self.find_path.call(query.merge(params)), opts
return [] unless @response.code == 200
self.from_json @response.body
end
|
.find_by_id(id, params = {}, opts = {}) ⇒ Object
90
91
92
93
94
95
96
97
98
|
# File 'lib/proxima/model.rb', line 90
def self.find_by_id(id, params = {}, opts = {})
raise "id cannot be blank" if id.blank?
params[:id] = id
@response = self.api.get self.find_by_id_path.call(params), opts
return nil unless @response.code == 200
self.from_json @response.body, single_model_from_array: true
end
|
.find_one(query = {}, params = {}, opts = {}) ⇒ Object
73
74
75
76
|
# File 'lib/proxima/model.rb', line 73
def self.find_one(query = {}, params = {}, opts = {})
query['$limit'] = 1
self.find(query, params, opts)[0]
end
|
.response ⇒ Object
29
30
31
|
# File 'lib/proxima/model.rb', line 29
def self.response
@response
end
|
.responses ⇒ Object
33
34
35
|
# File 'lib/proxima/model.rb', line 33
def self.responses
@responses || []
end
|
Instance Method Details
#destroy(params = {}) ⇒ Object
162
163
164
165
166
167
168
169
|
# File 'lib/proxima/model.rb', line 162
def destroy(params = {})
raise "Cannot destroy a new record" if new_record?
@response = self.class.api.delete(self.class.delete_by_id_path.call(self.to_h.merge(params)))
return false unless @response.code == 204
self.persisted = true
end
|
#new_record=(val) ⇒ Object
118
119
120
121
122
|
# File 'lib/proxima/model.rb', line 118
def new_record=(val)
@new_record = !!val
@persisted = !val
clear_changes_information unless val
end
|
#new_record? ⇒ Boolean
114
115
116
|
# File 'lib/proxima/model.rb', line 114
def new_record?
@new_record
end
|
#persisted=(val) ⇒ Object
109
110
111
112
|
# File 'lib/proxima/model.rb', line 109
def persisted=(val)
@persisted = !!val
changes_applied if val
end
|
#persisted? ⇒ Boolean
105
106
107
|
# File 'lib/proxima/model.rb', line 105
def persisted?
@persisted
end
|
#reload! ⇒ Object
154
155
156
|
# File 'lib/proxima/model.rb', line 154
def reload!
self.clear_changes_information
end
|
#response ⇒ Object
124
125
126
|
# File 'lib/proxima/model.rb', line 124
def response
@response
end
|
#restore(params = {}) ⇒ Object
171
172
173
174
175
176
177
178
|
# File 'lib/proxima/model.rb', line 171
def restore(params = {})
raise "Cannot restore a new record" if new_record?
@response = self.class.api.post(self.class.restore_by_id_path.call(self.to_h.merge(params)))
return false unless @response.code == 204
self.persisted = true
end
|
#rollback! ⇒ Object
158
159
160
|
# File 'lib/proxima/model.rb', line 158
def rollback!
self.restore_attributes
end
|
#save(params = {}, opts = {}) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/proxima/model.rb', line 128
def save(params = {}, opts = {})
return false unless self.valid?
if self.new_record?
path = self.class.create_path.call self.to_h.merge(params)
payload = { json: self.as_json(opts) }
@response = self.class.api.post path, payload
return false unless @response.code == 201
self.from_json @response.body, opts
self.new_record = false
return true
end
return true if self.persisted?
opts[:flatten] = true if opts[:flatten] == nil
path = self.class.update_by_id_path.call self.to_h.merge(params)
payload = { json: self.as_json(opts) }
@response = self.class.api.put path, payload
return false unless @response.code == 204
self.persisted = true
end
|