class Confection::Config

Config encapsulates a single configuration entry as defined in a project’s configuration file.

Attributes

block[R]

Most configuration are scripted. In thos cases the ‘@block` attributes holds the Proc instance, otherwise it is `nil`.

profile[R]

The name of the profile to which this configuration belongs.

tool[R]

The name of tool being configured.

Public Class Methods

new(tool, profile, context, value, &block) click to toggle source

Initialize Config instance. Config instances are per-configuration, which means they are associated with one and only one config entry.

# 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

Public Instance Methods

arity() click to toggle source

The arity of the configuration procedure.

@return [Fixnum] number of arguments

# File lib/confection/config.rb, line 90
def arity
  @block ? @block.arity : 0
end
block=(proc) click to toggle source

Set the configuration procedure.

@param [Proc] procedure

The configuration procedure.
# File lib/confection/config.rb, line 80
def block=(proc)
  @value = nil
  @block = proc.to_proc
end
call(*args) click to toggle source

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.

# File lib/confection/config.rb, line 99
def call(*args)
  #@value || @block.call(*args)
  @value || @context.instance_exec(*args, &block)
end
copy(alt={}) click to toggle source

Copy the configuration with alterations.

@param [Hash] alt

Alternate values for configuration attributes.

@return [Config] copied config

# 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() click to toggle source

Ruby 1.9 defines inspect as to_s, ugh.

# File lib/confection/config.rb, line 164
def inspect
  "#<#{self.class.name}:#{object_id} @tool=%s @profile=%s>" % [tool.inspect, profile.inspect]
end
profile=(name) click to toggle source

Change the profile name.

# File lib/confection/config.rb, line 43
def profile=(name)
  @profile = name.to_sym if name
end
text()

Alias for to_s.

@todo Should this alias be deprecated?

Alias for: to_s
to_h() click to toggle source

Return the value or procedure in the form of a Hash.

@return [Hash]

# File lib/confection/config.rb, line 125
def to_h
  (@value || HashBuilder.new(&@block)).to_h
end
to_proc() click to toggle source

Convert the underlying procedure into an ‘instance_exec` procedure. This allows the procedure to be evaluated in any scope that it is be needed.

# 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() click to toggle source

Return the value or procedure in the form of a String.

@return [String]

# File lib/confection/config.rb, line 134
def to_s
  (@value || call).to_s
end
Also aliased as: text
tool=(name) click to toggle source

Change the tool name. Note, this will rarely be used since, generally speaking, configurations tend to be very tool specific.

# File lib/confection/config.rb, line 31
def tool=(name)
  @tool = name.to_sym
end
value() click to toggle source

Some configuration are simple values. In those cases the ‘@value` attributes holds the object, otherwise it is `nil`.

# File lib/confection/config.rb, line 52
def value
  @value
end
value=(value) click to toggle source

Set the configuration value.

@param [Object] value

The configuration value.
# File lib/confection/config.rb, line 62
def value=(value)
  @block = nil
  @value = value
end