Module: Mongoid::Relations::Builders::ClassMethods
- Defined in:
- lib/mongoid/relations/builders.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#builder(name, metadata) ⇒ Class
Defines a builder method for an embeds_one relation.
-
#creator(name) ⇒ Class
Defines a creator method for an embeds_one relation.
Instance Method Details
#builder(name, metadata) ⇒ Class
Defines a builder method for an embeds_one relation. This is defined as #build_name.
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/mongoid/relations/builders.rb', line 48 def builder(name, ) tap do define_method("build_#{name}") do |*args| attributes = args.first || {} = args.size > 1 ? args[1] : {} document = Factory.build(.klass, attributes, ) _building do send("#{name}=", document) end end end end |
#creator(name) ⇒ Class
Defines a creator method for an embeds_one relation. This is defined as #create_name. After the object is built it will immediately save.
73 74 75 76 77 78 79 |
# File 'lib/mongoid/relations/builders.rb', line 73 def creator(name) tap do define_method("create_#{name}") do |*args| send("build_#{name}", *args).tap { |doc| doc.save } end end end |