module Creditario::Resource

Creditario::Resource

Modulo que se encarga de establecer el mecanismo de inicialización de las siguientes clases:

Attributes

attributes[R]

Hash que representa los attributos de un recurso.

Public Class Methods

new(attributes, links = []) click to toggle source

Recibe parametro attributes de tipo Hash y genera los métodos de escritura y lectura para cada elemento del Hash.

# File lib/creditario/resources/resource.rb, line 40
def initialize(attributes, links = [])
  @attributes = attributes
  @links = links
  define_attributes_setters_and_getters
end

Public Instance Methods

[](key) click to toggle source

Método para acceder con llave al elemento del Hash attributes

# File lib/creditario/resources/resource.rb, line 48
def [](key)
  attributes[key]
end
[]=(key, value) click to toggle source

Método para acceder con llave y sobreescribir el elemento del Hash attributes

# File lib/creditario/resources/resource.rb, line 54
def []=(key, value)
  attributes[key] = value
end

Private Instance Methods

build_associations(klass, collection_or_attributes) click to toggle source
# File lib/creditario/resources/resource.rb, line 83
def build_associations(klass, collection_or_attributes)
  return klass.new(collection_or_attributes) if collection_or_attributes.is_a?(Hash)

  collection_or_attributes.map do |attributes|
    klass.new(attributes)
  end
end
define_attribute_setter_and_getter(attribute, value) click to toggle source
# File lib/creditario/resources/resource.rb, line 73
def define_attribute_setter_and_getter(attribute, value)
  self.class.define_method(attribute) do
    attributes[attribute]
  end

  self.class.define_method("#{attribute}=") do |new_value|
    attributes[attribute] = new_value
  end
end
define_attributes_setters_and_getters() click to toggle source
# File lib/creditario/resources/resource.rb, line 60
def define_attributes_setters_and_getters
  attributes.each do |attribute, value|
    if self.class.class_variable_defined?(:@@associations)
      association = self.class.class_variable_get(:@@associations).find { |association| association[:name] == attribute.to_sym }
      unless association.nil?
        attributes[attribute] = build_associations(association[:class], value)
      end
    end

    define_attribute_setter_and_getter(attribute, value)
  end
end