Class: Payg::Entity
- Inherits:
-
Object
- Object
- Payg::Entity
- Defined in:
- lib/payg/entity.rb
Overview
Entity class is the base class for all Payg objects This saves data in a hash internally, and makes it available via direct methods
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Instance Method Summary collapse
-
#initialize(attributes) ⇒ Entity
constructor
A new instance of Entity.
-
#method_missing(name) ⇒ Object
This method fakes attr_reader, but uses the @attributes hash as the source, instead of instance variables.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#to_json(*args) ⇒ Object
Public: Convert the Entity object to JSON Returns the JSON representation of the Entity (as a string).
-
#with_a_bang ⇒ Object
Mutates the entity in accordance with the block passed to this construct.
Constructor Details
#initialize(attributes) ⇒ Entity
Returns a new instance of Entity.
10 11 12 |
# File 'lib/payg/entity.rb', line 10 def initialize(attributes) @attributes = attributes end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
This method fakes attr_reader, but uses the @attributes hash as the source, instead of instance variables
17 18 19 20 21 22 23 |
# File 'lib/payg/entity.rb', line 17 def method_missing(name) if @attributes.key? name.to_s @attributes[name.to_s] else super end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
8 9 10 |
# File 'lib/payg/entity.rb', line 8 def attributes @attributes end |
Instance Method Details
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
25 26 27 |
# File 'lib/payg/entity.rb', line 25 def respond_to_missing?(method_name, include_private = false) @attributes.key?(method_name.to_s) || super end |
#to_json(*args) ⇒ Object
Public: Convert the Entity object to JSON Returns the JSON representation of the Entity (as a string)
31 32 33 |
# File 'lib/payg/entity.rb', line 31 def to_json(*args) @attributes.to_json(*args) end |
#with_a_bang ⇒ Object
Mutates the entity in accordance with the block passed to this construct
Used to implement bang methods, by calling the non-bang method in the passed block
40 41 42 43 44 |
# File 'lib/payg/entity.rb', line 40 def with_a_bang mutated_entity = yield @attributes = mutated_entity.attributes mutated_entity end |