Configurable¶ ↑
Class configurations for the command line and web.
Description¶ ↑
Configurable adds methods to declare class configurations. Configurations are inheritable, delegate to methods, and have hash-like access. Configurable constructs configs such that they easily map to config files, web forms, and the command line.
Usage¶ ↑
Declare configurations using the config method. Config generates accessors that initialize with the default value.
class ConfigClass include Configurable config :flag, false # a flag config :switch, true # an on/off switch config :num, 3.14 # a number config :lst, [1,2,3] # a list of integers config :str, 'one' # a string end c = ConfigClass.new c.str # => 'one'
Configs may also be accessed through config (a kind of delegating hash):
c.str = 'two' c.config[:str] # => 'two' c.config[:str] = 'three' c.str # => 'three' c.config.to_hash # => { # :flag => false, # :switch => true, # :num => 3.14, # :lst => [1, 2, 3], # :str => 'three' # }
Configs may be imported and exported as simple objects which easily translate to and from user interfaces, be they config files, web forms, or the command line.
Config files:
c.config.import( 'flag' => true, 'num' => 6.022 ) c.config.export # => { # 'flag' => true, # 'switch' => true, # 'num' => 6.022, # 'lst' => [1, 2, 3], # 'str' => 'three' # }
Web forms:
params = { 'flag' => 'true', # checkbox 'switch' => 'true', # radio button 'num' => '2.71', # text input 'lst' => ['2', '6'] # list input (lst[]=2&lst[]=6) } c.config.import(params).to_hash # => { # :flag => true, # :switch => true, # :num => 2.71, # :lst => [2, 6], # :str => 'three' # }
Command Line:
c.config.parse %w{a --flag --no-switch --num 6.022 --lst 7 --lst 8,9 b c} # => ['a', 'b', 'c'] c.config.to_hash # => { # :flag => true, # :switch => false, # :num => 6.022, # :lst => [7, 8, 9], # :str => 'three' # } stdout = [] parser = c.config.parser do |psr| psr.on '-h', '--help', 'print help' do stdout << "options:" stdout << psr end end parser.parse('--help') "\n" + stdout.join("\n") # => %q{ # options: # --flag a flag # -h, --help print help # --lst LST... a list of integers (1,2,3) # --num NUM a number (3.14) # --str STR a string (one) # --[no-]switch an on/off switch # }
Configurable supports custom data types, nested configs, and config modules.
See the help documentation for more details:
Installation¶ ↑
Configurable is available as a gem.
% gem install configurable
Development¶ ↑
To get started, checkout the code from GitHub and run the tests:
git clone git://github.com/thinkerbot/configurable.git cd configurable rake test
Please report any issues here.
Info ¶ ↑
- Developer
- License