Class: Kounta::Resource
- Inherits:
-
Hashie::Dash
- Object
- Hashie::Dash
- Kounta::Resource
- Includes:
- Hashie::Extensions::Coercion, Hashie::Extensions::Dash::IndifferentAccess
- Defined in:
- lib/kounta/resource.rb
Direct Known Subclasses
Address, Break, Category, Company, Customer, Inventory, Line, Order, Payment, Person, PriceList, Product, Register, Shift, Site, Staff, Tax, Webhook
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
Class Method Summary collapse
- .coerce(data) ⇒ Object
-
.has_many(sym, klass, assignments, route) ⇒ Object
rubocop:disable Naming/PredicateName.
-
.has_many_in_time_range(sym, klass, assignments, route) ⇒ Object
rubocop:disable Naming/PredicateName.
-
.has_one(sym, klass, assignments, route) ⇒ Object
rubocop:disable Naming/PredicateName.
Instance Method Summary collapse
- #delete! ⇒ Object
- #ignored_properties(array = []) ⇒ Object
-
#initialize(hash = {}) ⇒ Resource
constructor
A new instance of Resource.
- #new? ⇒ Boolean
- #save! ⇒ Object
- #to_hash(hash = {}) ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ Resource
Returns a new instance of Resource.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/kounta/resource.rb', line 47 def initialize(hash = {}) return unless hash hash.each_pair do |k, v| begin self[k] = v if respond_to? k.to_sym rescue NoMethodError raise Kounta::Errors::UnknownResourceAttribute, "Unknown attribute: #{k} on resource #{self.class}" end end end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
9 10 11 |
# File 'lib/kounta/resource.rb', line 9 def client @client end |
Class Method Details
.coerce(data) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/kounta/resource.rb', line 15 def self.coerce(data) if data.is_a? Array data.map { |item| new(item) } else new(data) end end |
.has_many(sym, klass, assignments, route) ⇒ Object
rubocop:disable Naming/PredicateName
33 34 35 36 37 38 |
# File 'lib/kounta/resource.rb', line 33 def self.has_many(sym, klass, assignments, route) # rubocop:disable Naming/PredicateName define_method(sym) do |has_many_params = nil, *args| client.objects_from_response(klass, :get, route.call(self, has_many_params), params: args[0]) .map { |returned_klass| assign_into(returned_klass, self, assignments) } end end |
.has_many_in_time_range(sym, klass, assignments, route) ⇒ Object
rubocop:disable Naming/PredicateName
40 41 42 43 44 45 |
# File 'lib/kounta/resource.rb', line 40 def self.has_many_in_time_range(sym, klass, assignments, route) # rubocop:disable Naming/PredicateName define_method(sym) do |has_many_params = nil, *args| client.objects_from_response_in_time_range(klass, :get, route.call(self, has_many_params), params: args[0]) .map { |returned_klass| assign_into(returned_klass, self, assignments) } end end |
.has_one(sym, klass, assignments, route) ⇒ Object
rubocop:disable Naming/PredicateName
23 24 25 26 27 28 29 30 31 |
# File 'lib/kounta/resource.rb', line 23 def self.has_one(sym, klass, assignments, route) # rubocop:disable Naming/PredicateName define_method(sym) do |item_id = nil, *args| if item_id assign_into(client.object_from_response(klass, :get, route.call(self, item_id), params: args[0]), self, assignments) else assign_into(klass.new, self, assignments) end end end |
Instance Method Details
#delete! ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/kounta/resource.rb', line 81 def delete! return self if new? response = client.perform(resource_path, :delete) except!('id', 'created_at', 'updated_at') if response.status == 204 self end |
#ignored_properties(array = []) ⇒ Object
94 95 96 |
# File 'lib/kounta/resource.rb', line 94 def ignored_properties(array = []) array + %i[created_at updated_at id company_id] end |
#new? ⇒ Boolean
90 91 92 |
# File 'lib/kounta/resource.rb', line 90 def new? !id end |
#save! ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/kounta/resource.rb', line 68 def save! response = new? ? client.perform(resource_path, :post, body: to_hash) : client.perform(resource_path, :put, body: to_hash) # automatically follow redirects to resources response = client.perform(response.headers['location'], :get) if response.status == 201 response.parsed.each_pair do |k, v| self[k] = v if respond_to? k.to_sym end self end |
#to_hash(hash = {}) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/kounta/resource.rb', line 59 def to_hash(hash = {}) {}.tap do |returning| self.class.properties.each do |property| next if ignored_properties.include?(property) returning[property] = self[property] if self[property] end end.merge(hash) end |