settable 1.0 → 1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/examples/example.rb +11 -4
- data/examples/rails.rb +21 -7
- data/lib/settable.rb +25 -2
- data/test/settable_test.rb +252 -0
- metadata +23 -32
data/examples/example.rb
CHANGED
@@ -3,7 +3,7 @@ require 'settable'
|
|
3
3
|
|
4
4
|
class Configuration
|
5
5
|
include Settable
|
6
|
-
|
6
|
+
|
7
7
|
def initialize(&block)
|
8
8
|
instance_eval(&block) if block_given?
|
9
9
|
end
|
@@ -11,14 +11,19 @@ end
|
|
11
11
|
|
12
12
|
config = Configuration.new do
|
13
13
|
set :environment, 'development'
|
14
|
-
|
14
|
+
|
15
15
|
set :api_token do
|
16
16
|
return 'PRODTOKEN' if environment == 'production'
|
17
17
|
'DEVTOKEN'
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
set :api_endpoint, "http://example.com/api/#{api_token}"
|
21
21
|
set :environment, 'production'
|
22
|
+
|
23
|
+
namespace :chat do
|
24
|
+
set :enabled, true
|
25
|
+
set :token, 'abcde90210'
|
26
|
+
end
|
22
27
|
end
|
23
28
|
|
24
29
|
# external stuffs
|
@@ -33,4 +38,6 @@ puts config.environment
|
|
33
38
|
puts config.api_token
|
34
39
|
puts config.api_endpoint
|
35
40
|
puts config.something
|
36
|
-
puts config.debug?
|
41
|
+
puts config.debug?
|
42
|
+
puts config.chat.enabled?
|
43
|
+
puts config.chat.token
|
data/examples/rails.rb
CHANGED
@@ -9,7 +9,7 @@ end
|
|
9
9
|
class Configuration
|
10
10
|
include Settable
|
11
11
|
include Settable::Rails
|
12
|
-
|
12
|
+
|
13
13
|
def initialize(&block)
|
14
14
|
instance_eval(&block) if block_given?
|
15
15
|
end
|
@@ -17,25 +17,36 @@ end
|
|
17
17
|
|
18
18
|
@config = Configuration.new do
|
19
19
|
define_environments :blah, :qa
|
20
|
-
|
20
|
+
|
21
21
|
set :something, in_blah?
|
22
22
|
set :debug, in_environments?(:development, :test)
|
23
|
-
|
23
|
+
|
24
24
|
if in_production?
|
25
25
|
enable :tracking, :caching
|
26
26
|
else
|
27
27
|
disable :tracking, :caching
|
28
28
|
end
|
29
|
-
|
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
|
+
|
30
43
|
set :api_token do
|
31
44
|
in_production { return 'PRODTOKEN' }
|
32
45
|
in_development{ return 'DEVTOKEN' }
|
33
46
|
'OTHERTOKEN'
|
34
47
|
end
|
35
|
-
|
48
|
+
|
36
49
|
set :api_endpoint do
|
37
|
-
in_environments(:development, :test){ return "http://sandbox.example.com/api/#{api_token}" }
|
38
|
-
"http://example.com/api/#{api_token}"
|
39
50
|
end
|
40
51
|
end
|
41
52
|
|
@@ -44,6 +55,9 @@ puts @config.inspect
|
|
44
55
|
|
45
56
|
puts '-' * 80
|
46
57
|
|
58
|
+
puts @config.api.token
|
59
|
+
puts @config.api.endpoint
|
60
|
+
|
47
61
|
puts @config.tracking?
|
48
62
|
puts @config.caching?
|
49
63
|
puts @config.api_token
|
data/lib/settable.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
module Settable
|
2
|
-
VERSION = "1.
|
2
|
+
VERSION = "1.1"
|
3
3
|
|
4
4
|
module Rails
|
5
5
|
DEFAULT_ENVIRONMENTS = [:development, :production, :test]
|
6
|
+
CUSTOM_ENVIRONMENTS = []
|
6
7
|
|
7
8
|
# allow us to add custom environment helpers
|
8
9
|
def define_environments(*envs)
|
9
10
|
envs.each do |env|
|
11
|
+
CUSTOM_ENVIRONMENTS << env
|
10
12
|
define_metaclass_method(:"in_#{env}"){ |&block| in_environment(env.to_sym, &block) }
|
11
|
-
define_metaclass_method(:"in_#{env}?"){ in_environment?(env.to_sym) }
|
13
|
+
define_metaclass_method(:"in_#{env}?"){ in_environment?(env.to_sym) }
|
12
14
|
end
|
13
15
|
end
|
14
16
|
alias_method :define_environment, :define_environments
|
@@ -53,6 +55,10 @@ module Settable
|
|
53
55
|
set key, Proc.new{value}
|
54
56
|
end
|
55
57
|
end
|
58
|
+
|
59
|
+
def namespace(name, &block)
|
60
|
+
set name, Namespace.create(self, &block)
|
61
|
+
end
|
56
62
|
|
57
63
|
def enable(*keys)
|
58
64
|
keys.each{ |key| set key, true }
|
@@ -61,4 +67,21 @@ module Settable
|
|
61
67
|
def disable(*keys)
|
62
68
|
keys.each{ |key| set key, false }
|
63
69
|
end
|
70
|
+
|
71
|
+
class Namespace
|
72
|
+
def self.create(base, &block)
|
73
|
+
include Settable
|
74
|
+
klass = new
|
75
|
+
|
76
|
+
# good lord this is hack. but we need to re-define the custom environments in our
|
77
|
+
# namespaces
|
78
|
+
if base.class.ancestors.include?(Settable::Rails)
|
79
|
+
include Settable::Rails
|
80
|
+
klass.instance_eval{ define_environments *CUSTOM_ENVIRONMENTS }
|
81
|
+
end
|
82
|
+
|
83
|
+
klass.instance_eval(&block)
|
84
|
+
klass
|
85
|
+
end
|
86
|
+
end
|
64
87
|
end
|
@@ -0,0 +1,252 @@
|
|
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
|
metadata
CHANGED
@@ -1,29 +1,23 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: settable
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.1'
|
4
5
|
prerelease:
|
5
|
-
version: "1.0"
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Rob Hurring
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
date: 2011-05-09 00:00:00 -04:00
|
14
|
-
default_executable:
|
12
|
+
date: 2012-01-20 00:00:00.000000000Z
|
15
13
|
dependencies: []
|
16
|
-
|
17
14
|
description: Small include to make config files better
|
18
|
-
email:
|
15
|
+
email:
|
19
16
|
- robhurring@gmail.com
|
20
17
|
executables: []
|
21
|
-
|
22
18
|
extensions: []
|
23
|
-
|
24
19
|
extra_rdoc_files: []
|
25
|
-
|
26
|
-
files:
|
20
|
+
files:
|
27
21
|
- .gitignore
|
28
22
|
- Gemfile
|
29
23
|
- Rakefile
|
@@ -32,33 +26,30 @@ files:
|
|
32
26
|
- examples/rails.rb
|
33
27
|
- lib/settable.rb
|
34
28
|
- settable.gemspec
|
35
|
-
|
36
|
-
homepage:
|
29
|
+
- test/settable_test.rb
|
30
|
+
homepage: ''
|
37
31
|
licenses: []
|
38
|
-
|
39
32
|
post_install_message:
|
40
33
|
rdoc_options: []
|
41
|
-
|
42
|
-
require_paths:
|
34
|
+
require_paths:
|
43
35
|
- lib
|
44
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
37
|
none: false
|
46
|
-
requirements:
|
47
|
-
- -
|
48
|
-
- !ruby/object:Gem::Version
|
49
|
-
version:
|
50
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
43
|
none: false
|
52
|
-
requirements:
|
53
|
-
- -
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
version:
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
56
48
|
requirements: []
|
57
|
-
|
58
49
|
rubyforge_project: settable
|
59
|
-
rubygems_version: 1.
|
50
|
+
rubygems_version: 1.8.6
|
60
51
|
signing_key:
|
61
52
|
specification_version: 3
|
62
53
|
summary: Small include to make config files better
|
63
|
-
test_files:
|
64
|
-
|
54
|
+
test_files:
|
55
|
+
- test/settable_test.rb
|