class ApiStruct::Entity
Attributes
entity[R]
entity_status[R]
Public Class Methods
attr_entity(*attrs, &block)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/api_struct/entity.rb, line 7 def entity_attributes @entity_attributes ||= [] end
has_entities(attr, options)
click to toggle source
# 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)
click to toggle source
# 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
new(entity, entity_status = true)
click to toggle source
# 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
Private Class Methods
define_entity_attribute_getter(attr) { |entity| ... }
click to toggle source
# File lib/api_struct/entity.rb, line 46 def define_entity_attribute_getter(attr) define_method attr.to_s do block_given? ? yield(entity[attr]) : entity[attr] end end
define_entity_attribute_setter(attr)
click to toggle source
# File lib/api_struct/entity.rb, line 52 def define_entity_attribute_setter(attr) define_method "#{attr}=" do |value| entity[attr] = value end end
Public Instance Methods
failure?()
click to toggle source
# File lib/api_struct/entity.rb, line 72 def failure? entity_status == false end
success?()
click to toggle source
# File lib/api_struct/entity.rb, line 68 def success? entity_status == true end
Private Instance Methods
extract_attributes(attributes)
click to toggle source
# File lib/api_struct/entity.rb, line 78 def extract_attributes(attributes) formatted_attributes = attributes.map { |name, value| [format_name(name), value] }.to_h formatted_attributes.select { |key, _value| self.class.entity_attributes.include?(key.to_sym) } end
format_name(name)
click to toggle source
# File lib/api_struct/entity.rb, line 83 def format_name(name) Dry::Inflector.new.underscore(name).to_sym end