module ActiveRequest::HasMany::ClassMethods

Public Instance Methods

belongs_to(association, options = nil) click to toggle source
# File lib/active_request/belongs_to.rb, line 8
def belongs_to(association, options = nil)
  @belongs_tos ||= []
  class_name = !options.nil? && !options[:class_name].nil? ? options[:class_name] : (association.to_s.split.map(&:capitalize)*' ')
  foreign_key = !options.nil? && !options[:foreign_key].nil? ? options[:foreign_key] : "#{association}_id"
  @belongs_tos << { association: association, class_name: class_name, foreign_key: foreign_key }
end
belongs_tos() click to toggle source
# File lib/active_request/belongs_to.rb, line 15
def belongs_tos
  @belongs_tos
end
build_belongs_tos(alloc) click to toggle source
# File lib/active_request/belongs_to.rb, line 19
def build_belongs_tos(alloc)
  alloc.belongs_tos.each do |many|
    alloc.instance_variable_set("@#{many[:foreign_key]}", nil)
    alloc.instance_variable_set("@#{many[:association]}", nil)
    define_method(many[:association]) do
      variable = alloc.instance_variable_get("@#{many[:association]}")
      if id && variable.blank?
        father_ojb = Object.const_get(many[:class_name])
        variable = father_ojb.find send(many[:foreign_key])
        send("#{many[:association]}=", variable) if variable
      end
      variable
    end
    define_method(many[:foreign_key]) do
      alloc.instance_variable_get("@#{many[:foreign_key]}")
    end
    define_method("#{many[:association]}=") do |association_setter|
      alloc.instance_variable_set("@#{many[:association]}", association_setter)
    end
    define_method("#{many[:foreign_key]}=") do |foreign_key_setter|
      alloc.instance_variable_set("@#{many[:foreign_key]}", foreign_key_setter)
    end
  end if alloc.belongs_tos
end
build_has_manys(alloc) click to toggle source
# File lib/active_request/has_many.rb, line 18
def build_has_manys(alloc)
  alloc.has_manys.each do |many|
    alloc.instance_variable_set("@#{many[:association]}", [])
    define_method(many[:association]) do
      variable = alloc.instance_variable_get("@#{many[:association]}")
      if id && variable.blank?
        father_ojb = Object.const_get(many[:class_name])
        father_model_name = father_ojb.model_name.pluralize
        self_model_name = self.class.model_name
        self.class.base_uri("#{ActiveRequest.configuration.uri}/#{ActiveRequest.configuration.api_version}/")
        response = self.class.get("/#{self_model_name.pluralize}/#{id}/#{father_model_name}.json", headers: self.class.headers)
        return [] unless 200 == response.code
        body = JSON.parse(response.body)
        # TODO era para ser assim mesmo?
        childrens = body["#{self_model_name}/#{father_model_name}"].map { |params| father_ojb.new(params) }
        childrens.each do |ch|
          ch.instance_variable_set("@#{self_model_name}", self)
          ch.instance_variable_set("@#{self_model_name}_id", self.id)
        end
        send("#{many[:association]}=", childrens)
        variable = childrens
      end
      variable
    end
    define_method("#{many[:association]}=") do |many_setter|
      alloc.instance_variable_set("@#{many[:association]}", many_setter)
    end
  end if alloc.has_manys
end
has_many(association, options = nil) click to toggle source
# File lib/active_request/has_many.rb, line 8
def has_many(association, options = nil)
  @has_manys ||= []
  class_name = !options.nil? && !options[:class_name].nil? ? options[:class_name] : (association.to_s.split.map(&:capitalize)*' ').singularize
  @has_manys << { association: association, class_name: class_name }
end
has_manys() click to toggle source
# File lib/active_request/has_many.rb, line 14
def has_manys
  @has_manys
end