Class: Apiphobic::Resource::Model
- Inherits:
-
Object
- Object
- Apiphobic::Resource::Model
- Extended by:
- Forwardable
- Defined in:
- lib/apiphobic/resource/model.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_PROCESSORS =
%w{including}.freeze
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#processors ⇒ Object
Returns the value of attribute processors.
-
#resource ⇒ Object
Returns the value of attribute resource.
Instance Method Summary collapse
- #assign_attributes(attrs) ⇒ Object
-
#initialize(resource:, id:, parameters:, **options) ⇒ Model
constructor
A new instance of Model.
- #meta ⇒ Object
- #processed ⇒ Object
- #save ⇒ Object
- #save! ⇒ Object
Constructor Details
#initialize(resource:, id:, parameters:, **options) ⇒ Model
Returns a new instance of Model.
27 28 29 30 31 32 |
# File 'lib/apiphobic/resource/model.rb', line 27 def initialize(resource:, id:, parameters:, **) self.id = id self.resource = resource self.parameters = parameters.dup self.processors = .fetch(:processors, self.class::DEFAULT_PROCESSORS) end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
18 19 20 |
# File 'lib/apiphobic/resource/model.rb', line 18 def id @id end |
#parameters ⇒ Object
Returns the value of attribute parameters.
18 19 20 |
# File 'lib/apiphobic/resource/model.rb', line 18 def parameters @parameters end |
#processors ⇒ Object
Returns the value of attribute processors.
21 22 23 |
# File 'lib/apiphobic/resource/model.rb', line 21 def processors @processors end |
#resource ⇒ Object
Returns the value of attribute resource.
18 19 20 |
# File 'lib/apiphobic/resource/model.rb', line 18 def resource @resource end |
Instance Method Details
#assign_attributes(attrs) ⇒ Object
55 56 57 58 59 |
# File 'lib/apiphobic/resource/model.rb', line 55 def assign_attributes(attrs) processed.assign_attributes(attrs) processed end |
#meta ⇒ Object
49 50 51 52 53 |
# File 'lib/apiphobic/resource/model.rb', line 49 def ||= processors.inject({}) do |, processor| .merge processor.(processed, parameters) end end |
#processed ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/apiphobic/resource/model.rb', line 34 def processed @processed \ ||= begin processed = processors.inject(resource) do |processed_resource, processor| processor.processed(processed_resource, parameters) end if instance_of?(::Apiphobic::Resource::Collection) processed else processed.find_by(id: id) || processed.new end end end |
#save ⇒ Object
67 68 69 70 71 |
# File 'lib/apiphobic/resource/model.rb', line 67 def save processed.save processed end |
#save! ⇒ Object
61 62 63 64 65 |
# File 'lib/apiphobic/resource/model.rb', line 61 def save! processed.save! processed end |