module OldApiResource::Associations::ClassMethods

Public Instance Methods

association?(assoc) click to toggle source
# File lib/old_api_resource/associations.rb, line 87
def association?(assoc)
  self.related_objects.any? do |key, value|
    next if key.to_s == "scope"
    value.detect { |k,v| k.to_sym == assoc.to_sym }
  end
end
association_class_name(assoc) click to toggle source
# File lib/old_api_resource/associations.rb, line 94
def association_class_name(assoc)
  raise ArgumentError, "#{assoc} is not a valid association of #{self}" unless self.association?(assoc)
  result = self.related_objects.detect do |key,value|
    ret = value.detect{|k,v| k.to_sym == assoc.to_sym }
    return self.find_namespaced_class_name(ret[1]) if ret
  end
end
clear_associations() click to toggle source
# File lib/old_api_resource/associations.rb, line 102
def clear_associations
  self.related_objects.each do |_, val|
    val.clear
  end
end
scope(name, hsh) click to toggle source
# File lib/old_api_resource/associations.rb, line 67
      def scope(name, hsh)
        raise ArgumentError, "Expecting an attributes hash given #{hsh.inspect}" unless hsh.is_a?(Hash)
        self.related_objects[:scope][name.to_sym] = hsh
        # we also need to define a class method for each scope
        self.instance_eval <<-EOE, __FILE__, __LINE__ + 1
          def #{name}(opts = {})
            return OldApiResource::Associations::ResourceScope.new(self, :#{name}, opts)
          end
        EOE
      end
scope?(name) click to toggle source
# File lib/old_api_resource/associations.rb, line 78
def scope?(name)
  self.related_objects[:scope][name.to_sym].present?
end
scope_attributes(name) click to toggle source
# File lib/old_api_resource/associations.rb, line 82
def scope_attributes(name)
  raise "No such scope #{name}" unless self.scope?(name)
  self.related_objects[:scope][name.to_sym]
end
scopes() click to toggle source
# File lib/old_api_resource/associations.rb, line 63
def scopes
  return self.related_objects[:scope]
end

Protected Instance Methods

define_association_as_attribute(assoc_type, assoc_name) click to toggle source
# File lib/old_api_resource/associations.rb, line 109
        def define_association_as_attribute(assoc_type, assoc_name)
          define_attributes assoc_name
          case assoc_type.to_sym
            when :has_many
              self.class_eval <<-EOE, __FILE__, __LINE__ + 1
                def #{assoc_name}
                  self.attributes[:#{assoc_name}] ||= MultiObjectProxy.new(self.association_class_name('#{assoc_name}'), nil)
                end
              EOE
            else
              self.class_eval <<-EOE, __FILE__, __LINE__ + 1
                def #{assoc_name}
                  self.attributes[:#{assoc_name}] ||= SingleObjectProxy.new(self.association_class_name('#{assoc_name}'), nil)
                end
              EOE
          end
          # Always define the setter the same
          self.class_eval <<-EOE, __FILE__, __LINE__ + 1
            def #{assoc_name}=(val)
              #{assoc_name}_will_change! unless self.#{assoc_name}.internal_object == val
              self.#{assoc_name}.internal_object = val
            end
          EOE
        end
find_namespaced_class_name(klass) click to toggle source
# File lib/old_api_resource/associations.rb, line 134
def find_namespaced_class_name(klass)
  # return the name if it is itself namespaced
  return klass if klass =~ /::/
  ancestors = self.name.split("::")
  if ancestors.size > 1
    receiver = Object
    namespaces = ancestors[0..-2].collect do |mod|
      receiver = receiver.const_get(mod)
    end
    if namespace = namespaces.reverse.detect{|ns| ns.const_defined?(klass, false)}
      return namespace.const_get(klass).name
    end
  end

  return klass
end