Skip to content

Commit

Permalink
possibilities to dump each node of config to hash and redefine the nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Garmatenko committed Mar 22, 2012
1 parent 5b8747d commit 578ea98
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 4 deletions.
82 changes: 80 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,18 +1,96 @@
# ConfigMe

Gem provides convenient tool for storing configurations
Gem provides convenient tool for storing file based configurations.

## Installation

gem install config-me

## Usage
In Rails 3, add this to your Gemfile and run the bundle command.

gem "config-me"

## Defining config

ConfigMe do
foo do
bar 'test'
end

sample 666
end

## Reading config

ConfigMe.foo.bar # => 'test'
ConfigMe.sample # => 666
ConfigMe.foo # => an instance of ConfigMe::Node which have some useful methods

ConfigMe.test # => will raise exception ConfigMe::UndefinedSetting

## Changing existing config

Let's define config for a start:

ConfigMe do
foo do
bar 'test'
end
end

Then we could change value of the existing setting:

ConfigMe.foo.bar = 'sample'

Or add new setting:

ConfigMe.foo.sample = 'test'

The only restriction is that we couldn't add setting to not existing node:

ConfigMe.foo.test.cool = 5 # => will raise exception ConfigMe::UndefinedSetting 'test' for 'ConfigMe.foo'

But we could define it using blocks structure:

ConfigMe.foo do
test do
cool 5
end
end

ConfigMe.foo.test.cool # => 5
ConfigMe.foo.sample # => will raise exception ConfigMe::UndefinedSetting, because we redefined whole node

## Namespaces

Every time you define configuration in some namespace, the default is ```:production```,
but you can provide the namespace name you want:

ConfigMe do
foo 'bar'
end

ConfigMe(:development) do
foo 'foo'
end

ConfigMe.foo # => 'bar'
ConfigMe(:production).foo # => 'bar'
ConfigMe(:development).foo # => 'foo'

## Dumping config to hash

ConfigMe do
foo do
bar 'test'
end
end

ConfigMe.to_hash # => { :foo => { :bar => 'test' } }
ConfigMe.foo.to_hash # => { :bar => 'test' }






18 changes: 18 additions & 0 deletions features/step_definitions/config_me_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,22 @@
eval code_string
end

When /^I dump config node "([^"]*)" to hash$/ do |code_string|
@hash = eval(code_string).to_hash
end

Then /^It should be a hash like:$/ do |string_with_hash|
@hash.should == eval(string_with_hash)
end

When /^I dump whole config to hash$/ do
@hash = ConfigMe.to_hash
end

Given /^an yaml file with configuration below:$/ do |string|
pending # express the regexp above with the code you wish you had
end

When /^I read configuration from this yaml file$/ do
pending # express the regexp above with the code you wish you had
end
12 changes: 11 additions & 1 deletion lib/config-me/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ def [] key
@values[ key ]
end

def method_missing method, *args, &block
def to_hash
@values.inject({}) do |new_hash, pair|
key, value = pair

new_hash[ key ] = value.is_a?(ConfigMe::Node) ? value.to_hash : value
new_hash
end
end

def method_missing method, *args, &definitions
case
when block_given? then self[ method ] = ConfigMe::DefinitionsParser.parse!(@breadcrumbs, &definitions)
when setter?(method) then self[ setter_method(method) ] = args.first
when has_key?(method) then self[ method ]
else raise ConfigMe::UndefinedSetting.new(@breadcrumbs + [method])
Expand Down
2 changes: 1 addition & 1 deletion lib/config-me/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ConfigMe
VERSION = "0.0.3"
VERSION = "0.0.4"
end

0 comments on commit 578ea98

Please sign in to comment.