Class: BadBill::BaseResource
- Inherits:
-
Object
- Object
- BadBill::BaseResource
- Extended by:
- Resource
- Includes:
- ForwardMethods, Resource
- Defined in:
- lib/badbill/base_resource.rb
Direct Known Subclasses
Client, Invoice, InvoiceComment, InvoiceItem, InvoicePayment, Recurring
Instance Attribute Summary collapse
-
#data ⇒ Object
All data in a Hash.
-
#id ⇒ Object
ID of the resource.
Class Method Summary collapse
-
.all(filter = {}) ⇒ Array<Resource>
Get all resources of this type.
-
.create(params) ⇒ Resource
Create a new resource with the given parameters.
-
.find(id) ⇒ Resource
Get the resource with the given ID.
Instance Method Summary collapse
-
#delete ⇒ Boolean
Delete this resource.
- #error ⇒ Object
-
#initialize(id, data) ⇒ Resource
constructor
Public: Create new resource object.
-
#save ⇒ Resource
Save any changed data.
Methods included from Resource
Methods included from ForwardMethods
Constructor Details
#initialize(id, data) ⇒ Resource
Public: Create new resource object.
All resources have an ID and data. Methods are proxied to the data object.
23 24 25 26 |
# File 'lib/badbill/base_resource.rb', line 23 def initialize id, data @id = id @data = data end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class BadBill::ForwardMethods
Instance Attribute Details
#data ⇒ Object
All data in a Hash.
13 14 15 |
# File 'lib/badbill/base_resource.rb', line 13 def data @data end |
#id ⇒ Object
ID of the resource.
11 12 13 |
# File 'lib/badbill/base_resource.rb', line 11 def id @id end |
Class Method Details
.all(filter = {}) ⇒ Array<Resource>
Get all resources of this type.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/badbill/base_resource.rb', line 34 def self.all filter={} all = get resource_name, filter return all if all.error resources = all.__send__(resource_name) case resources['@total'].to_i when 0 [] when 1 data = resources.__send__(resource_name_singular) [new(data.id.to_i, data)] else resources.__send__(resource_name_singular).map do |res| new res.id.to_i, res end end end |
.create(params) ⇒ Resource
Create a new resource with the given parameters.
78 79 80 81 82 83 84 |
# File 'lib/badbill/base_resource.rb', line 78 def self.create params res = post(resource_name, {resource_name_singular => params}) return res if res.error res_data = res.__send__(resource_name_singular) new res_data.id.to_i, res_data end |
.find(id) ⇒ Resource
Get the resource with the given ID.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/badbill/base_resource.rb', line 57 def self.find id c = get resource_name, id if c.error if c.error.kind_of? Faraday::Error::ResourceNotFound return nil else return c end end data = c.__send__(resource_name_singular) return nil if data.nil? new id, data end |
Instance Method Details
#delete ⇒ Boolean
Delete this resource.
109 110 111 112 113 |
# File 'lib/badbill/base_resource.rb', line 109 def delete # Hack: We can't call #delete here, because we ARE #delete resp = call resource_name, id, nil, :delete !resp end |
#error ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/badbill/base_resource.rb', line 122 def error if @data.respond_to?(:error) @data.error else nil end end |
#save ⇒ Resource
Save any changed data.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/badbill/base_resource.rb', line 89 def save # Only save if @__mutated__ && !@__mutated__.empty? data = Hash[@__mutated__.map { |k| [k, @data[k]] }] res = put resource_name, id, {resource_name_singular => data} return res if res.error res_data = res.__send__(resource_name_singular) @data.merge!(res_data) @__mutated__.clear end self end |