Skip to content

Repository files navigation

Class configurations for the command line and web.

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.

Declare configurations using the config method. Config generates accessors that initialize with the default value.

classConfigClassincludeConfigurableconfig:flag, false# a flagconfig:switch, true# an on/off switchconfig:num, 3.14# a numberconfig:lst, [1,2,3] # a list of integersconfig:str, 'one'# a stringendc = ConfigClass.newc.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.parserdo|psr|psr.on'-h', '--help', 'print help'dostdout<<"options:"stdout<<psrendendparser.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:

Configurable is available as a gem.

% gem install configurable

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.

Developer

Simon Chiang

License

MIT-Style

About

Class configurations that map to the command line.

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages