Class: Bayarcash::Resources::Resource
- Inherits:
-
Object
- Object
- Bayarcash::Resources::Resource
- Defined in:
- lib/bayarcash/resources/resource.rb
Overview
Base data-transfer object for API responses.
Attributes are populated from the (snake_case) keys of the API payload and exposed as reader methods. Declared attributes always respond (returning nil when the API omits them); any unknown field the API returns is captured too, so the DTOs are tolerant of new or missing fields.
Direct Known Subclasses
FpxBankResource, FpxDirectDebitApplicationResource, FpxDirectDebitResource, PaymentIntentResource, PortalResource, TransactionResource
Class Method Summary collapse
-
.attributes(*names) ⇒ Object
Declare known attributes for a resource subclass.
-
.declared_attributes ⇒ Array<String>
Declared attribute names, including inherited ones.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Read a raw attribute by key (string or symbol).
-
#initialize(attributes = {}, bayarcash = nil) ⇒ Resource
constructor
A new instance of Resource.
- #method_missing(name, *args) ⇒ Object
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#to_h ⇒ Hash{String => Object}
(also: #to_array, #to_hash)
The attributes, with nested resources converted.
Constructor Details
#initialize(attributes = {}, bayarcash = nil) ⇒ Resource
Returns a new instance of Resource.
33 34 35 36 37 |
# File 'lib/bayarcash/resources/resource.rb', line 33 def initialize(attributes = {}, bayarcash = nil) @bayarcash = bayarcash @attributes = {} fill(attributes) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/bayarcash/resources/resource.rb', line 61 def method_missing(name, *args) key = name.to_s if @attributes.key?(key) || self.class.declared_attributes.include?(key) @attributes[key] else super end end |
Class Method Details
.attributes(*names) ⇒ Object
Declare known attributes for a resource subclass. Each becomes a reader that returns nil when absent.
16 17 18 19 20 21 22 23 |
# File 'lib/bayarcash/resources/resource.rb', line 16 def self.attributes(*names) @declared_attributes ||= [] names.each do |name| key = name.to_s @declared_attributes << key unless @declared_attributes.include?(key) define_method(key) { @attributes[key] } end end |
.declared_attributes ⇒ Array<String>
Returns declared attribute names, including inherited ones.
26 27 28 29 |
# File 'lib/bayarcash/resources/resource.rb', line 26 def self.declared_attributes inherited = superclass.respond_to?(:declared_attributes) ? superclass.declared_attributes : [] inherited + (@declared_attributes || []) end |
Instance Method Details
#[](key) ⇒ Object?
Read a raw attribute by key (string or symbol).
43 44 45 |
# File 'lib/bayarcash/resources/resource.rb', line 43 def [](key) @attributes[key.to_s] end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
56 57 58 59 |
# File 'lib/bayarcash/resources/resource.rb', line 56 def respond_to_missing?(name, include_private = false) key = name.to_s @attributes.key?(key) || self.class.declared_attributes.include?(key) || super end |
#to_h ⇒ Hash{String => Object} Also known as: to_array, to_hash
Returns the attributes, with nested resources converted.
48 49 50 51 52 |
# File 'lib/bayarcash/resources/resource.rb', line 48 def to_h @attributes.each_with_object({}) do |(key, value), memo| memo[key] = deep_to_h(value) end end |