module Subroutine::Factory

Constants

ALPHANUMERICS
ALPHAS
NUMERICS
RANDOM_FUNCTION_TYPES
VERSION

Public Class Methods

build_random(length: 8, type: :alphanumeric) click to toggle source
# File lib/subroutine/factory.rb, line 78
def self.build_random(length: 8, type: :alphanumeric)
  raise ArgumentError, ":type must be one of #{RANDOM_FUNCTION_TYPES.inspect}" unless RANDOM_FUNCTION_TYPES.include?(type.to_sym)

  source = (
    case type.to_sym
    when :alphanumeric then ALPHANUMERICS
    when :numeric then NUMERICS
    when :alpha then ALPHAS
    end
  )

  length.times.inject(+"") do |memo, _i|
    x = source.sample.to_s
    memo << x
  end
end
builder(name, *args) click to toggle source
# File lib/subroutine/factory.rb, line 47
def self.builder(name, *args)
  config = get_config!(name)
  ::Subroutine::Factory::Builder.new(config, *args)
end
configs() click to toggle source
# File lib/subroutine/factory.rb, line 16
def self.configs
  @@configs
end
create(name, *args) click to toggle source
# File lib/subroutine/factory.rb, line 39
def self.create(name, *args)
  builder(name, *args).execute!
end
define(name, options = {}, &block) click to toggle source
# File lib/subroutine/factory.rb, line 20
def self.define(name, options = {}, &block)
  config = ::Subroutine::Factory::Config.new(options)
  @@configs[name.to_sym] = config
  config.instance_eval(&block) if block_given?
  config.validate!
  config
end
get_config(name) click to toggle source
# File lib/subroutine/factory.rb, line 28
def self.get_config(name)
  @@configs[name.to_sym]
end
get_config!(name) click to toggle source
# File lib/subroutine/factory.rb, line 32
def self.get_config!(name)
  config = get_config(name)
  raise "Unknown Subroutine::Factory `#{name}`" unless config

  config
end
inputs(name, *args) click to toggle source
# File lib/subroutine/factory.rb, line 43
def self.inputs(name, *args)
  builder(name, *args).inputs
end
random(random_options = {}, &lambda) click to toggle source
# File lib/subroutine/factory.rb, line 63
def self.random(random_options = {}, &lambda)
  if block_given?
    proc do |*options|
      x = build_random(random_options)
      lambda.call(*[x, *options].compact)
    end
  else
    build_random(random_options)
  end
end
sequence(&lambda) click to toggle source
# File lib/subroutine/factory.rb, line 52
def self.sequence(&lambda)
  if block_given?
    proc do |*options|
      @@sequence += 1
      lambda.call(*[@@sequence, *options].compact)
    end
  else
    @@sequence += 1
  end
end
set_sequence(n) click to toggle source
# File lib/subroutine/factory.rb, line 95
def self.set_sequence(n)
  @@sequence = n
end