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
Client, Contact, CreditNote, CreditNoteItem, Invoice, InvoiceComment, InvoiceItem, InvoicePayment, Tag, Template
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
-
#as_json(_options = nil) ⇒ Hash
Returns the object with the right JSON structure.
-
#create ⇒ TrueClass
(also: #create!)
rubocop:disable Style/OpenStructUse – because of the convenient dynamic data access rubocop:disable Naming/PredicateMethod – because this method performs an action, not a predicate check (bool is for error signaling).
-
#delete ⇒ TrueClass
(also: #delete!)
rubocop:disable Naming/PredicateMethod – because this method performs an action, not a predicate check (bool is for error signaling).
-
#id ⇒ String?
The object’s ID.
-
#initialize(data = {}) ⇒ Billomat::Models::Base
constructor
Initializes a new model.
-
#method_missing(method, *args) ⇒ 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
(also: #save!)
Persists the current object in the API.
-
#update ⇒ TrueClass
(also: #update!)
rubocop:disable Naming/PredicateMethod – because this method performs an action, not a predicate check (bool is for error signaling).
-
#wrapped_data ⇒ Hash
Wraps the data so the API accepts the request.
Constructor Details
#initialize(data = {}) ⇒ Billomat::Models::Base
Initializes a new model.
rubocop:disable Style/OpenStructUse – because of the convenient
dynamic data access
36 37 38 |
# File 'lib/billomat/models/base.rb', line 36 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) ⇒ Object
All values in the @data hash can be accessed like a ‘normal’ method.
127 128 129 130 131 |
# File 'lib/billomat/models/base.rb', line 127 def method_missing(method, *args, &) return @data[method] if @data.to_h.key?(method) super end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
8 9 10 |
# File 'lib/billomat/models/base.rb', line 8 def data @data end |
Class Method Details
.find(id) ⇒ Billomat::Models::Base?
Tries to find the resource for the given id.
14 15 16 17 18 19 |
# File 'lib/billomat/models/base.rb', line 14 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.
25 26 27 |
# File 'lib/billomat/models/base.rb', line 25 def self.where(hash = {}) Billomat::Search.new(self, hash).run end |
Instance Method Details
#as_json(_options = nil) ⇒ Hash
Returns the object with the right JSON structure.
117 118 119 |
# File 'lib/billomat/models/base.rb', line 117 def as_json( = nil) @data.to_h end |
#create ⇒ TrueClass Also known as: create!
rubocop:disable Style/OpenStructUse – because of the convenient
dynamic data access
rubocop:disable Naming/PredicateMethod – because this method performs
an action, not a predicate check (bool is for error signaling)
58 59 60 61 62 63 64 65 66 |
# File 'lib/billomat/models/base.rb', line 58 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 Also known as: delete!
rubocop:disable Naming/PredicateMethod – because this method performs
an action, not a predicate check (bool is for error signaling)
89 90 91 92 93 94 |
# File 'lib/billomat/models/base.rb', line 89 def delete path = "#{self.class.base_path}/#{id}" Billomat::Gateway.new(:delete, path).run true end |
#id ⇒ String?
Returns the object’s ID.
99 100 101 |
# File 'lib/billomat/models/base.rb', line 99 def id @data['id'] || nil end |
#respond_to_missing?(method, include_privat = false) ⇒ TrueClass, FalseClass
Necessary for method_missing.
138 139 140 |
# File 'lib/billomat/models/base.rb', line 138 def respond_to_missing?(method, include_privat = false) @data.to_h.key?(method.to_s) || super end |
#save ⇒ TrueClass Also known as: save!
Persists the current object in the API. When record is new it calls create, otherwise it saves the object.
45 46 47 48 49 |
# File 'lib/billomat/models/base.rb', line 45 def save return create if id.nil? update end |
#update ⇒ TrueClass Also known as: update!
rubocop:disable Naming/PredicateMethod – because this method performs
an action, not a predicate check (bool is for error signaling)
75 76 77 78 79 80 81 |
# File 'lib/billomat/models/base.rb', line 75 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.
110 111 112 |
# File 'lib/billomat/models/base.rb', line 110 def wrapped_data { self.class.resource_name => @data.to_h } end |