Module: Neuron::Client::Base
Defined Under Namespace
Modules: ClassAndInstanceMethods
Class Method Summary
collapse
Instance Method Summary
collapse
#all, #api=, #create, #create!, #find, #validate=
Class Method Details
.included(base) ⇒ Object
176
177
178
179
180
|
# File 'lib/neuron-client/model/base.rb', line 176
def self.included(base)
base.send(:attr_accessor, :errors) if base.is_a?(Class)
base.send(:attr_reader, :id) if base.is_a?(Class)
base.extend(ClassAndInstanceMethods)
end
|
Instance Method Details
#apply_attributes!(attrs) ⇒ Object
188
189
190
191
192
193
194
|
# File 'lib/neuron-client/model/base.rb', line 188
def apply_attributes!(attrs)
if attrs.present?
attrs.each do |k,v|
self.send("#{k}=", v) if self.respond_to?("#{k}=")
end
end
end
|
252
253
254
255
|
# File 'lib/neuron-client/model/base.rb', line 252
def destroy
connected_to_admin!
connection.delete("#{resources_name}/#{id}")
end
|
220
221
222
|
# File 'lib/neuron-client/model/base.rb', line 220
def id=(id)
@id = Integer(id)
end
|
#initialize(attrs = {}) ⇒ Object
184
185
186
|
# File 'lib/neuron-client/model/base.rb', line 184
def initialize(attrs={})
apply_attributes!(attrs)
end
|
#new_record? ⇒ Boolean
216
217
218
|
# File 'lib/neuron-client/model/base.rb', line 216
def new_record?
id.nil?
end
|
224
225
226
|
# File 'lib/neuron-client/model/base.rb', line 224
def save
update_attributes
end
|
#to_create_hash(*except) ⇒ Object
208
209
210
|
# File 'lib/neuron-client/model/base.rb', line 208
def to_create_hash(*except)
to_hash(*([:errors, :updated_at, :created_at, :id] + except))
end
|
#to_hash(*except) ⇒ Object
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/neuron-client/model/base.rb', line 196
def to_hash(*except)
hash = {}
except = except.map(&:to_sym)
attributes.each do |attribute|
unless except.include?(attribute.to_sym)
value = send(attribute)
hash[attribute.to_s] = value
end
end
{resource_name => hash}
end
|
#to_update_hash(*except) ⇒ Object
212
213
214
|
# File 'lib/neuron-client/model/base.rb', line 212
def to_update_hash(*except)
to_hash(*([:errors, :updated_at, :created_at] + except))
end
|
#update_attributes(attrs = {}) ⇒ Object
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
# File 'lib/neuron-client/model/base.rb', line 228
def update_attributes(attrs={})
connected_to_admin!
@errors = catch :errors do
apply_attributes!(attrs) if attrs.present?
data = {}
if new_record?
data = to_create_hash
validate_against_schema!(:create, data)
data = connection.post(resources_name, data)
else
data = to_update_hash
validate_against_schema!(:update, data)
data = connection.put("#{resources_name}/#{id}", data)
end
apply_attributes!(data[resource_name]) unless data.nil?
[]
end
@errors.empty?
end
|
#valid? ⇒ Boolean
248
249
250
|
# File 'lib/neuron-client/model/base.rb', line 248
def valid?
@errors.empty?
end
|