Class: Yookassa::Entities::Base
- Inherits:
-
Object
- Object
- Yookassa::Entities::Base
- Defined in:
- lib/yookassa/entities/base.rb
Overview
Base entity class that wraps API response hashes with dynamic attribute access.
Attributes returned by the API are accessible as methods via method_missing.
Nested hashes are automatically wrapped into entity instances, and arrays of
hashes become arrays of entities.
Instance Attribute Summary collapse
-
#attributes ⇒ Hash<String, Object>
readonly
Normalized attribute hash (string keys).
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Hash-style attribute access.
-
#initialize(data) ⇒ Base
constructor
A new instance of Base.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#to_h ⇒ Hash<String, Object>
Returns the raw attribute hash.
Constructor Details
#initialize(data) ⇒ Base
Returns a new instance of Base.
22 23 24 |
# File 'lib/yookassa/entities/base.rb', line 22 def initialize(data) @attributes = self.class.send(:normalize, data) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object (private)
47 48 49 50 51 52 53 54 |
# File 'lib/yookassa/entities/base.rb', line 47 def method_missing(method_name, *args) key = method_name.to_s if @attributes.key?(key) wrap_value(@attributes[key]) else super end end |
Instance Attribute Details
#attributes ⇒ Hash<String, Object> (readonly)
Returns normalized attribute hash (string keys).
19 20 21 |
# File 'lib/yookassa/entities/base.rb', line 19 def attributes @attributes end |
Instance Method Details
#[](key) ⇒ Object?
Hash-style attribute access.
30 31 32 |
# File 'lib/yookassa/entities/base.rb', line 30 def [](key) @attributes[key.to_s] end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
41 42 43 |
# File 'lib/yookassa/entities/base.rb', line 41 def respond_to_missing?(method_name, include_private = false) @attributes.key?(method_name.to_s) || super end |
#to_h ⇒ Hash<String, Object>
Returns the raw attribute hash.
37 38 39 |
# File 'lib/yookassa/entities/base.rb', line 37 def to_h @attributes end |