config-me 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/config-me.rb +36 -0
- data/lib/config-me/configuration.rb +21 -0
- data/lib/config-me/definitions_parser.rb +24 -0
- data/lib/config-me/errors.rb +19 -0
- data/lib/config-me/node.rb +26 -0
- data/lib/config-me/version.rb +3 -0
- metadata +84 -0
data/lib/config-me.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'active_support/dependencies/autoload'
|
2
|
+
|
3
|
+
module ConfigMe
|
4
|
+
|
5
|
+
@current = nil
|
6
|
+
@configurations = {}
|
7
|
+
|
8
|
+
autoload :VERSION, 'config-me/version'
|
9
|
+
autoload :Node, 'config-me/node'
|
10
|
+
autoload :Configuration, 'config-me/configuration'
|
11
|
+
autoload :DefinitionsParser, 'config-me/definitions_parser'
|
12
|
+
autoload :UndefinedSetting, 'config-me/errors'
|
13
|
+
autoload :UndefinedScope, 'config-me/errors'
|
14
|
+
autoload :ConfigurationsNotDefined, 'config-me/errors'
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def self.method_missing method, *args, &block
|
19
|
+
raise ConfigMe::ConfigurationsNotDefined if @current.nil?
|
20
|
+
@current.send method, *args, &block
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def ConfigMe scope = :global, &definitions
|
25
|
+
configurations = ConfigMe.instance_variable_get :@configurations
|
26
|
+
|
27
|
+
if block_given?
|
28
|
+
configurations[scope] = ConfigMe::Configuration.new(&definitions)
|
29
|
+
ConfigMe.instance_variable_set :@configurations, configurations
|
30
|
+
ConfigMe.instance_variable_set :@current, configurations[scope]
|
31
|
+
else
|
32
|
+
raise ConfigMe::UndefinedScope.new(scope) unless configurations.has_key?(scope)
|
33
|
+
ConfigMe.instance_variable_set :@current, configurations[scope]
|
34
|
+
ConfigMe
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class ConfigMe::Configuration
|
2
|
+
|
3
|
+
def initialize &definitions
|
4
|
+
@tree = ConfigMe::DefinitionsParser.parse! %w(ConfigMe), &definitions
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.clear!
|
8
|
+
ConfigMe.instance_variable_set :@current, nil
|
9
|
+
ConfigMe.instance_variable_set :@configurations, {}
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def method_missing method, *args, &block
|
15
|
+
if @tree.has_key? method
|
16
|
+
@tree[ method ]
|
17
|
+
else
|
18
|
+
raise ConfigMe::UndefinedSetting.new(%W(ConfigMe #{method}))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class ConfigMe::DefinitionsParser
|
2
|
+
|
3
|
+
def self.parse! breadcrumbs = [], &definitions
|
4
|
+
new(breadcrumbs, &definitions).instance_variable_get(:@store)
|
5
|
+
end
|
6
|
+
|
7
|
+
def initialize breadcrumbs = [], &definitions
|
8
|
+
@store = ConfigMe::Node.new(breadcrumbs)
|
9
|
+
instance_exec &definitions
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def method_missing method, *args, &definitions
|
15
|
+
@store[ method ] = if block_given?
|
16
|
+
breadcrumbs = @store.instance_variable_get(:@breadcrumbs)
|
17
|
+
breadcrumbs << method
|
18
|
+
|
19
|
+
ConfigMe::DefinitionsParser.parse! breadcrumbs, &definitions
|
20
|
+
else
|
21
|
+
args.first
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ConfigMe
|
2
|
+
class ConfigurationsNotDefined < Exception
|
3
|
+
def initialize
|
4
|
+
super "No configurations were defined"
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class UndefinedScope < Exception
|
9
|
+
def initialize scope
|
10
|
+
super %(Undefined scope "#{scope}")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class UndefinedSetting < Exception
|
15
|
+
def initialize breadcrumbs
|
16
|
+
super %(Undefined setting "#{breadcrumbs.join('.')}")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class ConfigMe::Node
|
2
|
+
|
3
|
+
def initialize breadcrumbs = []
|
4
|
+
@values, @breadcrumbs = {}, breadcrumbs
|
5
|
+
end
|
6
|
+
|
7
|
+
def []=(key, value)
|
8
|
+
@values[ key ] = value
|
9
|
+
end
|
10
|
+
|
11
|
+
def [] key
|
12
|
+
@values[ key ]
|
13
|
+
end
|
14
|
+
|
15
|
+
def has_key? key
|
16
|
+
@values.has_key? key
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_missing method, *args, &block
|
20
|
+
if has_key? method
|
21
|
+
self[ method ]
|
22
|
+
else
|
23
|
+
raise ConfigMe::UndefinedSetting.new(@breadcrumbs + [method])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: config-me
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ivan Garmatenko
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-19 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: cucumber
|
16
|
+
requirement: &71931750 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.1.9
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *71931750
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &71931370 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '2.9'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *71931370
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: activesupport
|
38
|
+
requirement: &71931020 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '3.0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *71931020
|
47
|
+
description: Provides convenient tool for storing configuration
|
48
|
+
email:
|
49
|
+
- cheef.che@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- lib/config-me/version.rb
|
55
|
+
- lib/config-me/errors.rb
|
56
|
+
- lib/config-me/definitions_parser.rb
|
57
|
+
- lib/config-me/configuration.rb
|
58
|
+
- lib/config-me/node.rb
|
59
|
+
- lib/config-me.rb
|
60
|
+
homepage: http://github.com/cheef/config-me
|
61
|
+
licenses: []
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project: config-me
|
80
|
+
rubygems_version: 1.8.10
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: Provides convenient tool for storing configuration
|
84
|
+
test_files: []
|