Class: Confection::Controller

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/confection/controller.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(scope, *configs) ⇒ Controller

Initialize new Controller instance.

Parameters:

  • scope (Object)
  • configs (Array<Config>)


18
19
20
21
# File 'lib/confection/controller.rb', line 18

def initialize(scope, *configs)
  @scope   = scope
  @configs = configs
end

Instance Method Details

#call(*args) ⇒ Object

Execute the configuration code.



40
41
42
43
44
45
46
# File 'lib/confection/controller.rb', line 40

def call(*args)
  result = nil
  each do |config|
    result = config.call(*args)
  end
  result
end

#configureObject

Special configuration call.



51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object

Iterate over each config.



26
27
28
# File 'lib/confection/controller.rb', line 26

def each(&block)
  @configs.each(&block)
end

#exec(*args) ⇒ Object

Evaluate configuration in the context of the caller.

This is the same as calling:

instance_exec(*args, &config)


71
72
73
74
75
76
77
78
79
80
# 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

#inspectObject

Inspection string for controller.



161
162
163
# File 'lib/confection/controller.rb', line 161

def inspect
  "#<#{self.class}##{object_id}>"
end

#main_exec(*args) ⇒ Object Also known as: load

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)


90
91
92
93
94
95
96
97
98
99
100
# 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

#sizeObject

Number of configs.



33
34
35
# File 'lib/confection/controller.rb', line 33

def size
  @configs.size
end

#to_hObject



141
142
143
144
145
146
147
# File 'lib/confection/controller.rb', line 141

def to_h
  hsh = {}
  @configs.each do |c|
    hsh.merge!(c.to_h)
  end
  hsh
end

#to_procObject

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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_sObject Also known as: text

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



128
129
130
131
132
133
134
# 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