Module: Mongoid::Relations::Accessors::ClassMethods
- Defined in:
- lib/mongoid/relations/accessors.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#getter(name, metadata) ⇒ Class
Defines the getter for the relation.
-
#setter(name, metadata) ⇒ Class
Defines the setter for the relation.
Instance Method Details
#getter(name, metadata) ⇒ Class
Defines the getter for the relation. Nothing too special here: just return the instance variable for the relation if it exists or build the thing.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/mongoid/relations/accessors.rb', line 92 def getter(name, ) tap do define_method(name) do |*args| reload, variable = args.first, "@#{name}" if instance_variable_defined?(variable) && !reload instance_variable_get(variable) else building do build(name, attributes[.key], , :loading => true) end end end end end |
#setter(name, metadata) ⇒ Class
Defines the setter for the relation. This does a few things based on some conditions. If there is an existing association, a target substitution will take place, otherwise a new relation will be created with the supplied target.
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/mongoid/relations/accessors.rb', line 121 def setter(name, ) tap do define_method("#{name}=") do |object| if relation_exists?(name) || .many? || (object.blank? && send(name)) set_relation(name, send(name).substitute(object.substitutable)) else build(name, object.substitutable, ) end end end end |