class Confection::Controller

The Controller class is used to encapsulate the various methods of invocation that are posible on configuration blocks. It applies those invocations across it’s set of configurations.

Public Class Methods

new(scope, *configs) click to toggle source

Initialize new Controller instance.

@param [Object] scope

@param [Array<Config>] configs

# File lib/confection/controller.rb, line 18
def initialize(scope, *configs)
  @scope   = scope
  @configs = configs
end

Public Instance Methods

call(*args) click to toggle source

Execute the configuration code.

# File lib/confection/controller.rb, line 40
def call(*args)
  result = nil
  each do |config|
    result = config.call(*args)
  end
  result
end
configure() click to toggle source

Special configuration call.

# File lib/confection/controller.rb, line 51
def configure
  result = nil
  each do |config|
    case config.arity
    when 0
      exec
    when 1
      result = config.call(@scope)
    end
  end
  result
end
each(&block) click to toggle source

Iterate over each config.

# File lib/confection/controller.rb, line 26
def each(&block)
  @configs.each(&block)
end
exec(*args) click to toggle source

Evaluate configuration in the context of the caller.

This is the same as calling:

instance_exec(*args, &config)
# File lib/confection/controller.rb, line 71
def exec(*args)
  result = nil
  each do |config|
    if config.respond_to?(:to_proc)
      #@scope.extend(config.dsl) # ?
      result = @scope.instance_exec(*args, &config)
    end
  end
  result
end
inspect() click to toggle source

Inspection string for controller.

# File lib/confection/controller.rb, line 161
def inspect
  "#<#{self.class}##{object_id}>"
end
load(*args)

@deprecated Alias for ‘#main_exec` might be deprecated in future.

Alias for: main_exec
main_exec(*args) click to toggle source

Load config as script code in context of TOPLEVEL.

This is the same as calling:

main = ::TOPLEVEL_BINDING.eval('self')
main.instance_exec(*args, &config)
# File lib/confection/controller.rb, line 90
def main_exec(*args)
  result = nil
  main = ::Kernel.eval('self', ::TOPLEVEL_BINDING)  # ::TOPLEVEL_BINDING.eval('self') [1.9+]
  each do |config|
    if config.respond_to?(:to_proc)
      #main.extend(config.dsl)
      result = main.instance_exec(*args, &config)
    end
  end
  result
end
Also aliased as: load
size() click to toggle source

Number of configs.

# File lib/confection/controller.rb, line 33
def size
  @configs.size
end
text()
Alias for: to_s
to_h() click to toggle source
# File lib/confection/controller.rb, line 141
def to_h
  hsh = {}
  @configs.each do |c|
    hsh.merge!(c.to_h)
  end
  hsh
end
to_proc() click to toggle source

Only applicable to script and block configs, this method converts a set of code configs into a single block ready for execution.

# File lib/confection/controller.rb, line 109
def to_proc
  #properties = ::Confection.properties  # do these even matter here ?
  __configs__ = @configs
  block = Proc.new do |*args|
    result = nil
    #extend dsl  # TODO: extend DSL into instance context ?
    __configs__.each do |config|
      if config.respond_to?(:to_proc)
        result = instance_exec(*args, &config)
      end
    end
    result
  end
end
to_s() click to toggle source

Configurations texts joins together the contents of each configuration separated by two newlines (‘nn`).

# File lib/confection/controller.rb, line 128
def to_s
  txt = []
  @configs.each do |c|
    txt << c.to_s #if c.text
  end
  txt.join("\n\n")
end
Also aliased as: text