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

VERSION =
"0.1.0"
@@configs =
{}
@@sequence =
1

Class Method Summary collapse

Class Method Details

.configsObject



13
14
15
# File 'lib/subroutine/factory.rb', line 13

def self.configs
  @@configs
end

.create(name, *args) ⇒ Object



35
36
37
38
39
# File 'lib/subroutine/factory.rb', line 35

def self.create(name, *args)
  config = get_config!(name)
  builder = ::Subroutine::Factory::Builder.new(config, *args)
  builder.execute!
end

.define(name, options = {}, &block) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/subroutine/factory.rb', line 17

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



25
26
27
# File 'lib/subroutine/factory.rb', line 25

def self.get_config(name)
  @@configs[name.to_sym]
end

.get_config!(name) ⇒ Object



29
30
31
32
33
# File 'lib/subroutine/factory.rb', line 29

def self.get_config!(name)
  config = get_config(name)
  raise "Unknown Subroutine::Factory `#{name}`" unless config
  return config
end

.sequence(&lambda) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/subroutine/factory.rb', line 41

def self.sequence(&lambda)
  if block_given?
    Proc.new{
      @@sequence += 1
      lambda.call(@@sequence)
    }
  else
    @@sequence += 1
  end
end