Class: Toritori::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/toritori/factory.rb

Overview

Generates module that adds support for objects creation

Instance Method Summary collapse

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

#copyObject



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