Module: Subroutine::Factory
- Defined in:
- lib/subroutine/factory.rb,
lib/subroutine/factory/config.rb,
lib/subroutine/factory/builder.rb,
lib/subroutine/factory/version.rb,
lib/subroutine/factory/spec_helper.rb
Defined Under Namespace
Modules: SpecHelper
Classes: Builder, Config
Constant Summary
collapse
- RANDOM_FUNCTION_TYPES =
%i[alphanumeric numeric alpha]
- ALPHAS =
("a".."z").to_a
- NUMERICS =
(0..9).to_a
- ALPHANUMERICS =
NUMERICS + ALPHAS
- VERSION =
"0.1.9"
- @@configs =
{}
- @@sequence =
0
Class Method Summary
collapse
Class Method Details
.build_random(length: 8, type: :alphanumeric) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# 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) ⇒ Object
47
48
49
50
|
# File 'lib/subroutine/factory.rb', line 47
def self.builder(name, *args)
config = get_config!(name)
::Subroutine::Factory::Builder.new(config, *args)
end
|
.configs ⇒ Object
16
17
18
|
# File 'lib/subroutine/factory.rb', line 16
def self.configs
@@configs
end
|
.create(name, *args) ⇒ Object
39
40
41
|
# File 'lib/subroutine/factory.rb', line 39
def self.create(name, *args)
builder(name, *args).execute!
end
|
.define(name, options = {}, &block) ⇒ Object
20
21
22
23
24
25
26
|
# 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) ⇒ Object
28
29
30
|
# File 'lib/subroutine/factory.rb', line 28
def self.get_config(name)
@@configs[name.to_sym]
end
|
.get_config!(name) ⇒ Object
32
33
34
35
36
37
|
# 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
|
43
44
45
|
# File 'lib/subroutine/factory.rb', line 43
def self.inputs(name, *args)
builder(name, *args).inputs
end
|
.random(random_options = {}, &lambda) ⇒ Object
63
64
65
66
67
68
69
70
71
72
|
# 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) ⇒ Object
52
53
54
55
56
57
58
59
60
61
|
# 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) ⇒ Object
95
96
97
|
# File 'lib/subroutine/factory.rb', line 95
def self.set_sequence(n)
@@sequence = n
end
|