Class: Payg::Entity

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

Order

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attributesObject (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

Returns:

  • (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_bangObject

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