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) ⇒ 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.
97
98
99
100
|
# File 'lib/proxima/model.rb', line 97
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
|
# 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
@response.[:x_total_count] || 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) ⇒ 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)
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 = {}, 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(params.merge(query)), opts
return [] unless @response.code == 200
self.from_json @response.body
end
|
.find_by_id(id, params = {}, opts = {}) ⇒ Object
88
89
90
91
92
93
94
95
|
# File 'lib/proxima/model.rb', line 88
def self.find_by_id(id, params = {}, opts = {})
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 ⇒ Object
159
160
161
162
163
164
165
166
|
# File 'lib/proxima/model.rb', line 159
def destroy
raise "Cannot destroy a new record" 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
115
116
117
118
119
|
# File 'lib/proxima/model.rb', line 115
def new_record=(val)
@new_record = !!val
@persisted = !val
clear_changes_information unless val
end
|
#new_record? ⇒ Boolean
111
112
113
|
# File 'lib/proxima/model.rb', line 111
def new_record?
@new_record
end
|
#persisted=(val) ⇒ Object
106
107
108
109
|
# File 'lib/proxima/model.rb', line 106
def persisted=(val)
@persisted = !!val
changes_applied if val
end
|
#persisted? ⇒ Boolean
102
103
104
|
# File 'lib/proxima/model.rb', line 102
def persisted?
@persisted
end
|
#reload! ⇒ Object
151
152
153
|
# File 'lib/proxima/model.rb', line 151
def reload!
self.clear_changes_information
end
|
#response ⇒ Object
121
122
123
|
# File 'lib/proxima/model.rb', line 121
def response
@response
end
|
#restore ⇒ Object
168
169
170
171
172
173
174
175
|
# File 'lib/proxima/model.rb', line 168
def restore
raise "Cannot restore a new record" 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
155
156
157
|
# File 'lib/proxima/model.rb', line 155
def rollback!
self.restore_attributes
end
|
#save(options = {}, params = {}) ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/proxima/model.rb', line 125
def save(options = {}, params = {})
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 params.merge(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
|