Class: Courtier::Config
- Inherits:
-
Object
- Object
- Courtier::Config
- 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
-
#block ⇒ Object
readonly
Most configuration are scripted.
Instance Method Summary collapse
-
#apply? ⇒ Boolean
Does the configuration apply?.
-
#arity ⇒ Fixnum
The arity of the configuration procedure.
-
#call(*args) ⇒ Object
Call the configuration procedure.
-
#command ⇒ Object
(also: #tool)
The name of command being configured.
-
#command?(command = Courtier.current_command) ⇒ Boolean
Does the given ‘command` match the config’s command?.
-
#copy(alt = {}) ⇒ Config
Copy the configuration with alterations.
-
#feature ⇒ Object
The feature being configured.
-
#feature?(feature = Courtier.current_feature) ⇒ Boolean
Does the given ‘feature` match the config’s feature?.
-
#from ⇒ Object
The library from which this configuration derives.
-
#initialize(tool, properties = {}, &block) ⇒ Config
constructor
Initialize Config instance.
-
#match?(*args) ⇒ Boolean
Match config against tool and/or profile names.
- #onload? ⇒ Boolean
-
#profile ⇒ Object
The name of the profile to which this configuration belongs.
-
#profile?(profile = Courtier.current_profile) ⇒ Boolean
Does the given ‘profile` match the config’s profile?.
-
#property(name, value = ArgumentError) ⇒ Object
Get/set property.
-
#require_feature ⇒ Object
Require the feature.
-
#to_proc ⇒ Object
Returns underlying block.
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.
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
#block ⇒ Object (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?
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 |
#arity ⇒ Fixnum
The arity of the configuration procedure.
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 |
#command ⇒ Object 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?
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.
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 |
#feature ⇒ Object
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?
194 195 196 |
# File 'lib/courtier/config.rb', line 194 def feature?(feature=Courtier.current_feature) self.feature == feature.to_s end |
#from ⇒ Object
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.
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
87 88 89 |
# File 'lib/courtier/config.rb', line 87 def onload? @property[:onload] end |
#profile ⇒ Object
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?
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_feature ⇒ Object
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_proc ⇒ Object
Returns underlying block.
135 136 137 |
# File 'lib/courtier/config.rb', line 135 def to_proc block end |