confiture 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ ## 0.1.4
2
+
3
+ - fixing https://github.com/phoet/confiture/issues/1
4
+
1
5
  ## 0.1.3
2
6
 
3
7
  - raising an error when using config before initialization
data/README.md CHANGED
@@ -32,13 +32,13 @@ Rails style initializer (config/initializers/asin.rb):
32
32
 
33
33
  YAML style configuration:
34
34
 
35
- Your::Configuration.configure :yaml => 'config/some.yml'
35
+ Your::Configuration.configure yaml: 'config/some.yml'
36
36
 
37
37
  Inline style configuration:
38
38
 
39
- Your::Configuration.configure :secret => 'your-secret', :key => 'your-key'
39
+ Your::Configuration.configure secret: 'your-secret', key: 'your-key'
40
40
  # or
41
- client.configure :secret => 'your-secret', :key => 'your-key'
41
+ client.configure secret: 'your-secret', key: 'your-key'
42
42
 
43
43
  ## License
44
44
 
@@ -39,13 +39,13 @@ module Confiture
39
39
  #
40
40
  # You may pass options as a hash as well:
41
41
  #
42
- # Confiture::Configuration.configure :secret => 'your-secret', :key => 'your-key'
42
+ # Confiture::Configuration.configure secret: 'your-secret', key: 'your-key'
43
43
  #
44
44
  # Or configure everything using YAML:
45
45
  #
46
- # Confiture::Configuration.configure :yaml => 'config/asin.yml'
46
+ # Confiture::Configuration.configure yaml: 'config/asin.yml'
47
47
  #
48
- # Confiture::Configuration.configure :yaml => 'config/asin.yml' do |config, yml|
48
+ # Confiture::Configuration.configure yaml: 'config/asin.yml' do |config, yml|
49
49
  # config.key = yml[Rails.env]['key']
50
50
  # end
51
51
  #
@@ -53,7 +53,7 @@ module Confiture
53
53
  #
54
54
  # [yaml|yml] path to a yaml file with configuration
55
55
  #
56
- def configure(options={},reset=false)
56
+ def configure(options = {}, reset = false)
57
57
  init_config(reset)
58
58
  if yml_path = options[:yaml] || options[:yml]
59
59
  yml = File.open(yml_path) { |file| YAML.load(file) }
@@ -77,10 +77,11 @@ module Confiture
77
77
  # Run a block of code with temporary configuration.
78
78
  #
79
79
  def with_config(options={})
80
- current_data = data
81
- configure(options, true)
80
+ self.current_data = data
81
+ configure(options)
82
+ yield
82
83
  ensure
83
- self.data = current_data
84
+ self.current_data = nil
84
85
  end
85
86
 
86
87
  # Resets configuration to defaults
@@ -130,11 +131,20 @@ module Confiture
130
131
  private
131
132
 
132
133
  def data
133
- Thread.current[:confiture]
134
+ current_data || @data
134
135
  end
135
136
 
136
137
  def data=(data)
137
- Thread.current[:confiture] = data
138
+ @data = data
139
+ end
140
+
141
+ def current_data
142
+ Thread.current["__confiture_#{self}"]
143
+ end
144
+
145
+ def current_data=(data)
146
+ data = Data.new(data.options) unless data.nil?
147
+ Thread.current["__confiture_#{self}"] = data
138
148
  end
139
149
 
140
150
  def init_config(force=false)
@@ -144,13 +154,15 @@ module Confiture
144
154
  end
145
155
 
146
156
  def method_missing(meth, *args)
147
- meth = "#{meth}"
148
- if meth =~ /.+=/ && args.size == 1
149
- key = meth[0..-2].to_sym
157
+ key = "#{meth}"
158
+ if respond_to?(meth)
159
+ super
160
+ elsif key =~ /.+=/ && args.size == 1
161
+ key = key[0..-2].to_sym
150
162
  validate_key!(key)
151
163
  data.options[key] = args.last
152
164
  elsif args.size == 0
153
- key = meth.to_sym
165
+ key = key.to_sym
154
166
  validate_key!(key)
155
167
  data.options[key]
156
168
  else
@@ -1,3 +1,3 @@
1
1
  module Confiture
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -8,4 +8,4 @@ RSpec::Core::RakeTask.new do |t|
8
8
  t.pattern = 'spec/**/*_spec.rb'
9
9
  end
10
10
 
11
- task :default => :spec
11
+ task default: :spec
@@ -17,13 +17,13 @@ module Confiture
17
17
 
18
18
  class Default
19
19
  include Confiture::Configuration
20
- confiture_defaults(:key => 'value')
20
+ confiture_defaults(key: 'value')
21
21
  end
22
22
 
23
23
  describe Confiture do
24
24
  context "reset" do
25
25
  it "should reset the configuration" do
26
- Config.configure(:key => "value").tap do |config|
26
+ Config.configure(key: "value").tap do |config|
27
27
  config.key.should eql("value")
28
28
  config.reset!
29
29
  config.key.should be_nil
@@ -49,7 +49,7 @@ module Confiture
49
49
 
50
50
  it "should restore defaults" do
51
51
  Default.configure.key.should eql('value')
52
- Default.configure(:key => 'bla').key.should eql('bla')
52
+ Default.configure(key: 'bla').key.should eql('bla')
53
53
  Default.reset!
54
54
  Default.configure.key.should eql('value')
55
55
  end
@@ -85,14 +85,14 @@ module Confiture
85
85
  end
86
86
 
87
87
  it "should read configuration from yml" do
88
- Config.configure(:yaml => 'spec/config.yml').tap do |config|
88
+ Config.configure(yaml: 'spec/config.yml').tap do |config|
89
89
  config.secret.should eql('secret_yml')
90
90
  config.key.should eql('key_yml')
91
91
  end
92
92
  end
93
93
 
94
94
  it "should read configuration from yml with block" do
95
- conf = Config.configure :yaml => 'spec/config.yml' do |config, yml|
95
+ conf = Config.configure yaml: 'spec/config.yml' do |config, yml|
96
96
  config.secret = nil
97
97
  config.key = yml['secret']
98
98
  end
@@ -103,13 +103,49 @@ module Confiture
103
103
 
104
104
  context "with_config" do
105
105
  it "should have a config on a per block basis" do
106
- Config.configure(:key => "value").tap do |config|
107
- config.with_config(:key => "bla") do
108
- Config.key.should eql("bla")
109
- end
106
+ Config.configure(key: "value")
107
+ Config.key.should eql("value")
108
+ Config.with_config(key: "bla") do
109
+ Config.key.should eql("bla")
110
110
  end
111
111
  Config.key.should eql("value")
112
112
  end
113
113
  end
114
+
115
+ context "scoping" do
116
+ it "should scope to the configuration class" do
117
+ mapping = {
118
+ Config => "bla",
119
+ Valid => "nups",
120
+ Allowed => "blupp",
121
+ Default => "nups",
122
+ }
123
+ mapping.each do |clazz, value|
124
+ clazz.reset!
125
+ clazz.configure(key: value)
126
+ end
127
+ mapping.each do |clazz, value|
128
+ clazz.key.should eql(value)
129
+ end
130
+ end
131
+
132
+ it "should allow threads to override configuration" do
133
+ Config.reset!
134
+ Config.configure(key: "nups")
135
+ t = Thread.new do
136
+ Config.key.should eql("nups")
137
+ Thread.stop
138
+ Config.key.should eql("nups")
139
+ end
140
+ sleep 0.1 while t.status != 'sleep'
141
+ Config.with_config(key: "dups") do
142
+ Config.key.should eql("dups")
143
+ t.run
144
+ t.join
145
+ Config.key.should eql("dups")
146
+ end
147
+ Config.key.should eql("nups")
148
+ end
149
+ end
114
150
  end
115
151
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: confiture
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-01 00:00:00.000000000 Z
12
+ date: 2012-11-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake