Class: Yacl::Define::Plan
- Inherits:
-
Object
- Object
- Yacl::Define::Plan
- Defined in:
- lib/yacl/define/plan.rb
Overview
Public: The Plan is the order an method by which the Properties of your application are loaded and looked up. This is a base class from which you will define your Property lookups.
Example:
class MyPlan < Yacl::Define::Plan
try MyApp::Cli::Parser
try Yacl::Loader::Env, :prefix => 'MY_APP'
try Yacl::Loader::YamlDir, :parameter => 'config.dir'
try MyApp::Defaults
on_error do |exception|
$stderr.puts "ERROR: #{exception}"
$stderr.puts "Try --help for help"
exit 1
end
end
This creates a class MyPlan that when utilized by a Runner loads properties in the following cascading order:
1) Commandline is parsed and converted to Properties, these have the highest
priority
2) The environemtn variables are used, only those that start with ‘MY_APP’
as the variable prefix. These have the 2nd higest priority
3) A configuration directory is loaded, using the ‘config.dir’ parameter
that comes from either (1) or (2). Properties found in this config dir
hve the 3rd highest priority
4) Built in defaults if the property is not found any any of the previous
locations.
Defined Under Namespace
Instance Attribute Summary collapse
-
#properties ⇒ Object
readonly
Public: Return the Properties instance that results from instantiating the Plan.
Class Method Summary collapse
-
.has_on_error? ⇒ Boolean
Internal: Test to see if this class has an on_error callable defined.
-
.items ⇒ Object
Internal: Return the array of Items that are used in the child class definition.
-
.on_error(callable = nil, &block) ⇒ Object
Public: Define a callable to be invoked should an error happen while loading the properties of your application.
-
.try(klass, options = {}) ⇒ Object
Public: Add the given Loader child class or Loader duck-type class to the definition of the Plan.
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ Plan
constructor
Public: Create a new instance of the Plan.
-
#items ⇒ Object
Internal: Return the array of Items defined for this class.
Constructor Details
#initialize(params = {}) ⇒ Plan
Public: Create a new instance of the Plan. This should be invoked via #super in a child class.
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/yacl/define/plan.rb', line 137 def initialize( params = {} ) initial_properties = params[:initial_properties] || Yacl::Properties.new @properties = load_properties( initial_properties, params, items ) rescue Yacl::Error => ye if self.class.has_on_error? then self.class.on_error.call( ye ) else raise ye end end |
Instance Attribute Details
#properties ⇒ Object (readonly)
Public: Return the Properties instance that results from instantiating the Plan.
Returns an Properties instance.
132 133 134 |
# File 'lib/yacl/define/plan.rb', line 132 def properties @properties end |
Class Method Details
.has_on_error? ⇒ Boolean
Internal: Test to see if this class has an on_error callable defined.
Returns true or false if the on_error is defined.
108 109 110 |
# File 'lib/yacl/define/plan.rb', line 108 def has_on_error? defined? @on_error end |
.items ⇒ Object
Internal: Return the array of Items that are used in the child class definition.
Returns: an Array of Items
116 117 118 |
# File 'lib/yacl/define/plan.rb', line 116 def items @items ||= [] end |
.on_error(callable = nil, &block) ⇒ Object
Public: Define a callable to be invoked should an error happen while loading the properties of your application.
callable - the Callable to be invoked.
Example:
on_error( Proc.new{ fail "kaboom!" } )
on_error do
raise "Kaboom!"
end
Return the callable if no parameters are given and the callable is defined.
Raises an error if no parameters are given and the callable is NOT defined.
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/yacl/define/plan.rb', line 93 def on_error( callable = nil, &block ) if callable then @on_error = callable elsif block_given? @on_error = block elsif defined? @on_error return @on_error else raise Error, "on_error requires the use of a callable or a block" end end |
.try(klass, options = {}) ⇒ Object
Public: Add the given Loader child class or Loader duck-type class to the definition of the Plan.
klass - A Class that implements the Loader API. options - A Hash that will be passed to klass.new() when the klass is
initialized
Example:
try Yacl::Loader::YamlDir, :parameter => 'config.dir'
Returns nothing.
70 71 72 |
# File 'lib/yacl/define/plan.rb', line 70 def try( klass , = {} ) items << Item.new( klass, ) end |
Instance Method Details
#items ⇒ Object
Internal: Return the array of Items defined for this class
Returns an Array of Items.
124 125 126 |
# File 'lib/yacl/define/plan.rb', line 124 def items self.class.items end |