configus 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.swp
6
+ *.swo
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in configus.gemspec
4
+ gemspec
@@ -0,0 +1,58 @@
1
+ # Configus
2
+
3
+ ## Summary
4
+
5
+ Configus helps you easily manage environment specific settings
6
+
7
+ ## Installing
8
+
9
+ Add this to your `Gemfile`:
10
+
11
+ gem "configus"
12
+
13
+ ## Examples
14
+
15
+ ### Definition
16
+
17
+ Configus.build :development do # set current environment
18
+ env :production do
19
+ website_url 'http://example.com'
20
+ email do
21
+ pop do
22
+ address 'pop.example.com'
23
+ port 110
24
+ end
25
+ smtp do
26
+ address 'smtp.example.com'
27
+ port 25
28
+ end
29
+ end
30
+ end
31
+
32
+ env :development, :parent => :production do
33
+ website_url 'http://text.example.com'
34
+ email do
35
+ smtp do
36
+ address 'smpt.text.example.com'
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ ### Usage
43
+
44
+ configus.website_url # => 'http://text.example.com'
45
+ configus.email.pop.port # => 110
46
+
47
+ ### Rails
48
+
49
+ define your config in `config/initializers/configus.rb`
50
+
51
+ Configus.build Rails.env do
52
+ # settigns
53
+ end
54
+
55
+ ## Similar
56
+
57
+ * https://github.com/markbates/configatron
58
+ * https://github.com/railsjedi/rails_config
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "configus/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "configus"
7
+ s.version = Configus::VERSION
8
+ s.authors = ["Mokevnin Kirill"]
9
+ s.email = ["mokevnin@gmail.com"]
10
+ s.homepage = "https://github.com/kaize/configus"
11
+ s.summary = "Configus helps you easily manage environment specific settings"
12
+ s.description = "Configus helps you easily manage environment specific settings"
13
+
14
+ s.rubyforge_project = "configus"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ s.add_runtime_dependency "activesupport"
24
+ end
@@ -0,0 +1,17 @@
1
+ require 'active_support/core_ext/hash'
2
+
3
+ require "configus/version"
4
+ require 'configus/builder'
5
+ require 'configus/proxy'
6
+ require 'configus/config'
7
+ require 'configus/core_ext/kernel'
8
+
9
+ module Configus
10
+ def self.build(env, &block)
11
+ @config = Builder.build(env, &block)
12
+ end
13
+
14
+ def self.config
15
+ @config
16
+ end
17
+ end
@@ -0,0 +1,49 @@
1
+ module Configus
2
+ class Builder
3
+
4
+ class << self
5
+ def build(current_env, &block)
6
+ b = new(current_env, block)
7
+ Config.new(b.result)
8
+ end
9
+ end
10
+
11
+ def initialize(current_env, block)
12
+ @current_env = current_env.to_sym
13
+ @envs = {}
14
+ instance_eval &block
15
+ end
16
+
17
+ def result(env = nil)
18
+ env_name = env || @current_env
19
+ e = @envs[env_name]
20
+ unless e
21
+ raise "Call undefined env '#{env_name}'"
22
+ end
23
+
24
+ current_config = {}
25
+ if e[:block]
26
+ current_config = Proxy.generate(e[:block])
27
+ end
28
+
29
+ parent = e[:options][:parent]
30
+ if parent
31
+ parent_config = result(parent)
32
+ current_config = parent_config.deep_merge!(current_config)
33
+ end
34
+
35
+ current_config
36
+ end
37
+
38
+ private
39
+
40
+ def env(env, options = {}, &block)
41
+ env = env.to_sym
42
+ @envs[env] = {
43
+ :options => options,
44
+ }
45
+ @envs[env][:block] = block if block_given?
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,32 @@
1
+ require 'singleton'
2
+
3
+ module Configus
4
+ class Config
5
+ def initialize(config, section = nil)
6
+ @config = {}
7
+ config.each_pair do |key, value|
8
+ @config[key] = value.is_a?(Hash) ? Config.new(value) : value
9
+ end
10
+
11
+ (class << self; self; end).class_eval do
12
+ config.each_pair do |key, value|
13
+ if value.is_a? Hash
14
+ value = Config.new(value)
15
+ end
16
+
17
+ define_method key.to_sym do
18
+ value
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ def [](key)
25
+ @config[key]
26
+ end
27
+
28
+ def method_missing(meth, *args, &blk)
29
+ raise "'#{meth}' key does not exists in your configus"
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module Kernel
2
+ def configus
3
+ Configus.config
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ module Configus
2
+ class Proxy
3
+ attr_reader :result
4
+
5
+ class << self
6
+ def generate(block)
7
+ p = new(block)
8
+ p.result
9
+ end
10
+ end
11
+
12
+ def initialize(block)
13
+ @result = {}
14
+ instance_eval &block
15
+ end
16
+
17
+ def method_missing(key, value = nil, &block)
18
+ @result[key] = value ? value : self.class.generate(block)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Configus
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Configus::Builder do
4
+ before do
5
+ p = Proc.new do
6
+ env :production do
7
+ key 'value'
8
+ foo 'bar'
9
+ sections do
10
+ first 'first_value'
11
+ second 'second_value'
12
+ end
13
+ end
14
+
15
+ env :development, :parent => :production do
16
+ bar 'foo'
17
+ foo 'foobar'
18
+ sections do
19
+ first 'another_value'
20
+ end
21
+ end
22
+ end
23
+ builder = Configus::Builder.new(:development, p)
24
+ @options = builder.result
25
+ end
26
+
27
+ it 'should be generate correct hash' do
28
+ @options.should == {
29
+ :key => 'value',
30
+ :bar => 'foo',
31
+ :foo => 'foobar',
32
+ :sections => {
33
+ :first => 'another_value',
34
+ :second => 'second_value'
35
+ }
36
+ }
37
+ end
38
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Configus::Config do
4
+ before do
5
+ @options = {
6
+ :foo => 'bar',
7
+ :sections => {
8
+ :first => 'first_value',
9
+ :second => 'second_value'
10
+ }
11
+ }
12
+ @config = Configus::Config.new(@options)
13
+ end
14
+
15
+ it 'should be defined' do
16
+ @config.foo == @options[:foo]
17
+ @config.sections.first == @options[:sections][:first]
18
+ end
19
+
20
+ it 'should be raise' do
21
+ lambda {@config.dont_exists}.should raise_error(RuntimeError)
22
+ end
23
+
24
+ it 'should be available as hash' do
25
+ @config[:foo] == @options[:foo]
26
+ @config[:sections].second == @options[:sections][:second]
27
+ end
28
+ end
29
+
@@ -0,0 +1,3 @@
1
+ require 'configus'
2
+ require 'bundler/setup'
3
+
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: configus
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Mokevnin Kirill
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-05 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: activesupport
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ description: Configus helps you easily manage environment specific settings
49
+ email:
50
+ - mokevnin@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - .gitignore
59
+ - Gemfile
60
+ - README.md
61
+ - Rakefile
62
+ - configus.gemspec
63
+ - lib/configus.rb
64
+ - lib/configus/builder.rb
65
+ - lib/configus/config.rb
66
+ - lib/configus/core_ext/kernel.rb
67
+ - lib/configus/proxy.rb
68
+ - lib/configus/version.rb
69
+ - spec/configus/builder_spec.rb
70
+ - spec/configus/config_spec.rb
71
+ - spec/spec_helper.rb
72
+ homepage: https://github.com/kaize/configus
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options: []
77
+
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ requirements: []
99
+
100
+ rubyforge_project: configus
101
+ rubygems_version: 1.8.10
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Configus helps you easily manage environment specific settings
105
+ test_files: []
106
+
107
+ has_rdoc: