settable 1.1 → 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,261 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Basic Usage' do
4
+ it 'should allow setting' do
5
+ c = Settable.configure do
6
+ set :key, 'value'
7
+ end
8
+
9
+ c.key.should eq 'value'
10
+ end
11
+
12
+ it 'should allow block settings' do
13
+ c = Settable.configure do
14
+ set :key do
15
+ 'value'
16
+ end
17
+ end
18
+
19
+ c.key.should eq 'value'
20
+ end
21
+
22
+ it 'should have truthy methods' do
23
+ c = Settable.configure do
24
+ set :key do
25
+ 'value'
26
+ end
27
+ end
28
+
29
+ c.key?.should be_true
30
+ end
31
+ end
32
+
33
+ describe Settable::Namespace do
34
+ it 'should create a namespace' do
35
+ c = Settable.configure do
36
+ namespace :test do
37
+ set :key, 'value'
38
+ end
39
+ end
40
+
41
+ c.test.key.should eq 'value'
42
+ end
43
+
44
+ it 'should support nesting' do
45
+ c = Settable.configure do
46
+ namespace :a do
47
+ namespace :b do
48
+ set :key, 'value'
49
+ end
50
+ end
51
+ end
52
+
53
+ c.a.b.key.should eq 'value'
54
+ end
55
+ end
56
+
57
+ describe Settable::Environment do
58
+ module EnvTester
59
+ def self.call(environment)
60
+ environment.to_sym == :test
61
+ end
62
+ end
63
+
64
+ it 'should check the environment' do
65
+ c = Settable.configure do
66
+ use_environment EnvTester
67
+
68
+ set :key do
69
+ environment :test, 'value'
70
+ 'default'
71
+ end
72
+ end
73
+
74
+ c.key.should eq 'value'
75
+ end
76
+
77
+ it 'should default unless matched' do
78
+ c = Settable.configure do
79
+ use_environment EnvTester
80
+
81
+ set :key do
82
+ environment :bad_environment, 'value'
83
+ 'default'
84
+ end
85
+ end
86
+
87
+ c.key.should eq 'default'
88
+ end
89
+ end
90
+
91
+ describe Settable::Environment::Env do
92
+ subject do
93
+ Settable.configure do
94
+ use_environment :env
95
+
96
+ set :key do
97
+ environment :TEST, 'test found!'
98
+ 'default'
99
+ end
100
+ end
101
+ end
102
+
103
+ it 'should find matching key in ENV' do
104
+ ENV['TEST'] = '1'
105
+ subject.key.should eq 'test found!'
106
+ end
107
+
108
+ it 'should not find matching key in ENV' do
109
+ ENV.delete('TEST')
110
+ subject.key.should eq 'default'
111
+ end
112
+ end
113
+
114
+ describe Settable::Environment::Rails do
115
+ module ::Rails
116
+ def self.env=(env)
117
+ @env = env
118
+ end
119
+
120
+ def self.env
121
+ @env
122
+ end
123
+ end
124
+
125
+ subject do
126
+ Settable.configure do
127
+ use_environment :rails
128
+
129
+ set :key do
130
+ environment [:qa, :staging], 'qa_or_staging'
131
+ environment :development, 'development'
132
+ environment :production, 'production'
133
+ environment :test, 'test'
134
+ environment(:block){ 'block' }
135
+
136
+ 'default'
137
+ end
138
+
139
+ namespace :segment do
140
+ set :key do
141
+ environment [:qa, :staging], 'qa_or_staging'
142
+ environment :development, 'development'
143
+ environment :production, 'production'
144
+ environment :test, 'test'
145
+ environment(:block){ 'block' }
146
+
147
+ 'default'
148
+ end
149
+ end
150
+ end
151
+ end
152
+
153
+ describe '#environment_matches?' do
154
+ it 'should return false when not set' do
155
+ subject.instance_eval do
156
+ set :test, environment_matches?(:qa, :staging)
157
+ end
158
+
159
+ subject.test.should be_false
160
+ end
161
+
162
+ it 'should return true when set' do
163
+ Rails.env = :qa
164
+
165
+ subject.instance_eval do
166
+ set :test, environment_matches?(:qa, :staging)
167
+ end
168
+
169
+ subject.test.should be_true
170
+ end
171
+ end
172
+
173
+ it 'should match arrays' do
174
+ Rails.env = :qa
175
+ subject.key.should eq 'qa_or_staging'
176
+ subject.segment.key.should eq 'qa_or_staging'
177
+
178
+ Rails.env = :staging
179
+ subject.key.should eq 'qa_or_staging'
180
+ subject.segment.key.should eq 'qa_or_staging'
181
+ end
182
+
183
+ it 'should match development' do
184
+ Rails.env = :development
185
+ subject.key.should eq 'development'
186
+ subject.segment.key.should eq 'development'
187
+ end
188
+
189
+ it 'should match production' do
190
+ Rails.env = :production
191
+ subject.key.should eq 'production'
192
+ subject.segment.key.should eq 'production'
193
+ end
194
+
195
+ it 'should match test' do
196
+ Rails.env = :test
197
+ subject.key.should eq 'test'
198
+ subject.segment.key.should eq 'test'
199
+ end
200
+
201
+ it 'should not match bad environments' do
202
+ Rails.env = :unknown_bad_environment
203
+ subject.key.should eq 'default'
204
+ subject.segment.key.should eq 'default'
205
+ end
206
+
207
+ it 'should handle blocks' do
208
+ Rails.env = :block
209
+ subject.key.should eq 'block'
210
+ subject.segment.key.should eq 'block'
211
+ end
212
+ end
213
+
214
+ describe Settable::DSL do
215
+ class TestConfig1
216
+ include Settable
217
+
218
+ settable :config do
219
+ set :hello, 'world'
220
+ end
221
+
222
+ def something
223
+ config.hello
224
+ end
225
+ end
226
+
227
+ it 'instances methods should have access' do
228
+ TestConfig1.new.something.should eq 'world'
229
+ end
230
+
231
+ it 'class should have access' do
232
+ TestConfig1.config.hello.should eq 'world'
233
+ end
234
+
235
+ it 'instnace should have generated method' do
236
+ TestConfig1.new.config.should be_kind_of(Settable::Namespace)
237
+ end
238
+ end
239
+
240
+ describe 'Interpolating config' do
241
+ class TestConfig2
242
+ include Settable
243
+
244
+ settable :config do
245
+ set :hello, "hello"
246
+ set :hello_world, "#{hello} world!"
247
+
248
+ set :hello_world_block do
249
+ "#{root.hello} world!"
250
+ end
251
+ end
252
+ end
253
+
254
+ it 'should interpolate values from config' do
255
+ TestConfig2.config.hello_world.should eq 'hello world!'
256
+ end
257
+
258
+ it 'should interpolate values from config (within a block)' do
259
+ TestConfig2.config.hello_world_block.should eq 'hello world!'
260
+ end
261
+ end
@@ -0,0 +1,8 @@
1
+ $:.push File.expand_path("../../lib", __FILE__)
2
+ require 'settable'
3
+
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ config.filter_run :focus
8
+ end
metadata CHANGED
@@ -1,16 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: settable
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.1'
5
- prerelease:
4
+ version: 3.0.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Rob Hurring
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-01-20 00:00:00.000000000Z
13
- dependencies: []
11
+ date: 2014-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
14
27
  description: Small include to make config files better
15
28
  email:
16
29
  - robhurring@gmail.com
@@ -19,37 +32,40 @@ extensions: []
19
32
  extra_rdoc_files: []
20
33
  files:
21
34
  - .gitignore
35
+ - .rspec
36
+ - .travis.yml
22
37
  - Gemfile
23
38
  - Rakefile
24
39
  - Readme.md
25
- - examples/example.rb
26
- - examples/rails.rb
27
40
  - lib/settable.rb
41
+ - lib/settable/environment/env.rb
42
+ - lib/settable/environment/rails.rb
28
43
  - settable.gemspec
29
- - test/settable_test.rb
30
- homepage: ''
44
+ - spec/settable_spec.rb
45
+ - spec/spec_helper.rb
46
+ homepage: https://github.com/robhurring/settable
31
47
  licenses: []
48
+ metadata: {}
32
49
  post_install_message:
33
50
  rdoc_options: []
34
51
  require_paths:
35
52
  - lib
36
53
  required_ruby_version: !ruby/object:Gem::Requirement
37
- none: false
38
54
  requirements:
39
- - - ! '>='
55
+ - - '>='
40
56
  - !ruby/object:Gem::Version
41
57
  version: '0'
42
58
  required_rubygems_version: !ruby/object:Gem::Requirement
43
- none: false
44
59
  requirements:
45
- - - ! '>='
60
+ - - '>='
46
61
  - !ruby/object:Gem::Version
47
62
  version: '0'
48
63
  requirements: []
49
- rubyforge_project: settable
50
- rubygems_version: 1.8.6
64
+ rubyforge_project:
65
+ rubygems_version: 2.0.6
51
66
  signing_key:
52
- specification_version: 3
67
+ specification_version: 4
53
68
  summary: Small include to make config files better
54
69
  test_files:
55
- - test/settable_test.rb
70
+ - spec/settable_spec.rb
71
+ - spec/spec_helper.rb
data/examples/example.rb DELETED
@@ -1,43 +0,0 @@
1
- $:.push File.expand_path("../../lib", __FILE__)
2
- require 'settable'
3
-
4
- class Configuration
5
- include Settable
6
-
7
- def initialize(&block)
8
- instance_eval(&block) if block_given?
9
- end
10
- end
11
-
12
- config = Configuration.new do
13
- set :environment, 'development'
14
-
15
- set :api_token do
16
- return 'PRODTOKEN' if environment == 'production'
17
- 'DEVTOKEN'
18
- end
19
-
20
- set :api_endpoint, "http://example.com/api/#{api_token}"
21
- set :environment, 'production'
22
-
23
- namespace :chat do
24
- set :enabled, true
25
- set :token, 'abcde90210'
26
- end
27
- end
28
-
29
- # external stuffs
30
- config.set :something, 1
31
- config.enable :debug
32
-
33
- puts config.inspect
34
-
35
- puts '-' * 80
36
-
37
- puts config.environment
38
- puts config.api_token
39
- puts config.api_endpoint
40
- puts config.something
41
- puts config.debug?
42
- puts config.chat.enabled?
43
- puts config.chat.token
data/examples/rails.rb DELETED
@@ -1,65 +0,0 @@
1
- $:.push File.expand_path("../../lib", __FILE__)
2
- require 'settable'
3
-
4
- # stub out our rails
5
- module Rails
6
- def self.env; :blah end
7
- end
8
-
9
- class Configuration
10
- include Settable
11
- include Settable::Rails
12
-
13
- def initialize(&block)
14
- instance_eval(&block) if block_given?
15
- end
16
- end
17
-
18
- @config = Configuration.new do
19
- define_environments :blah, :qa
20
-
21
- set :something, in_blah?
22
- set :debug, in_environments?(:development, :test)
23
-
24
- if in_production?
25
- enable :tracking, :caching
26
- else
27
- disable :tracking, :caching
28
- end
29
-
30
- namespace :api do
31
- set :token do
32
- in_production { return 'PRODTOKEN' }
33
- in_development{ return 'DEVTOKEN' }
34
- 'OTHERTOKEN'
35
- end
36
-
37
- set :endpoint do
38
- in_environments(:development, :test){ return "http://sandbox.example.com/api/#{token}" }
39
- "http://example.com/api/#{token}"
40
- end
41
- end
42
-
43
- set :api_token do
44
- in_production { return 'PRODTOKEN' }
45
- in_development{ return 'DEVTOKEN' }
46
- 'OTHERTOKEN'
47
- end
48
-
49
- set :api_endpoint do
50
- end
51
- end
52
-
53
- # external stuffs
54
- puts @config.inspect
55
-
56
- puts '-' * 80
57
-
58
- puts @config.api.token
59
- puts @config.api.endpoint
60
-
61
- puts @config.tracking?
62
- puts @config.caching?
63
- puts @config.api_token
64
- puts @config.api_endpoint
65
- puts @config.debug?
@@ -1,252 +0,0 @@
1
- $:.push File.expand_path("../../lib", __FILE__)
2
- require 'test/unit'
3
- require 'settable'
4
-
5
- module Rails
6
- class << self; attr_accessor :env; end
7
- self.env = :development
8
- end
9
-
10
- class Configuration
11
- include Settable
12
- def initialize(&block)
13
- instance_eval(&block) if block_given?
14
- end
15
- end
16
-
17
- class RailsConfiguration < Configuration
18
- include Settable::Rails
19
- end
20
-
21
- # test using an instance eval'd config
22
- class SettableBlockTest < Test::Unit::TestCase
23
- def setup
24
- @config = Configuration.new do
25
- set :string, 'hello world'
26
- set :numeric, 10
27
- set :block do
28
- 'block'
29
- end
30
- set :combined, "#{string}-combined"
31
- enable :tracking
32
- disable :caching
33
- end
34
- end
35
-
36
- def test_settables
37
- assert_equal [:string, :numeric, :block, :combined, :tracking, :caching].sort, @config.__settables__.sort
38
- end
39
-
40
- def test_strings
41
- assert_equal 'hello world', @config.string
42
- end
43
-
44
- def test_numeric
45
- assert_equal 10, @config.numeric
46
- end
47
-
48
- def test_block
49
- assert_equal 'block', @config.block
50
- end
51
-
52
- def test_string_interpolation
53
- assert_equal 'hello world-combined', @config.combined
54
- end
55
-
56
- def test_enable
57
- assert @config.tracking
58
- end
59
-
60
- def test_disable
61
- assert !@config.caching
62
- end
63
-
64
- def test_question_marks
65
- assert @config.combined? == true
66
- end
67
- end
68
-
69
- # test individual setting config
70
- class SettableExternalTest < Test::Unit::TestCase
71
- def setup
72
- @config = Configuration.new
73
- end
74
-
75
- def test_strings
76
- @config.set :string, 'hello world'
77
- assert_equal 'hello world', @config.string
78
- end
79
-
80
- def test_numeric
81
- @config.set :numeric, 10
82
- assert_equal 10, @config.numeric
83
- end
84
-
85
- def test_block
86
- @config.set :block do
87
- 'block'
88
- end
89
- assert_equal 'block', @config.block
90
- end
91
-
92
- def test_enable
93
- @config.enable :tracking
94
- assert @config.tracking
95
- end
96
-
97
- def test_disable
98
- @config.disable :caching
99
- assert !@config.caching
100
- end
101
-
102
- def test_block_with_value
103
- @config.set :value, 'value'
104
- @config.set :block do
105
- value
106
- end
107
- assert_equal 'value', @config.block
108
- end
109
-
110
- def test_string_interpolation
111
- @config.set :hello, 'hello'
112
- @config.set :combined, "#{@config.hello}-world"
113
- assert_equal 'hello-world', @config.combined
114
- end
115
-
116
- def test_question_marks
117
- @config.set :hello, 'world'
118
- assert @config.hello? == true
119
- end
120
- end
121
-
122
- class SettableRailsTest < Test::Unit::TestCase
123
- def setup
124
- @config = RailsConfiguration.new do
125
- define_environments :custom
126
- set :debug, in_development?
127
-
128
- set :api_token do
129
- in_production{ return 'PROD' }
130
- in_custom{ return 'CUSTOM' }
131
- 'DEFAULT'
132
- end
133
-
134
- set :api_endpoint do
135
- in_environments(:development, :test){ return 'dev.example.com' }
136
- 'example.com'
137
- end
138
- end
139
- end
140
-
141
- def test_settables
142
- assert_equal [:debug, :api_endpoint, :api_token].sort, @config.__settables__.sort
143
- end
144
-
145
- def test_in_environment_helper
146
- Rails.env = :development
147
- assert @config.in_development? == true
148
- end
149
-
150
- def test_custom_environments
151
- Rails.env = :custom
152
- assert @config.in_custom? == true
153
- end
154
-
155
- def test_development_block
156
- Rails.env = :development
157
- assert_equal 'DEFAULT', @config.api_token
158
- end
159
-
160
- def test_production_block
161
- Rails.env = :production
162
- assert_equal 'PROD', @config.api_token
163
- end
164
-
165
- def test_custom_block
166
- Rails.env = :custom
167
- assert_equal 'CUSTOM', @config.api_token
168
- end
169
-
170
- def test_multiple_environments_in_development
171
- Rails.env = :development
172
- assert_equal 'dev.example.com', @config.api_endpoint
173
- end
174
-
175
- def test_multiple_environments_in_test
176
- Rails.env = :test
177
- assert_equal 'dev.example.com', @config.api_endpoint
178
- end
179
-
180
- def test_multiple_environments_in_other
181
- Rails.env = :production
182
- assert_equal 'example.com', @config.api_endpoint
183
- end
184
-
185
- def test_question_marks
186
- assert @config.api_endpoint? == true
187
- end
188
- end
189
-
190
- # test using namespaces on bare configs
191
- class SettableNamespaceTest < Test::Unit::TestCase
192
- def setup
193
- @config = Configuration.new do
194
- namespace :email do
195
- set :from, 'no-reply@example.com'
196
- set :subject do
197
- "Mail from #{from}"
198
- end
199
- end
200
-
201
- set :from, 'russia with love'
202
- end
203
- end
204
-
205
- def test_namespace_from
206
- assert_equal 'no-reply@example.com', @config.email.from
207
- end
208
-
209
- def test_from
210
- assert_equal 'russia with love', @config.from
211
- end
212
-
213
- def test_namespace_blocks
214
- assert_equal 'Mail from no-reply@example.com', @config.email.subject
215
- end
216
- end
217
-
218
- # test using namespaces on rails configs
219
- class SettableRailsNamespaceTest < Test::Unit::TestCase
220
- def setup
221
- @config = RailsConfiguration.new do
222
- define_environments :custom
223
-
224
- namespace :api do
225
- set :token do
226
- in_production{ return 'PROD' }
227
- in_custom{ return 'CUSTOM' }
228
- 'DEFAULT'
229
- end
230
- end
231
- end
232
- end
233
-
234
- def test_namespace_environments
235
- assert @config.api.respond_to?(:in_custom?)
236
- end
237
-
238
- def test_namespace_blocks_dev
239
- Rails.env = :development
240
- assert_equal 'DEFAULT', @config.api.token
241
- end
242
-
243
- def test_namespace_blocks_prod
244
- Rails.env = :production
245
- assert_equal 'PROD', @config.api.token
246
- end
247
-
248
- def test_namespace_blocks_custom
249
- Rails.env = :custom
250
- assert_equal 'CUSTOM', @config.api.token
251
- end
252
- end