Class: Bayarcash::Resources::Resource

Inherits:
Object
  • Object
show all
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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, bayarcash = nil) ⇒ Resource

Returns a new instance of Resource.

Parameters:

  • attributes (Hash) (defaults to: {})

    the API payload

  • bayarcash (Bayarcash::Client, nil) (defaults to: nil)

    owning client (never serialized)



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.

Parameters:

  • names (Array<Symbol, String>)


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_attributesArray<String>

Returns declared attribute names, including inherited ones.

Returns:

  • (Array<String>)

    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).

Parameters:

  • key (String, Symbol)

Returns:

  • (Object, nil)


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

Returns:

  • (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_hHash{String => Object} Also known as: to_array, to_hash

Returns the attributes, with nested resources converted.

Returns:

  • (Hash{String => Object})

    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