Module: OldApiResource::Associations::ClassMethods
- Defined in:
- lib/old_api_resource/associations.rb
Instance Method Summary collapse
- #association?(assoc) ⇒ Boolean
- #association_class_name(assoc) ⇒ Object
- #clear_associations ⇒ Object
- #scope(name, hsh) ⇒ Object
- #scope?(name) ⇒ Boolean
- #scope_attributes(name) ⇒ Object
- #scopes ⇒ Object
Instance Method Details
#association?(assoc) ⇒ Boolean
87 88 89 90 91 92 |
# File 'lib/old_api_resource/associations.rb', line 87 def association?(assoc) self..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
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..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 ⇒ Object
102 103 104 105 106 |
# File 'lib/old_api_resource/associations.rb', line 102 def clear_associations self..each do |_, val| val.clear end end |
#scope(name, hsh) ⇒ Object
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.[: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
78 79 80 |
# File 'lib/old_api_resource/associations.rb', line 78 def scope?(name) self.[: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.[:scope][name.to_sym] end |
#scopes ⇒ Object
63 64 65 |
# File 'lib/old_api_resource/associations.rb', line 63 def scopes return self.[:scope] end |