Class: ApiStruct::Entity
Constant Summary
ApiStruct::Extensions::ApiClient::REJECTED_METHODS
Instance Attribute Summary collapse
#clients
Class Method Summary
collapse
Instance Method Summary
collapse
from_failure, from_monad, from_success
client_service
#underscore
Constructor Details
#initialize(entity, entity_status = true) ⇒ Entity
Returns a new instance of Entity.
61
62
63
64
65
66
|
# File 'lib/api_struct/entity.rb', line 61
def initialize(entity, entity_status = true)
raise EntityError, "#{entity} must be Hash" unless entity.is_a?(Hash)
@entity = Hashie::Mash.new((entity))
@entity_status = entity_status
__setobj__(@entity)
end
|
Instance Attribute Details
#entity ⇒ Object
Returns the value of attribute entity.
59
60
61
|
# File 'lib/api_struct/entity.rb', line 59
def entity
@entity
end
|
#entity_status ⇒ Object
Returns the value of attribute entity_status.
59
60
61
|
# File 'lib/api_struct/entity.rb', line 59
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
35
36
37
|
# File 'lib/api_struct/entity.rb', line 35
def collection(entities, entity_type = self)
Collection.new(entities, entity_type)
end
|
.convert_to_entity(item, entity_type = self) ⇒ Object
39
40
41
42
|
# File 'lib/api_struct/entity.rb', line 39
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
|
.entity_attributes ⇒ Object
7
8
9
|
# File 'lib/api_struct/entity.rb', line 7
def entity_attributes
@entity_attributes ||= []
end
|
.has_entities(attr, options) ⇒ Object
20
21
22
23
24
25
|
# File 'lib/api_struct/entity.rb', line 20
def has_entities(attr, options)
entity_attributes << attr.to_sym
define_method attr.to_s do
self.class.collection(entity[attr], options[:as])
end
end
|
.has_entity(attr, options) ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/api_struct/entity.rb', line 27
def has_entity(attr, options)
entity_attributes << attr.to_sym
define_method attr.to_s do
return unless entity[attr]
self.class.convert_to_entity(entity[attr], options[:as])
end
end
|
Instance Method Details
#failure? ⇒ Boolean
72
73
74
|
# File 'lib/api_struct/entity.rb', line 72
def failure?
entity_status == false
end
|
#success? ⇒ Boolean
68
69
70
|
# File 'lib/api_struct/entity.rb', line 68
def success?
entity_status == true
end
|