Module: Mongoid::Relations::Builders::ClassMethods

Defined in:
lib/mongoid/relations/builders.rb

Overview

:nodoc:

Since:

  • 2.0.0.rc.1

Instance Method Summary collapse

Instance Method Details

#builder(name, metadata) ⇒ Class

Defines a builder method for an embeds_one relation. This is defined as #build_name.

Examples:

Person.builder("name")

Parameters:

Returns:

  • (Class)

    The class being set up.

Since:

  • 2.0.0.rc.1



48
49
50
51
52
53
54
55
# File 'lib/mongoid/relations/builders.rb', line 48

def builder(name, )
  tap do
    define_method("build_#{name}") do |*args|
      document = Factory.build(.klass, args.first || {})
      send("#{name}=", document, :binding => true)
    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.

Examples:

Person.creator("name")

Parameters:

Returns:

  • (Class)

    The class being set up.

Since:

  • 2.0.0.rc.1



69
70
71
72
73
74
75
# File 'lib/mongoid/relations/builders.rb', line 69

def creator(name)
  tap do
    define_method("create_#{name}") do |*args|
      send("build_#{name}", *args).tap(&:save)
    end
  end
end