Class: ApiStruct::Entity

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
ApiStruct::Extensions::ApiClient, ApiStruct::Extensions::DryMonads
Defined in:
lib/api_struct/entity.rb

Direct Known Subclasses

ApiStruct::Errors::Entity

Constant Summary

Constants included from ApiStruct::Extensions::ApiClient

ApiStruct::Extensions::ApiClient::REJECTED_METHODS

Instance Attribute Summary collapse

Attributes included from ApiStruct::Extensions::ApiClient

#clients

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApiStruct::Extensions::DryMonads

from_failure, from_monad, from_success

Methods included from ApiStruct::Extensions::ApiClient

client_service

Methods included from Concerns::Underscore

#underscore

Constructor Details

#initialize(entity, entity_status = true) ⇒ Entity

Returns a new instance of Entity.

Raises:



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(extract_attributes(entity))
  @entity_status = entity_status
  __setobj__(@entity)
end

Instance Attribute Details

#entityObject (readonly)

Returns the value of attribute entity.



59
60
61
# File 'lib/api_struct/entity.rb', line 59

def entity
  @entity
end

#entity_statusObject (readonly)

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

Raises:



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_attributesObject



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

Returns:

  • (Boolean)


72
73
74
# File 'lib/api_struct/entity.rb', line 72

def failure?
  entity_status == false
end

#success?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/api_struct/entity.rb', line 68

def success?
  entity_status == true
end