Class: PairKit::DslFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/pair_kit/dsl_factory.rb,
lib/pair_kit/dsl_factory/builder.rb,
lib/pair_kit/dsl_factory/version.rb,
lib/pair_kit/dsl_factory/wrapper.rb,
lib/pair_kit/dsl_factory/context_dsl.rb

Defined Under Namespace

Classes: Builder, ContextDsl, Wrapper

Constant Summary collapse

VERSION =
'0.0.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ DslFactory

Returns a new instance of DslFactory.



10
11
12
13
# File 'lib/pair_kit/dsl_factory.rb', line 10

def initialize(&block)
  @builders = {}
  instance_exec(&block) if block
end

Instance Attribute Details

#buildersObject (readonly)

Returns the value of attribute builders.



8
9
10
# File 'lib/pair_kit/dsl_factory.rb', line 8

def builders
  @builders
end

#default_builderObject



29
30
31
# File 'lib/pair_kit/dsl_factory.rb', line 29

def default_builder
  builders.keys.first
end

Instance Method Details

#branch(&block) ⇒ Object



33
34
35
36
37
# File 'lib/pair_kit/dsl_factory.rb', line 33

def branch(&block)
  self.class.new
      .tap { |x| builders.each { |k, v| x.builders[k] = Class.new(v) } }
      .tap { |x| x.instance_exec(&block) if block }
end

#build(thing, context = nil, **options, &block) ⇒ Object



24
25
26
27
# File 'lib/pair_kit/dsl_factory.rb', line 24

def build(thing, context = nil, **options, &block)
  ContextDsl.new(self, context).call(thing, **options, &block)
  thing
end

#configure_builder(name, *modules, &block) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/pair_kit/dsl_factory.rb', line 15

def configure_builder(name, *modules, &block)
  (@builders[name.to_sym] ||= Class.new(Builder))
    .tap { |builder| modules.flatten.each { |mod| builder.class_exec { include mod }}}
    .tap { |builder| builder.class_exec(&block) if block }

  @default_builder = @builders.keys.first if @builders.size == 1
  self
end