Class: Billomat::Models::Base
- Inherits:
-
Object
- Object
- Billomat::Models::Base
- Defined in:
- lib/billomat/models/base.rb
Overview
This class is the base for all other models (resources). It handles the communication with the gateway to talk to the API.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Class Method Summary collapse
-
.find(id) ⇒ Billomat::Models::Base?
Tries to find the resource for the given id.
-
.where(hash = {}) ⇒ Array<Billomat::Models::Base>
Allows to query for a record.
Instance Method Summary collapse
- #create ⇒ TrueClass
- #delete ⇒ TrueClass
-
#id ⇒ String?
The object’s ID.
-
#initialize(data = {}) ⇒ Billomat::Models::Base
constructor
Initializes a new model.
-
#method_missing(method, *args, &block) ⇒ Object
All values in the @data hash can be accessed like a ‘normal’ method.
-
#respond_to_missing?(method, include_privat = false) ⇒ TrueClass, FalseClass
Necessary for method_missing.
-
#save ⇒ TrueClass
Persists the current object in the API.
- #update ⇒ TrueClass
-
#wrapped_data ⇒ Hash
Wraps the data so the API accepts the request.
Constructor Details
#initialize(data = {}) ⇒ Billomat::Models::Base
Initializes a new model
38 39 40 |
# File 'lib/billomat/models/base.rb', line 38 def initialize(data = {}) @data = OpenStruct.new(data) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
All values in the @data hash can be accessed like a ‘normal’ method
104 105 106 107 |
# File 'lib/billomat/models/base.rb', line 104 def method_missing(method, *args, &block) return @data[method] if @data.to_h.keys.include?(method) super end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
11 12 13 |
# File 'lib/billomat/models/base.rb', line 11 def data @data end |
Class Method Details
.find(id) ⇒ Billomat::Models::Base?
Tries to find the resource for the given id
18 19 20 21 22 |
# File 'lib/billomat/models/base.rb', line 18 def self.find(id) return nil if id.nil? resp = Billomat::Gateway.new(:get, "#{base_path}/#{id}").run new(resp[resource_name]) end |
.where(hash = {}) ⇒ Array<Billomat::Models::Base>
Allows to query for a record
29 30 31 |
# File 'lib/billomat/models/base.rb', line 29 def self.where(hash = {}) Billomat::Search.new(self, hash).run end |
Instance Method Details
#create ⇒ TrueClass
53 54 55 56 57 58 59 60 61 |
# File 'lib/billomat/models/base.rb', line 53 def create resp = Billomat::Gateway.new( :post, self.class.base_path, wrapped_data ).run @data = OpenStruct.new(resp[self.class.resource_name]) true end |
#delete ⇒ TrueClass
73 74 75 76 77 78 |
# File 'lib/billomat/models/base.rb', line 73 def delete path = "#{self.class.base_path}/#{id}" Billomat::Gateway.new(:delete, path).run true end |
#id ⇒ String?
Returns The object’s ID.
81 82 83 |
# File 'lib/billomat/models/base.rb', line 81 def id @data['id'] || nil end |
#respond_to_missing?(method, include_privat = false) ⇒ TrueClass, FalseClass
Necessary for method_missing
115 116 117 |
# File 'lib/billomat/models/base.rb', line 115 def respond_to_missing?(method, include_privat = false) @data.to_h.keys.include?(method.to_s) || super end |
#save ⇒ TrueClass
Persists the current object in the API. When record is new it calls create, otherwise it saves the object.
47 48 49 50 |
# File 'lib/billomat/models/base.rb', line 47 def save return create if id.blank? update end |
#update ⇒ TrueClass
64 65 66 67 68 69 70 |
# File 'lib/billomat/models/base.rb', line 64 def update path = "#{self.class.base_path}/#{id}" resp = Billomat::Gateway.new(:put, path, wrapped_data).run @data = resp[self.class.resource_name] true end |
#wrapped_data ⇒ Hash
Wraps the data so the API accepts the request
93 94 95 |
# File 'lib/billomat/models/base.rb', line 93 def wrapped_data { self.class.resource_name => @data.to_h } end |