class MR::Factory::ModelFactory

Attributes

config[R]
model_class[R]
record_class[R]

Public Class Methods

new(model_class, record_class, &block) click to toggle source
# File lib/mr/factory/model_factory.rb, line 14
def initialize(model_class, record_class, &block)
  @model_class  = model_class
  @record_class = record_class
  @config       = Config.new(@record_class)
  self.instance_eval(&block) if block

  @record_factory = MR::Factory::RecordFactory.new(record_class)
end

Public Instance Methods

instance(args = nil)
Alias for: model
instance_stack(args = nil)
Alias for: stack
model(args = nil) click to toggle source
# File lib/mr/factory/model_factory.rb, line 23
def model(args = nil)
  @model_class.new(@record_factory.record).tap do |model|
    self.config.apply_args(model, args || {})
  end
end
Also aliased as: instance
saved_dependencies_stack(args = nil) click to toggle source
# File lib/mr/factory/model_factory.rb, line 54
def saved_dependencies_stack(args = nil)
  self.stack(args).tap(&:create_deps)
end
Also aliased as: saved_deps_stack
saved_dependencies_stack_model(args = nil) click to toggle source
# File lib/mr/factory/model_factory.rb, line 59
def saved_dependencies_stack_model(args = nil)
  self.saved_deps_stack(args).model
end
Also aliased as: saved_deps_stack_model
saved_deps_stack(args = nil)
saved_deps_stack_model(args = nil)
saved_instance(args = nil)
Alias for: saved_model
saved_model(args = nil) click to toggle source
# File lib/mr/factory/model_factory.rb, line 30
def saved_model(args = nil)
  model = self.model(args).tap(&:save)
  model.record.reset_save_called if model.record.kind_of?(MR::FakeRecord)
  model
end
Also aliased as: saved_instance
saved_stack(args = nil) click to toggle source
# File lib/mr/factory/model_factory.rb, line 46
def saved_stack(args = nil)
  self.stack(args).tap(&:create)
end
saved_stack_model(args = nil) click to toggle source
# File lib/mr/factory/model_factory.rb, line 50
def saved_stack_model(args = nil)
  self.saved_stack(args).model
end
stack(args = nil) click to toggle source
# File lib/mr/factory/model_factory.rb, line 37
def stack(args = nil)
  MR::Factory::ModelStack.new(self.model(args), self.config)
end
Also aliased as: instance_stack
stack_model(args = nil) click to toggle source
# File lib/mr/factory/model_factory.rb, line 42
def stack_model(args = nil)
  self.stack(args).model
end

Private Instance Methods

default_args(&block) click to toggle source
# File lib/mr/factory/model_factory.rb, line 72
def default_args(&block)
  self.config.set_default_args(&block)
end
default_association(association_name, factory, options = nil) click to toggle source
# File lib/mr/factory/model_factory.rb, line 66
def default_association(association_name, factory, options = nil)
  self.config.add_association_factory(association_name, factory, options)
rescue NoAssociationError => exception
  raise exception.class, exception.message, caller
end