config-me 0.0.4 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/config-me.rb CHANGED
@@ -1,36 +1,29 @@
1
1
  require 'active_support/dependencies/autoload'
2
+ require 'active_support/core_ext/class/attribute_accessors'
2
3
 
3
4
  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'
5
+ autoload :Base, 'config-me/base'
6
+ autoload :Defaults, 'config-me/defaults'
11
7
  autoload :DefinitionsParser, 'config-me/definitions_parser'
12
- autoload :UndefinedSetting, 'config-me/errors'
13
- autoload :UndefinedScope, 'config-me/errors'
14
- autoload :ConfigurationsNotDefined, 'config-me/errors'
8
+ autoload :Importer, 'config-me/importer'
9
+ autoload :Node, 'config-me/node'
10
+ autoload :Runner, 'config-me/runner'
11
+ autoload :VERSION, 'config-me/version'
12
+
13
+ # Errors
14
+ autoload :UnrecognizedError, 'config-me/errors'
15
+ autoload :UndefinedSetting, 'config-me/errors'
16
+ autoload :ConfigurationNotDefined, 'config-me/errors'
17
+ autoload :WrongFormat, 'config-me/errors'
15
18
 
16
19
  private
17
20
 
18
21
  def self.method_missing method, *args, &block
19
- raise ConfigMe::ConfigurationsNotDefined if @current.nil?
20
- @current.send method, *args, &block
22
+ ConfigMe::Base.delegate_to_current_configuration method, *args, &block
21
23
  end
22
- end
23
24
 
24
- def ConfigMe scope = :production, &definitions
25
- configurations = ConfigMe.instance_variable_get :@configurations
25
+ end
26
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
27
+ def ConfigMe namespace_or_options = ConfigMe::Defaults.namespace, &definitions
28
+ ConfigMe.tap { ConfigMe::Runner.parse namespace_or_options, &definitions }
36
29
  end
@@ -0,0 +1,34 @@
1
+ class ConfigMe::Base
2
+
3
+ cattr_accessor :configurations
4
+ cattr_accessor :current_namespace
5
+
6
+ attr_accessor :node
7
+
8
+ class << self
9
+ def current_configuration
10
+ find_configuration current_namespace || ConfigMe::Defaults.namespace
11
+ end
12
+
13
+ def find_configuration namespace
14
+ raise ConfigMe::ConfigurationNotDefined.new(namespace) if !configurations.has_key?(namespace) || namespace.nil?
15
+ configurations[ namespace ]
16
+ end
17
+
18
+ def clear!
19
+ self.configurations = {}
20
+ reset_current_namespace!
21
+ end
22
+
23
+ def delegate_to_current_configuration method, *args, &block
24
+ current_configuration.node.send(method, *args, &block).tap do
25
+ reset_current_namespace!
26
+ end
27
+ end
28
+
29
+ def reset_current_namespace!
30
+ self.current_namespace = nil
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,8 @@
1
+ module ConfigMe::Defaults
2
+ class << self
3
+ attr_accessor :namespace, :proc_auto_calling
4
+ end
5
+
6
+ self.namespace = :production
7
+ self.proc_auto_calling = true
8
+ end
@@ -1,19 +1,26 @@
1
1
  module ConfigMe
2
- class ConfigurationsNotDefined < Exception
2
+
3
+ class UnrecognizedError < RuntimeError
3
4
  def initialize
4
- super "No configurations were defined"
5
+ super "Really something wrong"
5
6
  end
6
7
  end
7
8
 
8
- class UndefinedScope < Exception
9
- def initialize scope
10
- super %(Undefined scope "#{scope}")
9
+ class ConfigurationNotDefined < StandardError
10
+ def initialize name
11
+ super "Configuration :#{name} not defined"
11
12
  end
12
13
  end
13
14
 
14
- class UndefinedSetting < Exception
15
+ class UndefinedSetting < ArgumentError
15
16
  def initialize breadcrumbs
16
17
  super %(Undefined setting "#{breadcrumbs.join('.')}")
17
18
  end
18
19
  end
20
+
21
+ class WrongFormat < ArgumentError
22
+ def initialize expected, received
23
+ super %(Expected #{expected} format, but received #{received})
24
+ end
25
+ end
19
26
  end
@@ -0,0 +1,41 @@
1
+ module ConfigMe::Importer
2
+
3
+ def self.from_definitions namespace, definitions
4
+ with_configuration(namespace) do |configuration|
5
+ configuration.node = ConfigMe::DefinitionsParser.parse! %w(ConfigMe), &definitions
6
+ end
7
+ end
8
+
9
+ def self.from_hash namespace, hash
10
+ raise ConfigMe::WrongFormat.new('hash', hash.class) unless hash.is_a?(Hash)
11
+ with_configuration(namespace) do |configuration|
12
+ configuration.node = convert_hash hash, %w(ConfigMe)
13
+ end
14
+ end
15
+
16
+ def self.from_yaml namespace, yaml
17
+ require 'yaml'
18
+ from_hash namespace, YAML::load(File.open(yaml))
19
+ end
20
+
21
+ def self.with_configuration namespace
22
+ ConfigMe::Base.new.tap do |configuration|
23
+ yield configuration
24
+ ConfigMe::Base.configurations ||= {}
25
+ ConfigMe::Base.configurations[ namespace ] = configuration
26
+ end
27
+ end
28
+
29
+ def self.convert_hash hash, breadcrumbs
30
+ ConfigMe::Node.new(breadcrumbs).tap do |node|
31
+ hash.each do |key, value|
32
+ node[ key.to_sym ] = if value.is_a?(Hash)
33
+ convert_hash value, (breadcrumbs + [ key.to_sym ])
34
+ else
35
+ value
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ end
@@ -25,13 +25,24 @@ class ConfigMe::Node
25
25
  case
26
26
  when block_given? then self[ method ] = ConfigMe::DefinitionsParser.parse!(@breadcrumbs, &definitions)
27
27
  when setter?(method) then self[ setter_method(method) ] = args.first
28
- when has_key?(method) then self[ method ]
28
+ when has_key?(method) then read_setting(method, *args)
29
29
  else raise ConfigMe::UndefinedSetting.new(@breadcrumbs + [method])
30
30
  end
31
31
  end
32
32
 
33
33
  private
34
34
 
35
+ def read_setting name, *args
36
+ value = self[ name ]
37
+
38
+ case
39
+ when value.is_a?(Proc) && ConfigMe::Defaults.proc_auto_calling
40
+ value.call(*args)
41
+ else
42
+ value
43
+ end
44
+ end
45
+
35
46
  def setter? key
36
47
  key.to_s =~ /^[a-zA-Z0-9_]+=$/
37
48
  end
@@ -0,0 +1,65 @@
1
+ module ConfigMe::Runner
2
+ class << self
3
+
4
+ attr_accessor :namespace, :options, :definitions
5
+
6
+ def parse namespace_or_options, &definitions
7
+ parse_execution_params! namespace_or_options, &definitions
8
+ case_and_apply_action!
9
+ end
10
+
11
+ def case_and_apply_action!
12
+ case
13
+ when reading? then set_current_namespace
14
+ when writing?
15
+ ConfigMe::Importer.from_definitions namespace, definitions
16
+ when importing_from_hash?
17
+ ConfigMe::Importer.from_hash namespace, options[:hash]
18
+ when importing_from_yaml?
19
+ ConfigMe::Importer.from_yaml namespace, options[:yaml]
20
+ else
21
+ raise ConfigMe::UnrecognizedError.new
22
+ end
23
+ end
24
+
25
+ def set_current_namespace
26
+ ConfigMe::Base.current_namespace = namespace
27
+ end
28
+
29
+ def parse_execution_params! namespace_or_options, &definitions
30
+ self.definitions = definitions
31
+
32
+ case
33
+ # ConfigMe.foo
34
+ # ConfigMe(:development).foo
35
+ when namespace_or_options.is_a?(Symbol) || namespace_or_options.is_a?(String)
36
+ self.namespace = namespace_or_options.to_sym
37
+ self.options = {}
38
+
39
+ # ConfigMe(:from_hash => { :foo => :bar })
40
+ # ConfigMe(:namespace => :development)
41
+ when namespace_or_options.is_a?(Hash)
42
+ self.namespace = namespace_or_options.delete(:namespace) || ConfigMe::Defaults.namespace
43
+ self.options = namespace_or_options
44
+ else
45
+ raise ConfigMe::UnrecognizedError.new
46
+ end
47
+ end
48
+
49
+ def reading?
50
+ options.empty? && definitions.nil?
51
+ end
52
+
53
+ def writing?
54
+ options.empty? && !definitions.nil?
55
+ end
56
+
57
+ def importing_from_hash?
58
+ options.has_key?(:hash)
59
+ end
60
+
61
+ def importing_from_yaml?
62
+ options.has_key?(:yaml)
63
+ end
64
+ end
65
+ end
@@ -1,3 +1,3 @@
1
1
  module ConfigMe
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,84 +1,119 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: config-me
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.4
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 6
10
+ version: 0.0.6
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Ivan Garmatenko
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-03-22 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2012-03-24 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: cucumber
16
- requirement: &73904440 !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
17
24
  none: false
18
- requirements:
25
+ requirements:
19
26
  - - ~>
20
- - !ruby/object:Gem::Version
21
- version: '1.1'
27
+ - !ruby/object:Gem::Version
28
+ hash: 13
29
+ segments:
30
+ - 1
31
+ - 1
32
+ version: "1.1"
22
33
  type: :development
23
- prerelease: false
24
- version_requirements: *73904440
25
- - !ruby/object:Gem::Dependency
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
26
36
  name: rspec
27
- requirement: &73904110 !ruby/object:Gem::Requirement
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
28
39
  none: false
29
- requirements:
40
+ requirements:
30
41
  - - ~>
31
- - !ruby/object:Gem::Version
32
- version: '2.9'
42
+ - !ruby/object:Gem::Version
43
+ hash: 17
44
+ segments:
45
+ - 2
46
+ - 9
47
+ version: "2.9"
33
48
  type: :development
34
- prerelease: false
35
- version_requirements: *73904110
36
- - !ruby/object:Gem::Dependency
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
37
51
  name: activesupport
38
- requirement: &73903820 !ruby/object:Gem::Requirement
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
39
54
  none: false
40
- requirements:
55
+ requirements:
41
56
  - - ~>
42
- - !ruby/object:Gem::Version
43
- version: '3.0'
57
+ - !ruby/object:Gem::Version
58
+ hash: 7
59
+ segments:
60
+ - 3
61
+ - 0
62
+ version: "3.0"
44
63
  type: :runtime
45
- prerelease: false
46
- version_requirements: *73903820
64
+ version_requirements: *id003
47
65
  description: Provides convenient tool for storing configuration
48
- email:
66
+ email:
49
67
  - cheef.che@gmail.com
50
68
  executables: []
69
+
51
70
  extensions: []
71
+
52
72
  extra_rdoc_files: []
53
- files:
73
+
74
+ files:
54
75
  - lib/config-me/version.rb
76
+ - lib/config-me/importer.rb
55
77
  - lib/config-me/errors.rb
56
78
  - lib/config-me/definitions_parser.rb
57
- - lib/config-me/configuration.rb
79
+ - lib/config-me/defaults.rb
80
+ - lib/config-me/base.rb
58
81
  - lib/config-me/node.rb
82
+ - lib/config-me/runner.rb
59
83
  - lib/config-me.rb
60
84
  homepage: http://github.com/cheef/config-me
61
85
  licenses: []
86
+
62
87
  post_install_message:
63
88
  rdoc_options: []
64
- require_paths:
89
+
90
+ require_paths:
65
91
  - lib
66
- required_ruby_version: !ruby/object:Gem::Requirement
92
+ required_ruby_version: !ruby/object:Gem::Requirement
67
93
  none: false
68
- requirements:
69
- - - ! '>='
70
- - !ruby/object:Gem::Version
71
- version: '0'
72
- required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
102
  none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '0'
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
78
110
  requirements: []
111
+
79
112
  rubyforge_project: config-me
80
113
  rubygems_version: 1.8.10
81
114
  signing_key:
82
115
  specification_version: 3
83
116
  summary: Provides convenient tool for storing configuration
84
117
  test_files: []
118
+
119
+ has_rdoc:
@@ -1,17 +0,0 @@
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
- @tree.send method, *args, &block
16
- end
17
- end