Class: Confection::Config

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#blockObject

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

#profileObject

The name of the profile to which this configuration belongs.



38
39
40
# File 'lib/confection/config.rb', line 38

def profile
  @profile
end

#toolObject

The name of tool being configured.



24
25
26
# File 'lib/confection/config.rb', line 24

def tool
  @tool
end

Instance Method Details

#arityFixnum

The arity of the configuration procedure.

Returns:

  • (Fixnum)

    number of arguments



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.

Parameters:

  • alt (Hash) (defaults to: {})

    Alternate values for configuration attributes.

Returns:



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

#inspectObject

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_hHash

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

Returns:

  • (Hash)


125
126
127
# File 'lib/confection/config.rb', line 125

def to_h
  (@value || HashBuilder.new(&@block)).to_h
end

#to_procObject

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_sString Also known as: text

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

Returns:

  • (String)


134
135
136
# File 'lib/confection/config.rb', line 134

def to_s
  (@value || call).to_s
end

#valueObject

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.

Parameters:

  • value (Object)

    The configuration value.



62
63
64
65
# File 'lib/confection/config.rb', line 62

def value=(value)
  @block = nil
  @value = value
end