Class: Glamazon::Associations::HasMany
- Inherits:
-
Array
- Object
- Array
- Glamazon::Associations::HasMany
show all
- Includes:
- Finder
- Defined in:
- lib/glamazon/associations.rb
Instance Method Summary
collapse
Methods included from Finder
#find, #method_missing
Constructor Details
#initialize(association_type, klass = nil, associated_klass = nil, options = {}) ⇒ HasMany
Returns a new instance of HasMany.
35
36
37
38
39
40
41
|
# File 'lib/glamazon/associations.rb', line 35
def initialize(association_type, klass = nil, associated_klass = nil, options ={})
@callbacks = Hash.new { |h,k| h[k] = [] }
Hash[options.select { |k,v| k.to_s =~ /_add$/}].each { |k,v| @callbacks[k] << options[k] }
@class = klass
@associated_class = associated_klass ? associated_klass.to_s.classify.constantize : association_type.to_s.singularize.classify.constantize
super 0
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Glamazon::Finder
Instance Method Details
#<<(object) ⇒ Object
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/glamazon/associations.rb', line 48
def <<(object)
if object.instance_of?(@associated_class)
return if include?(object)
object.send :"#{@class.class.to_s.downcase}=", @class
@callbacks[:after_add].each { |cb| cb.call @class, object }
super object
else
raise Glamazon::AssociationTypeMismatch.new "Object is of incorrect type. Must be an instance of #{@associated_class}."
end
end
|
#all ⇒ Object
42
43
44
|
# File 'lib/glamazon/associations.rb', line 42
def all
self
end
|
#create(attrs = {}) ⇒ Object
45
46
47
|
# File 'lib/glamazon/associations.rb', line 45
def create(attrs={})
@associated_class.create(attrs).tap { |o| self << o }
end
|