Class: Toritori::Factory
- Inherits:
-
Object
- Object
- Toritori::Factory
- Defined in:
- lib/toritori/factory.rb
Overview
Generates module that adds support for objects creation
Instance Method Summary collapse
- #copy ⇒ Object
- #create(*args, **kwargs, &block) ⇒ Object
-
#initialize(name, base_class: nil, creation_method: :new) ⇒ Factory
constructor
A new instance of Factory.
- #subclass(produces: nil, creation_method: @creation_method, &block) ⇒ Object
Constructor Details
#initialize(name, base_class: nil, creation_method: :new) ⇒ Factory
10 11 12 13 14 |
# File 'lib/toritori/factory.rb', line 10 def initialize(name, base_class: nil, creation_method: :new) @name = name @base_class = base_class @creation_method = creation_method end |
Instance Method Details
#copy ⇒ Object
6 7 8 |
# File 'lib/toritori/factory.rb', line 6 def copy self.class.new(@name, base_class: @base_class, creation_method: @creation_method) end |
#create(*args, **kwargs, &block) ⇒ Object
22 23 24 25 26 |
# File 'lib/toritori/factory.rb', line 22 def create(*args, **kwargs, &block) return @base_class.new(*args, **kwargs, &block) if @creation_method == :new @base_class.public_send(@creation_method, *args, **kwargs, &block) end |
#subclass(produces: nil, creation_method: @creation_method, &block) ⇒ Object
16 17 18 19 20 |
# File 'lib/toritori/factory.rb', line 16 def subclass(produces: nil, creation_method: @creation_method, &block) @base_class = check_base_class(produces) || @base_class @base_class = Class.new(@base_class, &block) if block @creation_method = creation_method end |