Module: Protip::Resource
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations
- Defined in:
- lib/protip/resource.rb
Defined Under Namespace
Modules: ClassMethods, Creatable, Destroyable, ExtraMethods, SearchMethods, Updatable
Instance Method Summary
collapse
Instance Method Details
#attributes ⇒ Object
275
276
277
278
279
280
281
|
# File 'lib/protip/resource.rb', line 275
def attributes
self.class.message.all_fields.map{|field| field.name}.inject({}) do |hash, attribute_name|
hash[attribute_name] = message.field?(attribute_name) ? public_send(attribute_name).as_json : nil
hash
end
end
|
#errors ⇒ Object
283
284
285
|
# File 'lib/protip/resource.rb', line 283
def errors
@errors ||= ActiveModel::Errors.new(self)
end
|
#initialize(message_or_attributes = {}) ⇒ Object
232
233
234
235
236
237
238
239
240
241
242
243
244
|
# File 'lib/protip/resource.rb', line 232
def initialize(message_or_attributes = {})
if self.class.message == nil
raise RuntimeError.new('Must define a message class using `resource`')
end
if message_or_attributes.is_a?(self.class.message)
self.message = message_or_attributes
else
self.message = self.class.message.new
assign_attributes message_or_attributes
end
super()
end
|
#message=(message) ⇒ Object
246
247
248
|
# File 'lib/protip/resource.rb', line 246
def message=(message)
@wrapper = Protip::Wrapper.new(message, self.class.converter)
end
|
#persisted? ⇒ Boolean
271
272
273
|
# File 'lib/protip/resource.rb', line 271
def persisted?
message.field?(:id)
end
|
#save ⇒ Object
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'lib/protip/resource.rb', line 250
def save
success = true
begin
if persisted?
update!
else
create!
end
rescue Protip::UnprocessableEntityError => error
success = false
error.errors.messages.each do |message|
errors.add :base, message
end
error.errors.field_errors.each do |field_error|
errors.add field_error.field, field_error.message
end
end
success
end
|