class Manufacturable::Builder

Attributes

args[R]
block[R]
key[R]
kwargs[R]
type[R]

Public Class Methods

build(*args, **kwargs, &block) click to toggle source
# File lib/manufacturable/builder.rb, line 5
def self.build(*args, **kwargs, &block)
  self.new(*args, **kwargs, &block).build
end
build_all(*args, **kwargs, &block) click to toggle source
# File lib/manufacturable/builder.rb, line 13
def self.build_all(*args, **kwargs, &block)
  self.new(*args, **kwargs, &block).build_all
end
build_one(*args, **kwargs, &block) click to toggle source
# File lib/manufacturable/builder.rb, line 9
def self.build_one(*args, **kwargs, &block)
  self.new(*args, **kwargs, &block).build_one
end
builds?(*args, **kwargs, &block) click to toggle source
# File lib/manufacturable/builder.rb, line 17
def self.builds?(*args, **kwargs, &block)
  self.new(*args, **kwargs, &block).builds?
end
new(*args, **kwargs, &block) click to toggle source
# File lib/manufacturable/builder.rb, line 41
def initialize(*args, **kwargs, &block)
  @type, @key, *@args = args
  @kwargs, @block = kwargs, block
end

Public Instance Methods

build() click to toggle source
# File lib/manufacturable/builder.rb, line 21
def build
  return_first? ? instances.first : instances
end
build_all() click to toggle source
# File lib/manufacturable/builder.rb, line 29
def build_all
  instances
end
build_one() click to toggle source
# File lib/manufacturable/builder.rb, line 25
def build_one
  last_instance
end
builds?() click to toggle source
# File lib/manufacturable/builder.rb, line 33
def builds?
  Registrar.registered_keys(type).include?(key)
end

Private Instance Methods

instances() click to toggle source
# File lib/manufacturable/builder.rb, line 50
def instances
  @instances ||= klasses.map do |klass|
    klass.new(*args, **kwargs_with_key).tap { |instance| block&.call(instance) }
  end
end
klasses() click to toggle source
# File lib/manufacturable/builder.rb, line 60
def klasses
  Registrar.get(type, key).to_a
end
kwargs_with_key() click to toggle source
# File lib/manufacturable/builder.rb, line 64
def kwargs_with_key
  kwargs.merge(manufacturable_item_key: key)
end
last_instance() click to toggle source
# File lib/manufacturable/builder.rb, line 56
def last_instance
  last_klass&.new(*args, **kwargs_with_key)&.tap { |instance| block&.call(instance) }
end
last_klass() click to toggle source
# File lib/manufacturable/builder.rb, line 68
def last_klass
  klasses.last
end
return_first?() click to toggle source
# File lib/manufacturable/builder.rb, line 46
def return_first?
  instances.size < 2
end