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
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
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
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
Iterate over each config.
# File lib/confection/controller.rb, line 26 def each(&block) @configs.each(&block) end
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
Inspection string for controller.
# File lib/confection/controller.rb, line 161 def inspect "#<#{self.class}##{object_id}>" end
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
Number of configs.
# File lib/confection/controller.rb, line 33 def size @configs.size end
# File lib/confection/controller.rb, line 141 def to_h hsh = {} @configs.each do |c| hsh.merge!(c.to_h) end hsh end
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
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