app-ctx
by dirk luesebrink
http://www.sofasportler.de/dirk.blog

== DESCRIPTION:

Every application needs configuration and app-ctx provides a concise way of
doing it.

For all applications (you are not a mouseclicker, are u?), once in a while
you need to supply some configuration values to overrule the built-in
defaults. The app-ctx gem does unify and organize built-in constants,
config files and commandline option with a clearly defined priority, from
low to high:

- procedural: set from your implementation App::Config#set_default_values
- YAML default values file loaded from next to the $0 script
- user supplied configuration file, eg.: --config=/tmp/foo.yml
- command line options and flags: --foo --bar=foo

But for your application it is of no interesst from where the values are
coming: command line option: "--port=1234", a user configuration file or
from the applications built-in default values. Therefor +app-ctx+ combines
value settings from various sources into a single configuration hash.

== SYNOPSIS:

(hint: see also the examples dir)
basically you have two ways to use it:

require 'app-ctx' # of course,and than...

1. closures (see examples/run_with_block.rb)

App::ctx.run do |context| ... end

where context object provides:

values: the combined key and value settings
argv: remaining argument(not the options) of the command line
defaults_path: full path to the defaults file


2. with a mainclass(see examples/run_with_class.rb)

App::run :class => Simple

for the second case(with a mainclass) an application instance of this class
is created. The first argument is than taken as method name and executed,
again with a context oject provided.

$ ruby example/run_with_class show

will result in the :show method beeing called:

context = Config.new...
...
app = Simple.new...
app.show(context)


= String/Type mappings

Commandline options are strings only, but sometimes you need strongly typed
primitive values. +app-ctx+ does automatically convert integer and float
values, see "examples/conversions.rb" for:

$ ./examples/conversions.rb
:f=>3.14
typeof 'i': Fixnum
typeof 'f': Float

$ ./examples/conversions.rb -i=17 -f=2.12
:f=>2.12
typeof 'i': Fixnum
typeof 'f': Float

$ ./examples/conversions.rb -i=i -f=f
:f=>"f"
typeof 'i': String
typeof 'f': String


== Flags/Boolean values

Flags(options without values) are converted to boolean values, see
examples/boolean.rb:

$ ruby ./examples/boolean.rb
:bool=>false
typeof 'bool': FalseClass

$ ruby ./examples/boolean.rb --bool
:bool=>true
typeof 'bool': TrueClass


== Ruby Conversions

When Fixnum, Float and boolean conversion are not enough, as a last resort,
you can use ruby code directly for evaluation of option values. Replacing
'=' with ':' results in assigning the ruby evaluation value to the key, see
examples/ruby_conv.rb:

$ ruby ./examples/ruby_conv.rb
:a=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
typeof 'r': Range
typeof 'a': Array

$ ruby ./examples/ruby_conv.rb -r:2..3 -a:'(-1..1).to_a'
:a=>[-1, 0, 1]
typeof 'r': Range
typeof 'a': Array

$ ruby ./examples/ruby_conv.rb -r:2..3 -a:\(-1..1\).to_a
:a=>[-1, 0, 1]
typeof 'r': Range
typeof 'a': Array

== REQUIREMENTS:

* the hoe gem

== INSTALL:

* sudo gem install app-ctx --include-dependencies

== LICENSE:

(The MIT License)

Copyright (c) 2007 FIX

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.