Class: RC::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/rc/config.rb

Overview

Config encapsulates a single configuration entry as defined in a project’s ruby rc file. A config consists of a possible ‘command`, `feature`, `profile` and `onload` flag.

If ‘command` or `feature` are nil, then the configuration applies to all commands and/or features.

If profile is ‘nil` it automatically becomes `default`, which is the profile used when no profile is specified.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Config

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

Parameters:

  • target (#to_s)

    The command or feature name. (optional)

  • properties (Hash)

    Any additional properties associated with the config entry.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rc/config.rb', line 25

def initialize(*args, &block)
  properties = (Hash === args.last ? args.pop : {})
  target     = args.first

  @property = {:command=>nil, :feature=>nil, :profile=>'default'}

  if target
    @property[:command] = target.to_s
    @property[:feature] = target.to_s
  end

  @block = block

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

Instance Attribute Details

#blockObject (readonly)

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



97
98
99
# File 'lib/rc/config.rb', line 97

def block
  @block
end

Instance Method Details

#apply_to_command?Boolean Also known as: apply_to_tool?

Does the configuration apply?

Returns:

  • (Boolean)


246
247
248
249
250
251
# File 'lib/rc/config.rb', line 246

def apply_to_command?
  return false if onload?
  return false unless command? if command
  return false unless profile? if profile
  return true
end

#apply_to_feature?Boolean

Returns:

  • (Boolean)


254
255
256
257
258
259
# File 'lib/rc/config.rb', line 254

def apply_to_feature?
  return false unless onload?
  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



111
112
113
# File 'lib/rc/config.rb', line 111

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

#call(*args) ⇒ Object

Call the configuration procedure.



129
130
131
# File 'lib/rc/config.rb', line 129

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

#commandObject

The name of command being configured.



64
65
66
# File 'lib/rc/config.rb', line 64

def command
  @property[:command]
end

#command=(name) ⇒ Object (private)



289
290
291
# File 'lib/rc/config.rb', line 289

def command=(name)
  @property[:command] = name ? name.to_s : nil
end

#command?(command = RC.current_command) ⇒ Boolean Also known as: tool

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

Returns:

  • (Boolean)


206
207
208
209
# File 'lib/rc/config.rb', line 206

def command?(command=RC.current_command)
  return true if self.command.nil?
  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:



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

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.



57
58
59
# File 'lib/rc/config.rb', line 57

def feature
  @property[:feature]
end

#feature=(path) ⇒ Object (private)



294
295
296
# File 'lib/rc/config.rb', line 294

def feature=(path)
  @property[:feature] = path ? path.to_s : nil
end

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

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

Returns:

  • (Boolean)


196
197
198
199
# File 'lib/rc/config.rb', line 196

def feature?(feature=RC.current_feature)
  return true if self.feature.nil?
  self.feature == feature.to_s
end

#fromObject

The library from which this configuration derives.



81
82
83
# File 'lib/rc/config.rb', line 81

def from
  @property[:from]
end

#get(name) ⇒ Object (private)

Get property.



266
267
268
# File 'lib/rc/config.rb', line 266

def get(name)
  @property[name.to_sym]
end

#match?(*args) ⇒ Boolean

Match config against tool and/or profile names.

Returns:

  • (Boolean)


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

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

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

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

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

  return true
end

#onload?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/rc/config.rb', line 88

def onload?
  @property[:onload]
end

#profileObject

The name of the profile to which this configuration belongs.



74
75
76
# File 'lib/rc/config.rb', line 74

def profile
  @property[:profile]
end

#profile=(name) ⇒ Object (private)



299
300
301
# File 'lib/rc/config.rb', line 299

def profile=(name)
  @property[:profile] = name ? name.to_s : 'default'
end

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

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

Returns:

  • (Boolean)


217
218
219
# File 'lib/rc/config.rb', line 217

def profile?(profile=RC.current_profile)
  self.profile == (profile || 'default').to_s
end

#property(name, value = ArgumentError) ⇒ Object

Get/set property.



46
47
48
49
50
51
52
# File 'lib/rc/config.rb', line 46

def property(name, value=ArgumentError)
  if value == ArgumentError
    get(name)
  else
    set(name, value)
  end
end

#require_featureObject

Require the feature.



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

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

#set(name, value) ⇒ Object (private)

Set property.



273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/rc/config.rb', line 273

def set(name, value)
  case name.to_sym
  when :command
    self.command = value
  when :tool  # deprecate ?
    self.command = value
  when :feature
    self.feature = value
  when :profile
    self.profile = value
  else
    @property[name.to_sym] = value
  end
end

#to_procObject

Returns underlying block.



136
137
138
# File 'lib/rc/config.rb', line 136

def to_proc
  block
end