Module: OldApiResource::Associations::ClassMethods

Defined in:
lib/old_api_resource/associations.rb

Instance Method Summary collapse

Instance Method Details

#association?(assoc) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
# 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) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
# 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_associationsObject



102
103
104
105
106
# 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) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/old_api_resource/associations.rb', line 78

def scope?(name)
  self.related_objects[:scope][name.to_sym].present?
end

#scope_attributes(name) ⇒ Object



82
83
84
85
# 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

#scopesObject



63
64
65
# File 'lib/old_api_resource/associations.rb', line 63

def scopes
  return self.related_objects[:scope]
end