omniconf 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -36,10 +36,12 @@ $ rails c
36
36
  => 123
37
37
  > Omniconf.configuration.api.username # it works with nested values too
38
38
  => "root"
39
- > Omniconf.configuration.api # outputs a hash if nested
39
+ > Omniconf.configuration.api # returns a hash if nested
40
40
  => {"username"=>"root"}
41
+ > Omniconf.configuration.non_existant # returns nil
42
+ => nil
41
43
  > Omniconf.configuration.non_existant.config_value # raises an exception
42
- => Omniconf::UnknownConfigurationValue: cannot get a configuration value with no parent
44
+ => NoMethodError: undefined method `config_value' for nil:NilClass
43
45
  ```
44
46
 
45
47
  ## Setting values
@@ -52,7 +54,7 @@ $ rails c
52
54
  > Omniconf.configuration.api.username = "admin" # it works with nested values too
53
55
  => "admin"
54
56
  > Omniconf.configuration.brand_new_value = "whatever" # raises an exception because you've got to tell which back-end will store the new value
55
- => Omniconf::UnknownConfigurationValue: cannot set a configuration value with no parent
57
+ => Omniconf::UnknownConfigurationValue: cannot set a configuration value with no parents
56
58
  > Omniconf.sources[:database].brand_new_value = "whatever" # adds a new record in ConfigValue model
57
59
  => "whatever"
58
60
  ```
data/lib/omniconf.rb CHANGED
@@ -43,13 +43,14 @@ module Omniconf
43
43
  params[:adapter].load_configuration!
44
44
  Omniconf.logger.info "Loaded configuration from #{source_id.inspect} source"
45
45
  end
46
+ Omniconf.logger.info "Global configuration: #{configuration.inspect}"
46
47
  end
47
48
 
48
49
  alias_method :reload_configuration!, :load_configuration!
49
50
 
50
51
  def merge_configuration! source_id
51
- Omniconf.logger.debug "Merging from #{source_id.inspect} source"
52
52
  source_config = Omniconf.sources[source_id].to_hash
53
+ Omniconf.logger.debug "Merging #{source_config.inspect} from #{source_id.inspect} source"
53
54
  global_config = Omniconf.configuration.to_hash
54
55
  global_config.recursive_merge!(source_config) do |key, old_val, new_val|
55
56
  Omniconf.logger.warn \
@@ -74,7 +75,7 @@ module Omniconf
74
75
  end
75
76
  end
76
77
  raise UnknownConfigurationValue,
77
- "cannot set a configuration value with no parent" unless found
78
+ "cannot set a configuration value with no parents" unless found
78
79
  end
79
80
 
80
81
  private
@@ -82,12 +83,16 @@ module Omniconf
82
83
  return unless @settings.sources
83
84
  @settings.sources.each do |source_id, params|
84
85
  adapter_file = params[:type].to_s
86
+
85
87
  require "omniconf/adapters/#{adapter_file}"
88
+
86
89
  adapter_class = adapter_file.split('_').map {|w| w.capitalize}.join
87
90
  raise unless params[:adapter].nil?
91
+
88
92
  params[:adapter] = Omniconf::Adapter.class_eval do
89
93
  const_get(adapter_class).new(source_id, params)
90
94
  end
95
+
91
96
  Omniconf.logger.debug "Registered #{adapter_class}::#{source_id} source"
92
97
  end
93
98
  end
@@ -1,8 +1,10 @@
1
1
  module Omniconf
2
2
  class BlankSlate
3
- instance_methods.each do |method|
4
- undef_method method unless method =~ /^__/ or
5
- [:inspect, :instance_of?, :object_id, :should].include? method.to_sym
3
+ # do not undef these methods
4
+ WHITELIST = %w(inspect object_id)
5
+
6
+ instance_methods.map(&:to_s).each do |method|
7
+ undef_method method unless method.start_with?('__') || WHITELIST.include?(method)
6
8
  end
7
9
  end
8
10
  end
@@ -27,13 +27,11 @@ module Omniconf
27
27
 
28
28
  def method_missing(method, *args)
29
29
  raise NoMethodError, "undefined method `#{method}' for #{self}" \
30
- if method.to_s[0..1] == '__' # will save hours tracking down heisenbugs
30
+ if method.to_s.start_with? '__' # will save hours tracking down heisenbugs
31
31
 
32
32
  len = args.length
33
- if new_key = method.to_s.chomp!('=') # write
34
- if len == 1
35
- key, value = new_key, args[0]
36
- else
33
+ if key = method.to_s.chomp!('=') # write
34
+ unless len == 1 # args[0] is the value
37
35
  raise ArgumentError, "wrong number of arguments (#{len} for 1)"
38
36
  end
39
37
 
@@ -45,12 +43,12 @@ module Omniconf
45
43
  parent = parent[:object].__parent
46
44
  end
47
45
 
48
- @__adapter.set_value(full_key, value) # notify the adapter
46
+ @__adapter.set_value(full_key, args[0]) # notify the adapter
49
47
  else
50
48
  Omniconf.logger.warn "No adapter to notify"
51
49
  end
52
50
 
53
- @__table[key] = value # update our internal config hash
51
+ @__table[key] = args[0] # update our internal config hash
54
52
 
55
53
  if @__adapter.is_a? Omniconf::Adapter::Base
56
54
  # we need to merge the global config
@@ -63,21 +61,6 @@ module Omniconf
63
61
  if value.is_a?(Hash)
64
62
  Configuration.new(@__adapter, value, {:root => key, :object => self})
65
63
  else
66
- unless value
67
- # add a catch-all exception
68
- # (more descriptive than "undefined method `xxx' for nil:NilClass")
69
- class << value
70
- def method_missing method, *args
71
- if method == :to_str
72
- # need to raise NoMethodError here for Ruby 1.9.2 compatibility
73
- raise NoMethodError, "undefined method `#{method}' for #{self}"
74
- else
75
- raise UnknownConfigurationValue,
76
- "cannot get a configuration value with no parent"
77
- end
78
- end
79
- end
80
- end
81
64
  value
82
65
  end
83
66
 
@@ -1,3 +1,3 @@
1
1
  module Omniconf
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
data/omniconf.gemspec CHANGED
@@ -5,9 +5,9 @@ require "omniconf/version"
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "omniconf"
7
7
  s.version = Omniconf::VERSION
8
- s.authors = ["Cedric Felizard"]
9
- s.email = ["cedric@picklive.com"]
10
- s.homepage = "https://github.com/Picklive/omniconf"
8
+ s.authors = ["Cédric Félizard"]
9
+ s.email = ["cedric@felizard.fr"]
10
+ s.homepage = "https://github.com/infertux/omniconf"
11
11
  s.summary = %q{Merge multiple configuration sources into one.}
12
12
  s.description = %q{Merge configurations from multiple back-ends for easy use in a complex application.}
13
13
 
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  s.add_development_dependency 'rake', '~> 0.9'
22
- s.add_development_dependency 'rspec', '~> 2.8'
22
+ s.add_development_dependency 'rspec', '~> 2.10'
23
23
  s.add_development_dependency 'sqlite3', '~> 1.3'
24
24
  s.add_development_dependency 'activerecord', '~> 3.2'
25
25
  end
@@ -1,10 +1,18 @@
1
+ common: &common
2
+ common_key: common_value
3
+ override_key: override_me
4
+
1
5
  test:
6
+ <<: *common
2
7
  yaml_key: yaml_value
3
- nested:
4
- yaml_key: nested_yaml_value
5
- overridden: by_yaml
8
+ override_key: override_value
6
9
  some_integer: 1337
7
10
  some_float: 3.14
8
11
  some_array: [1, 2, 3]
12
+ nested:
13
+ yaml_key: nested_yaml_value
14
+
9
15
  development:
16
+ <<: *common
10
17
  yaml_key: yaml_value_dev
18
+
@@ -86,6 +86,17 @@ describe Omniconf::Adapter::Yaml do
86
86
  config.yaml_key.should == 'yaml_value_dev'
87
87
  end
88
88
 
89
+ it "merges common values" do
90
+ %w(test development).each do |env|
91
+ load_configuration env
92
+ config.common_key.should == 'common_value'
93
+ end
94
+ end
95
+
96
+ it "overrides common values" do
97
+ config.override_key.should == 'override_value'
98
+ end
99
+
89
100
  it "keeps value types" do
90
101
  config.some_integer.should == 1337
91
102
  config.some_float.should == 3.14
@@ -17,6 +17,11 @@ describe Omniconf::Configuration do
17
17
  }
18
18
  end
19
19
 
20
+ it "is blank slated" do
21
+ # check we don't have the 40-ish methods inherited from the Object class
22
+ Omniconf::Configuration.new(nil).hash.should be_nil
23
+ end
24
+
20
25
  describe "#initialize" do
21
26
  it "takes a hash as argument" do
22
27
  expect { Omniconf::Configuration.new nil, hash }.to_not raise_error
@@ -3,7 +3,7 @@ require 'omniconf/helpers'
3
3
 
4
4
  describe Hash do
5
5
  describe "#recursive_stringify_keys!" do
6
- it "stringify hash's keys recursively" do
6
+ it "stringify hash keys recursively" do
7
7
  hash = {:a => 1, :b => {:aa => {:aaa => 'AAA', 'bbb' => nil}}}
8
8
  hash.recursive_stringify_keys!
9
9
  hash.should == {'a' => 1, 'b' => {'aa' => {'aaa' => 'AAA', 'bbb' => nil}}}
@@ -53,10 +53,6 @@ describe Omniconf do
53
53
  Omniconf.configuration
54
54
  end
55
55
 
56
- it "returns an instance of Omniconf::Configuration" do
57
- config.should be_an_instance_of Omniconf::Configuration
58
- end
59
-
60
56
  describe "getting values" do
61
57
  it "loads and merges all configurations" do
62
58
  config.yaml_key.should == 'yaml_value'
@@ -89,7 +85,7 @@ describe Omniconf do
89
85
  end
90
86
 
91
87
  it "fails to create a new value with no sources" do
92
- # How the hell would I know where to store it?!
88
+ # since we cannot know in which source to save it
93
89
  config.no.should be_nil
94
90
  expect {
95
91
  config.no = 'whatever'
@@ -102,7 +98,7 @@ describe Omniconf do
102
98
 
103
99
  expect {
104
100
  Omniconf.sources[:database].no.no = 'new_ar_value'
105
- }.to raise_error Omniconf::UnknownConfigurationValue
101
+ }.to raise_error NoMethodError
106
102
  end
107
103
  end
108
104
  end
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniconf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - Cedric Felizard
8
+ - Cédric Félizard
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-12 00:00:00.000000000 Z
12
+ date: 2012-06-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &2152356800 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,21 +21,31 @@ dependencies:
21
21
  version: '0.9'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2152356800
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.9'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: rspec
27
- requirement: &2152342400 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ~>
31
36
  - !ruby/object:Gem::Version
32
- version: '2.8'
37
+ version: '2.10'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *2152342400
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.10'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: sqlite3
38
- requirement: &2152340720 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ~>
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '1.3'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *2152340720
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: activerecord
49
- requirement: &2152339860 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ~>
@@ -54,11 +69,16 @@ dependencies:
54
69
  version: '3.2'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *2152339860
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '3.2'
58
78
  description: Merge configurations from multiple back-ends for easy use in a complex
59
79
  application.
60
80
  email:
61
- - cedric@picklive.com
81
+ - cedric@felizard.fr
62
82
  executables: []
63
83
  extensions: []
64
84
  extra_rdoc_files: []
@@ -95,7 +115,7 @@ files:
95
115
  - spec/omniconf/helpers_spec.rb
96
116
  - spec/omniconf_spec.rb
97
117
  - spec/spec_helper.rb
98
- homepage: https://github.com/Picklive/omniconf
118
+ homepage: https://github.com/infertux/omniconf
99
119
  licenses: []
100
120
  post_install_message:
101
121
  rdoc_options: []
@@ -115,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
135
  version: '0'
116
136
  requirements: []
117
137
  rubyforge_project: omniconf
118
- rubygems_version: 1.8.10
138
+ rubygems_version: 1.8.24
119
139
  signing_key:
120
140
  specification_version: 3
121
141
  summary: Merge multiple configuration sources into one.