Class: WrAPI::Request::Entity
- Inherits:
-
Object
- Object
- WrAPI::Request::Entity
- Defined in:
- lib/wrapi/entity.rb
Overview
Entity class to represent and manipulate API data
Class Method Summary collapse
-
.create(attr) ⇒ Entity+
Factory method to create an entity or array of entities.
-
.entify(attribute) ⇒ Array<Entity>
Converts an array of hashes to an array of entities.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Checks if two entities are equal.
-
#accessor(method) ⇒ Object
Accesses an attribute, converting it to an Entity if it is a Hash or Array.
-
#attributes ⇒ Hash
Returns the attributes of the entity.
-
#attributes=(val) ⇒ Object
Sets the attributes of the entity.
-
#clone ⇒ Entity
Clones the entity.
-
#initialize(attr) ⇒ Entity
constructor
Initializes a new Entity.
-
#method_missing(method_sym, *arguments, &block) ⇒ Object
Handles dynamic method calls for attribute access and assignment.
-
#respond_to?(method_sym, include_private = false) ⇒ Boolean
Checks if the entity responds to a method.
-
#to_json(_options = {}) ⇒ String
Converts the entity to a JSON string.
Constructor Details
#initialize(attr) ⇒ Entity
Initializes a new Entity
27 28 29 30 31 32 33 34 |
# File 'lib/wrapi/entity.rb', line 27 def initialize(attr) case attr when Hash @attributes = attr.clone.transform_keys(&:to_s) else @attributes = attr.clone end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *arguments, &block) ⇒ Object
Handles dynamic method calls for attribute access and assignment
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/wrapi/entity.rb', line 55 def method_missing(method_sym, *arguments, &block) # assignment method = method_sym.to_s assignment = method_sym[/.*(?==\z)/m] if assignment raise ArgumentError, "wrong number of arguments (given #{arguments.length}, expected 1)", caller(1) unless arguments.length == 1 @attributes[assignment] = arguments[0] elsif @attributes.include? method accessor(method) else # delegate to hash @attributes.send(method_sym, *arguments, &block) end end |
Class Method Details
.create(attr) ⇒ Entity+
Factory method to create an entity or array of entities
16 17 18 19 20 21 22 |
# File 'lib/wrapi/entity.rb', line 16 def self.create(attr) if attr.is_a? Array Entity.entify(attr) elsif attr Entity.new(attr) end end |
.entify(attribute) ⇒ Array<Entity>
Converts an array of hashes to an array of entities
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/wrapi/entity.rb', line 132 def self.entify(attribute) if attribute.any? && (attribute.first.is_a? Hash) attribute.dup.map do |item| #item.is_a?(Hash) ? self.class.new(item) : item Entity.create(item) end else attribute end end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Checks if two entities are equal
123 124 125 |
# File 'lib/wrapi/entity.rb', line 123 def ==(other) (self.class == other.class) && (self.attributes.eql? other.attributes) end |
#accessor(method) ⇒ Object
Accesses an attribute, converting it to an Entity if it is a Hash or Array
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/wrapi/entity.rb', line 97 def accessor(method) attribute = @attributes[method] case attribute when Hash @attributes[method] = self.class.new(attribute) when Array # make deep copy @attributes[method] = Entity.entify(attribute) else attribute end end |
#attributes ⇒ Hash
Returns the attributes of the entity
39 40 41 |
# File 'lib/wrapi/entity.rb', line 39 def attributes @attributes || {} end |
#attributes=(val) ⇒ Object
Sets the attributes of the entity
46 47 48 |
# File 'lib/wrapi/entity.rb', line 46 def attributes=(val) @attributes = val || {} end |
#clone ⇒ Entity
Clones the entity
113 114 115 116 117 |
# File 'lib/wrapi/entity.rb', line 113 def clone cln = super cln.attributes = @attributes.clone cln end |
#respond_to?(method_sym, include_private = false) ⇒ Boolean
Checks if the entity responds to a method
76 77 78 79 80 81 82 83 |
# File 'lib/wrapi/entity.rb', line 76 def respond_to?(method_sym, include_private = false) @attributes ||= {} if @attributes.include? method_sym.to_s true else @attributes.respond_to?(method_sym, include_private) end end |
#to_json(_options = {}) ⇒ String
Converts the entity to a JSON string
89 90 91 |
# File 'lib/wrapi/entity.rb', line 89 def to_json( = {}) @attributes.to_json end |