Class: Courtier::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/courtier/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, properties = {}, &block) ⇒ Config

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

Parameters:

  • tool (#to_sym)

    The tool’s name.

  • profile (#to_sym, nil)

    Profile name, or nil.

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

    Any additional properties associated with the config entry.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/courtier/config.rb', line 21

def initialize(tool, properties={}, &block)
  @property = {:profile=>'default'}

  @property[:command] = tool.to_s
  @property[:feature] = tool.to_s

  @block = block

  properties.each do |k, v|
    property(k,v)
  end
end

Instance Attribute Details

#blockObject (readonly)

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



96
97
98
# File 'lib/courtier/config.rb', line 96

def block
  @block
end

Instance Method Details

#apply?Boolean

Does the configuration apply?

Returns:

  • (Boolean)


241
242
243
244
245
# File 'lib/courtier/config.rb', line 241

def apply?()
  return false unless command? if command
  return false unless profile? if profile
  return true
end

#arityFixnum

The arity of the configuration procedure.

Returns:

  • (Fixnum)

    number of arguments



110
111
112
# File 'lib/courtier/config.rb', line 110

def arity
  @block ? @block.arity : 0
end

#call(*args) ⇒ Object

Call the configuration procedure.



128
129
130
# File 'lib/courtier/config.rb', line 128

def call(*args)
  block.call(*args) if block
end

#commandObject Also known as: tool

The name of command being configured.



60
61
62
# File 'lib/courtier/config.rb', line 60

def command
  @property[:command]
end

#command?(command = Courtier.current_command) ⇒ Boolean

Does the given ‘command` match the config’s command?

Returns:

  • (Boolean)


203
204
205
# File 'lib/courtier/config.rb', line 203

def command?(command=Courtier.current_command)
  self.command == command.to_s
end

#copy(alt = {}) ⇒ Config

Copy the configuration with alterations.

Parameters:

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

    Alternate values for configuration attributes.

Returns:



156
157
158
159
160
161
162
163
# File 'lib/courtier/config.rb', line 156

def copy(alt={})
  tool = @property[:feature] || @property[:command]
  copy = self.class.new(tool, @property.dup, &@block)
  alt.each do |k,v|
    copy.property(k, v)
  end
  copy
end

#featureObject

The feature being configured.



53
54
55
# File 'lib/courtier/config.rb', line 53

def feature
  @property[:feature]
end

#feature?(feature = Courtier.current_feature) ⇒ Boolean

Does the given ‘feature` match the config’s feature?

Returns:

  • (Boolean)


194
195
196
# File 'lib/courtier/config.rb', line 194

def feature?(feature=Courtier.current_feature)
  self.feature == feature.to_s
end

#fromObject

The library from which this configuration derives. This is a shortcut for ‘property(:from)`.



80
81
82
# File 'lib/courtier/config.rb', line 80

def from
  @property[:from]
end

#match?(*args) ⇒ Boolean

Match config against tool and/or profile names.

Returns:

  • (Boolean)


170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/courtier/config.rb', line 170

def match?(*args)
  props = Hash === args.last ? args.pop : {}

  if tool = args.shift
    props[:command] = tool.to_s
    props[:feature] = tool.to_s
  end

  if props[:profile]
    props[:profile] = (props[:profile] || :default).to_s
  end

  props.each do |k,v|
    return false unless property(k) == v
  end

  return true
end

#onload?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/courtier/config.rb', line 87

def onload?
  @property[:onload]
end

#profileObject

The name of the profile to which this configuration belongs.



72
73
74
# File 'lib/courtier/config.rb', line 72

def profile
  @property[:profile]
end

#profile?(profile = Courtier.current_profile) ⇒ Boolean

Does the given ‘profile` match the config’s profile?

Returns:

  • (Boolean)


212
213
214
# File 'lib/courtier/config.rb', line 212

def profile?(profile=Courtier.current_profile)
  self.profile == (profile || :default).to_s
end

#property(name, value = ArgumentError) ⇒ Object

Get/set property.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/courtier/config.rb', line 37

def property(name, value=ArgumentError)
  name = name.to_sym

  return @property[name] if value == ArgumentError

  case name
  when :feature, :command, :profile
    @property[name] = value.to_s
  else
    @property[name] = value
  end
end

#require_featureObject

Require the feature.



117
118
119
120
121
122
123
# File 'lib/courtier/config.rb', line 117

def require_feature
  begin
    require feature
  rescue LoadError
    #warn "No such feature -- `#{feature}'"
  end
end

#to_procObject

Returns underlying block.



135
136
137
# File 'lib/courtier/config.rb', line 135

def to_proc
  block
end