Class: Hen
- Inherits:
-
Object
- Object
- Hen
- Defined in:
- lib/hen.rb,
lib/hen/cli.rb,
lib/hen/dsl.rb,
lib/hen/version.rb,
lib/hen/commands.rb
Overview
The class handling the program logic. This is what you use in your Rakefile. See the README for more information.
Defined Under Namespace
Modules: CLI, Commands, DSL, Version
Constant Summary collapse
- HENDIRS =
The directories to search for hen files. Set environment variable
HENPATHto add more. [File.('../hens', __FILE__)]
- HENS =
All hens found, mapped by their name.
Hash.new { |h, k| h[k] = [] }
- RCDIRS =
Directories to search for
.henrcfiles. [ENV.user_home, '.']
- HENRC_NAME =
The name of the
.henrcfile. '.henrc'- VERSION =
Version.to_s
Class Attribute Summary collapse
-
.hens ⇒ Object
readonly
The global container for all loaded hens.
-
.verbose ⇒ Object
readonly
The verbosity concerning errors and warnings.
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
The hen’s definition block.
-
#dependencies ⇒ Object
readonly
The list of the hen’s dependencies.
-
#name ⇒ Object
readonly
The hen’s name.
Class Method Summary collapse
-
.[](hen) ⇒ Object
call-seq: Hen => aHen.
-
.add_hen(hen, overwrite = false) ⇒ Object
call-seq: add_hen(hen, overwrite = false).
-
.config(key = nil) ⇒ Object
call-seq: config => aHash config(key) => anObject.
-
.default_henrc ⇒ Object
call-seq: default_henrc => aString.
-
.henrc ⇒ Object
call-seq: henrc => anArray.
-
.lay!(*args) ⇒ Object
call-seq: lay! lay!(:some_hen, :some_other_hen) lay!(exclude: [:some_hen, :some_other_hen]).
Instance Method Summary collapse
-
#initialize(args, overwrite = false, &block) ⇒ Hen
constructor
call-seq: new(args, overwrite = false) { … }.
-
#lay! ⇒ Object
call-seq: hen.lay!.
Constructor Details
#initialize(args, overwrite = false, &block) ⇒ Hen
call-seq:
new(args, overwrite = false) { ... }
Creates a new Hen instance of a certain name and optional dependencies; see #resolve_args for details on the args argument. Requires a definition block; see #lay! for details.
Adds itself to the global hen container via add_hen.
243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/hen.rb', line 243 def initialize(args, overwrite = false, &block) @name, @dependencies = resolve_args(args) @laid = false unless @block = block raise LocalJumpError, "#{@name}: no block given" if verbose return end self.class.add_hen(self, overwrite) end |
Class Attribute Details
.hens ⇒ Object (readonly)
The global container for all loaded hens.
74 75 76 |
# File 'lib/hen.rb', line 74 def hens @hens end |
.verbose ⇒ Object (readonly)
The verbosity concerning errors and warnings.
77 78 79 |
# File 'lib/hen.rb', line 77 def verbose @verbose end |
Instance Attribute Details
#block ⇒ Object (readonly)
The hen’s definition block.
233 234 235 |
# File 'lib/hen.rb', line 233 def block @block end |
#dependencies ⇒ Object (readonly)
The list of the hen’s dependencies.
230 231 232 |
# File 'lib/hen.rb', line 230 def dependencies @dependencies end |
#name ⇒ Object (readonly)
The hen’s name.
227 228 229 |
# File 'lib/hen.rb', line 227 def name @name end |
Class Method Details
.[](hen) ⇒ Object
138 139 140 |
# File 'lib/hen.rb', line 138 def [](hen) @hens[hen] end |
.add_hen(hen, overwrite = false) ⇒ Object
call-seq:
add_hen(hen, overwrite = false)
Adds hen to the global container. Overwrites an existing hen only if overwrite is true.
126 127 128 129 130 131 132 |
# File 'lib/hen.rb', line 126 def add_hen(hen, overwrite = false) if overwrite @hens[hen.name] = hen else @hens[hen.name] ||= hen end end |
.config(key = nil) ⇒ Object
call-seq:
config => aHash
config(key) => anObject
The configuration resulting from the user’s .henrc. Takes optional key argument as “path” into the config hash, returning the thusly retrieved value.
Example:
config('a/b/c') #=> @config[:a][:b][:c]
168 169 170 171 172 173 174 |
# File 'lib/hen.rb', line 168 def config(key = nil) @config ||= load_config return @config unless key key.split('/').inject(@config) { |value, k| value.fetch(k.to_sym) } rescue IndexError, NoMethodError end |
.default_henrc ⇒ Object
call-seq:
default_henrc => aString
The path to a suitable default .henrc location.
154 155 156 |
# File 'lib/hen.rb', line 154 def default_henrc find_henrc(false).first end |
.henrc ⇒ Object
call-seq:
henrc => anArray
The paths to the user’s .henrc files.
146 147 148 |
# File 'lib/hen.rb', line 146 def henrc @henrc ||= find_henrc end |
.lay!(*args) ⇒ Object
call-seq:
lay!
lay!(:some_hen, :some_other_hen)
lay!(exclude: [:some_hen, :some_other_hen])
Loads the hens, causing them to lay their eggs^H^H^Htasks. Either all, if no restrictions are specified, or the given hens, or all but those given in the :exclude option.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/hen.rb', line 87 def lay!(*args) # Extract potential options hash = args.last.is_a?(Hash) ? args.pop : {} @verbose = [:verbose] if .key?(:verbose) yield.each { |key, value| config[key].update(value) } if block_given? # Handle include/exclude requirements excl = [:exclude] args, default = args.empty? ? [Array(excl), true] : [args, false] inclexcl = Hash.new(default) args.each { |arg| inclexcl[arg.to_s] = !default } # Load all available hens (as far as the # include/exclude conditions are met) load_hens { |hen| inclexcl[hen] } # Execute each hen definition hens.each { |name, hen| # Load any dependencies, in case they're not included yet begin load_hens(*hen.dependencies) rescue LoadError => err warn "#{name}: Required dependency missing: " << File.basename(err.to_s, '.rake') if verbose next end hen.lay! } end |
Instance Method Details
#lay! ⇒ Object
call-seq:
hen.lay!
Runs the definition block, exposing helper methods from the DSL.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/hen.rb', line 260 def lay! return if laid? @laid = true # Call dependencies first dependencies.each { |hen| self.class[hen].lay! } block.bind(DSL).call rescue => err trace = $DEBUG || Rake.application..trace warn "#{name}: #{err} (#{err.class})" if trace || verbose warn err.backtrace.join("\n ") if trace end |