Class: MR::Factory::ModelFactory
- Inherits:
-
Object
- Object
- MR::Factory::ModelFactory
- Defined in:
- lib/mr/factory/model_factory.rb
Defined Under Namespace
Classes: Config
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#record_class ⇒ Object
readonly
Returns the value of attribute record_class.
Instance Method Summary collapse
-
#initialize(model_class, record_class, &block) ⇒ ModelFactory
constructor
A new instance of ModelFactory.
- #model(args = nil) ⇒ Object (also: #instance)
- #saved_dependencies_stack(args = nil) ⇒ Object (also: #saved_deps_stack)
- #saved_dependencies_stack_model(args = nil) ⇒ Object (also: #saved_deps_stack_model)
- #saved_model(args = nil) ⇒ Object (also: #saved_instance)
- #saved_stack(args = nil) ⇒ Object
- #saved_stack_model(args = nil) ⇒ Object
- #stack(args = nil) ⇒ Object (also: #instance_stack)
- #stack_model(args = nil) ⇒ Object
Constructor Details
#initialize(model_class, record_class, &block) ⇒ ModelFactory
Returns a new instance of ModelFactory.
14 15 16 17 18 19 20 21 |
# 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 |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
12 13 14 |
# File 'lib/mr/factory/model_factory.rb', line 12 def config @config end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
12 13 14 |
# File 'lib/mr/factory/model_factory.rb', line 12 def model_class @model_class end |
#record_class ⇒ Object (readonly)
Returns the value of attribute record_class.
12 13 14 |
# File 'lib/mr/factory/model_factory.rb', line 12 def record_class @record_class end |
Instance Method Details
#model(args = nil) ⇒ Object Also known as: instance
23 24 25 26 27 |
# 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 |
#saved_dependencies_stack(args = nil) ⇒ Object Also known as: saved_deps_stack
54 55 56 |
# File 'lib/mr/factory/model_factory.rb', line 54 def saved_dependencies_stack(args = nil) self.stack(args).tap(&:create_deps) end |
#saved_dependencies_stack_model(args = nil) ⇒ Object Also known as: saved_deps_stack_model
59 60 61 |
# File 'lib/mr/factory/model_factory.rb', line 59 def saved_dependencies_stack_model(args = nil) self.saved_deps_stack(args).model end |
#saved_model(args = nil) ⇒ Object Also known as: saved_instance
30 31 32 33 34 |
# 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 |
#saved_stack(args = nil) ⇒ Object
46 47 48 |
# 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) ⇒ Object
50 51 52 |
# File 'lib/mr/factory/model_factory.rb', line 50 def saved_stack_model(args = nil) self.saved_stack(args).model end |
#stack(args = nil) ⇒ Object Also known as: instance_stack
37 38 39 |
# File 'lib/mr/factory/model_factory.rb', line 37 def stack(args = nil) MR::Factory::ModelStack.new(self.model(args), self.config) end |
#stack_model(args = nil) ⇒ Object
42 43 44 |
# File 'lib/mr/factory/model_factory.rb', line 42 def stack_model(args = nil) self.stack(args).model end |