Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit with worked version

  • Loading branch information...
commit 755f762a0c621d428fee79269db93929cd7cc80d 0 parents
Ivan Garmatenko authored
5 .gitignore
@@ -0,0 +1,5 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
+.idea
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in config-me.gemspec
+gemspec
18 README.markdown
@@ -0,0 +1,18 @@
+# ConfigMe
+
+Gem provides convenient tool for storing configurations
+
+## Installation
+
+ gem install config-me
+
+## Usage
+
+ ConfigMe do
+ foo do
+ bar 'test'
+ end
+ end
+
+ ConfigMe.foo.bar # => 'test'
+
1  Rakefile
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
26 config-me.gemspec
@@ -0,0 +1,26 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "config-me/version"
+
+Gem::Specification.new do |s|
+ s.name = "config-me"
+ s.version = ConfigMe::VERSION
+ s.authors = ["Ivan Garmatenko"]
+ s.email = %w(cheef.che@gmail.com)
+ s.homepage = "http://github.com/cheef/config-me"
+ s.summary = %q{Provides convenient tool for storing configuration}
+ s.description = %q{Provides convenient tool for storing configuration}
+
+ s.rubyforge_project = "config-me"
+
+ s.files = []
+ %w(lib).each do |folder|
+ s.files += Dir.glob("#{folder}/**/*.*")
+ end
+
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.require_paths = %w(lib)
+
+ s.add_development_dependency "cucumber"
+ s.add_development_dependency "rspec"
+end
58 features/define_configuration.feature
@@ -0,0 +1,58 @@
+Feature: defining configuration through blocks
+
+ Scenario: set 1st level setting
+ Given an empty configuration
+ When I define configuration below:
+ """
+ ConfigMe do
+ foo 'bar'
+ end
+ """
+ Then setting "ConfigMe.foo" should be equal to "bar"
+
+ Scenario: set 2nd level setting
+ Given an empty configuration
+ When I define configuration below:
+ """
+ ConfigMe do
+ foo do
+ bar 'foo'
+ end
+ end
+ """
+ Then setting "ConfigMe.foo.bar" should be equal to "foo"
+
+ Scenario: set 3rd level setting
+ Given an empty configuration
+ When I define configuration below:
+ """
+ ConfigMe do
+ foo do
+ bar do
+ sample 'bar'
+ end
+ end
+ end
+ """
+ Then setting "ConfigMe.foo.bar.sample" should be equal to "bar"
+
+ Scenario Outline: set different types of values and make sure that they are not change
+ Given an empty configuration
+ When I define configuration below:
+ """
+ ConfigMe do
+ foo <value>
+ end
+ """
+ Then setting "ConfigMe.foo" should be of type "<type>"
+
+ Examples:
+ | value | type |
+ | 'foo' | String |
+ | 100500 | Fixnum |
+ | 4.3 | Float |
+ | true | TrueClass |
+ | proc { 'foo' } | Proc |
+ | Time.now | Time |
+ | Date.today | Date |
+
19 features/missed_keys.feature
@@ -0,0 +1,19 @@
+Feature: raising errors on undefined settings
+
+ Scenario Outline: try to get non defined setting and make sure that exception raise
+ Given an empty configuration
+ When I define configuration below:
+ """
+ ConfigMe do
+ foo 'bar'
+ end
+ """
+ And I ask for an "ConfigMe.<key>" setting
+ Then It should raise error "ConfigMe::UndefinedSetting"
+
+ Examples:
+ | key |
+ | bar |
+ | bar.foo |
+ | bar.foo.bar |
+
27 features/step_definitions/config_me_steps.rb
@@ -0,0 +1,27 @@
+Given /^an empty configuration$/ do
+ ConfigMe::Configuration.clear!
+end
+
+When /^I define configuration below:$/ do |code_string|
+ eval code_string
+end
+
+Then /^setting "([^"]*)" should be equal to "([^"]*)"$/ do |code_string, value|
+ eval(code_string).should == value
+end
+
+Then /^setting "([^"]*)" should be of type "([^"]*)"$/ do |code_string, klass|
+ eval(code_string).class.name.should == klass
+end
+
+When /^I ask for an "([^"]*)" setting$/ do |code_string|
+ begin
+ @setting = eval(code_string)
+ rescue Exception => e
+ @error = e
+ end
+end
+
+Then /^It should raise error "([^"]*)"$/ do |klass|
+ @error.class.name.should == klass
+end
4 features/support.rb/env.rb
@@ -0,0 +1,4 @@
+$:.push File.expand_path("../../../lib", __FILE__)
+
+require 'config-me'
+require 'rspec/matchers'
72 lib/config-me.rb
@@ -0,0 +1,72 @@
+require "config-me/version"
+
+module ConfigMe
+
+ class UndefinedSetting < Exception; end
+
+ module Configuration
+ def self.parse! &definitions
+ @tree = DefinitionsParser.parse! &definitions
+ end
+
+ def self.clear!
+ @tree = nil
+ end
+
+ private
+
+ def self.method_missing method, *args, &block
+ if @tree.has_key? method
+ @tree[ method ]
+ else
+ raise ConfigMe::UndefinedSetting.new(%(Undefined setting "#{method}"))
+ end
+ end
+ end
+
+ class Node < Hash
+ def method_missing method, *args, &block
+ if has_key? method
+ self[ method ]
+ else
+ raise ConfigMe::UndefinedSetting.new(%(Undefined setting "#{@breadcrumbs}.#{method}"))
+ end
+ end
+ end
+
+ class DefinitionsParser
+
+ def self.parse! &definitions
+ new(&definitions).instance_variable_get(:@store)
+ end
+
+ def initialize &definitions
+ @store = Node.new
+ instance_exec &definitions
+ end
+
+ private
+
+ def method_missing method, *args, &definitions
+ @store[ method ] = if block_given?
+ DefinitionsParser.parse!(&definitions)
+ else
+ args.first
+ end
+ end
+ end
+
+ private
+
+ def self.method_missing method, *args, &block
+ Configuration.send method, *args, &block
+ end
+end
+
+def ConfigMe &definitions
+ if block_given?
+ ConfigMe::Configuration.parse! &definitions
+ else
+ const_get :ConfigMe
+ end
+end
3  lib/config-me/version.rb
@@ -0,0 +1,3 @@
+module ConfigMe
+ VERSION = "0.0.1"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.