Class: ApiStruct::Entity
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- ApiStruct::Entity
- Defined in:
- lib/api_struct/entity.rb
Direct Known Subclasses
Defined Under Namespace
Classes: EntityMash
Constant Summary
Constants included from ApiStruct::Extensions::ApiClient
ApiStruct::Extensions::ApiClient::REJECTED_METHODS
Instance Attribute Summary collapse
-
#entity ⇒ Object
readonly
Returns the value of attribute entity.
-
#entity_status ⇒ Object
readonly
Returns the value of attribute entity_status.
Attributes included from ApiStruct::Extensions::ApiClient
Class Method Summary collapse
- .attr_entity(*attrs, &block) ⇒ Object
- .collection(entities, entity_type = self) ⇒ Object
- .convert_to_entity(item, entity_type = self) ⇒ Object
- .entities?(attr, options) ⇒ Boolean (also: has_entities)
- .entity?(attr, options) ⇒ Boolean (also: has_entity)
- .entity_attributes ⇒ Object
Instance Method Summary collapse
- #failure? ⇒ Boolean
-
#initialize(entity, entity_status = true) ⇒ Entity
constructor
rubocop:disable Style/OptionalBooleanParameter.
-
#success? ⇒ Boolean
rubocop:enable Style/OptionalBooleanParameter.
Methods included from ApiStruct::Extensions::DryMonads
from_failure, from_monad, from_success
Methods included from ApiStruct::Extensions::ApiClient
Methods included from Concerns::Underscore
Constructor Details
#initialize(entity, entity_status = true) ⇒ Entity
rubocop:disable Style/OptionalBooleanParameter
68 69 70 71 72 73 |
# File 'lib/api_struct/entity.rb', line 68 def initialize(entity, entity_status = true) raise EntityError, "#{entity} must be Hash" unless entity.is_a?(Hash) @entity = EntityMash.new(extract_attributes(entity)) @entity_status = entity_status __setobj__(@entity) end |
Instance Attribute Details
#entity ⇒ Object (readonly)
Returns the value of attribute entity.
61 62 63 |
# File 'lib/api_struct/entity.rb', line 61 def entity @entity end |
#entity_status ⇒ Object (readonly)
Returns the value of attribute entity_status.
61 62 63 |
# File 'lib/api_struct/entity.rb', line 61 def entity_status @entity_status end |
Class Method Details
.attr_entity(*attrs, &block) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/api_struct/entity.rb', line 11 def attr_entity(*attrs, &block) entity_attributes.concat attrs attrs.each do |attr| define_entity_attribute_getter(attr, &block) define_entity_attribute_setter(attr) end end |
.collection(entities, entity_type = self) ⇒ Object
37 38 39 |
# File 'lib/api_struct/entity.rb', line 37 def collection(entities, entity_type = self) Collection.new(entities, entity_type) end |
.convert_to_entity(item, entity_type = self) ⇒ Object
41 42 43 44 |
# File 'lib/api_struct/entity.rb', line 41 def convert_to_entity(item, entity_type = self) raise EntityError, "#{entity_type} must be inherited from base_entity" unless entity_type < ApiStruct::Entity entity_type.new(item) end |
.entities?(attr, options) ⇒ Boolean Also known as: has_entities
20 21 22 23 24 25 |
# File 'lib/api_struct/entity.rb', line 20 def entities?(attr, ) entity_attributes << attr.to_sym define_method attr.to_s do self.class.collection(entity[attr], [:as]) end end |
.entity?(attr, options) ⇒ Boolean Also known as: has_entity
28 29 30 31 32 33 34 |
# File 'lib/api_struct/entity.rb', line 28 def entity?(attr, ) entity_attributes << attr.to_sym define_method attr.to_s do return false unless entity[attr] self.class.convert_to_entity(entity[attr], [:as]) end end |
.entity_attributes ⇒ Object
7 8 9 |
# File 'lib/api_struct/entity.rb', line 7 def entity_attributes @entity_attributes ||= [] end |
Instance Method Details
#failure? ⇒ Boolean
80 81 82 |
# File 'lib/api_struct/entity.rb', line 80 def failure? entity_status == false end |
#success? ⇒ Boolean
rubocop:enable Style/OptionalBooleanParameter
76 77 78 |
# File 'lib/api_struct/entity.rb', line 76 def success? entity_status == true end |