Class: Confection::Config
- Inherits:
-
Object
- Object
- Confection::Config
- Defined in:
- lib/confection/config.rb
Overview
Config encapsulates a single configuration entry as defined in a project’s configuration file.
Instance Attribute Summary collapse
-
#block ⇒ Object
Most configuration are scripted.
-
#profile ⇒ Object
The name of the profile to which this configuration belongs.
-
#tool ⇒ Object
The name of tool being configured.
Instance Method Summary collapse
-
#arity ⇒ Fixnum
The arity of the configuration procedure.
-
#call(*args) ⇒ Object
Call the procedure.
-
#copy(alt = {}) ⇒ Config
Copy the configuration with alterations.
-
#initialize(tool, profile, context, value, &block) ⇒ Config
constructor
Initialize Config instance.
-
#inspect ⇒ Object
Ruby 1.9 defines #inspect as #to_s, ugh.
-
#to_h ⇒ Hash
Return the value or procedure in the form of a Hash.
-
#to_proc ⇒ Object
Convert the underlying procedure into an ‘instance_exec` procedure.
-
#to_s ⇒ String
(also: #text)
Return the value or procedure in the form of a String.
-
#value ⇒ Object
Some configuration are simple values.
-
#value=(value) ⇒ Object
Set the configuration value.
Constructor Details
#initialize(tool, profile, context, value, &block) ⇒ Config
Initialize Config instance. Config instances are per-configuration, which means they are associated with one and only one config entry.
12 13 14 15 16 17 18 19 |
# File 'lib/confection/config.rb', line 12 def initialize(tool, profile, context, value, &block) self.tool = tool self.profile = profile self.value = value self.block = block if block @context = context end |
Instance Attribute Details
#block ⇒ Object
Most configuration are scripted. In thos cases the ‘@block` attributes holds the Proc instance, otherwise it is `nil`.
72 73 74 |
# File 'lib/confection/config.rb', line 72 def block @block end |
#profile ⇒ Object
The name of the profile to which this configuration belongs.
38 39 40 |
# File 'lib/confection/config.rb', line 38 def profile @profile end |
#tool ⇒ Object
The name of tool being configured.
24 25 26 |
# File 'lib/confection/config.rb', line 24 def tool @tool end |
Instance Method Details
#arity ⇒ Fixnum
The arity of the configuration procedure.
90 91 92 |
# File 'lib/confection/config.rb', line 90 def arity @block ? @block.arity : 0 end |
#call(*args) ⇒ Object
Call the procedure. Configuration procedures are evaluated in the scope of a per-configuration file context instance, which is extended by the DSL evaluation context.
99 100 101 102 |
# File 'lib/confection/config.rb', line 99 def call(*args) #@value || @block.call(*args) @value || @context.instance_exec(*args, &block) end |
#copy(alt = {}) ⇒ Config
Copy the configuration with alterations.
153 154 155 156 157 158 159 |
# File 'lib/confection/config.rb', line 153 def copy(alt={}) copy = dup alt.each do |k,v| copy.__send__("#{k}=", v) end copy end |
#inspect ⇒ Object
Ruby 1.9 defines #inspect as #to_s, ugh.
164 165 166 |
# File 'lib/confection/config.rb', line 164 def inspect "#<#{self.class.name}:#{object_id} @tool=%s @profile=%s>" % [tool.inspect, profile.inspect] end |
#to_h ⇒ Hash
Return the value or procedure in the form of a Hash.
125 126 127 |
# File 'lib/confection/config.rb', line 125 def to_h (@value || HashBuilder.new(&@block)).to_h end |
#to_proc ⇒ Object
Convert the underlying procedure into an ‘instance_exec` procedure. This allows the procedure to be evaluated in any scope that it is be needed.
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/confection/config.rb', line 109 def to_proc if value = @value lambda{ value } else block = @block lambda do |*args| instance_exec(*args, &block) end end end |
#to_s ⇒ String Also known as: text
Return the value or procedure in the form of a String.
134 135 136 |
# File 'lib/confection/config.rb', line 134 def to_s (@value || call).to_s end |
#value ⇒ Object
Some configuration are simple values. In those cases the ‘@value` attributes holds the object, otherwise it is `nil`.
52 53 54 |
# File 'lib/confection/config.rb', line 52 def value @value end |
#value=(value) ⇒ Object
Set the configuration value.
62 63 64 65 |
# File 'lib/confection/config.rb', line 62 def value=(value) @block = nil @value = value end |