choices 0.3.2 → 0.4.0
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/Rakefile +8 -0
- data/lib/choices.rb +12 -9
- data/lib/choices/rails.rb +2 -2
- data/test/settings/with_local.local.yml +5 -0
- data/test/settings/with_local.yml +6 -0
- data/test/settings/without_local.yml +13 -0
- data/test/test_load_settings.rb +32 -0
- metadata +24 -3
data/Rakefile
CHANGED
data/lib/choices.rb
CHANGED
@@ -1,28 +1,31 @@
|
|
1
1
|
require 'hashie/mash'
|
2
2
|
require 'erb'
|
3
|
+
require 'yaml'
|
3
4
|
|
4
5
|
module Choices
|
5
6
|
extend self
|
6
7
|
|
7
8
|
def load_settings(filename, env)
|
8
|
-
mash = Hashie::Mash.new(load_settings_hash(filename
|
9
|
+
mash = Hashie::Mash.new(load_settings_hash(filename))
|
9
10
|
|
10
|
-
with_local_settings(filename,
|
11
|
+
with_local_settings(filename, '.local') do |local|
|
11
12
|
mash.update local
|
12
13
|
end
|
13
|
-
|
14
|
-
|
14
|
+
|
15
|
+
mash.fetch(env) do
|
16
|
+
raise IndexError, %{Missing key for "#{env}" in `#{filename}'}
|
17
|
+
end
|
15
18
|
end
|
16
|
-
|
17
|
-
def load_settings_hash(filename
|
19
|
+
|
20
|
+
def load_settings_hash(filename)
|
18
21
|
yaml_content = ERB.new(IO.read(filename)).result
|
19
|
-
yaml_load(yaml_content)
|
22
|
+
yaml_load(yaml_content)
|
20
23
|
end
|
21
24
|
|
22
|
-
def with_local_settings(filename,
|
25
|
+
def with_local_settings(filename, suffix)
|
23
26
|
local_filename = filename.sub(/(\.\w+)?$/, "#{suffix}\\1")
|
24
27
|
if File.exists? local_filename
|
25
|
-
hash = load_settings_hash(local_filename
|
28
|
+
hash = load_settings_hash(local_filename)
|
26
29
|
yield hash if hash
|
27
30
|
end
|
28
31
|
end
|
data/lib/choices/rails.rb
CHANGED
@@ -39,8 +39,8 @@ module Choices::Rails
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
if defined? Rails::
|
43
|
-
Rails::
|
42
|
+
if defined? Rails::Engine::Configuration
|
43
|
+
Rails::Engine::Configuration.send(:include, Choices::Rails)
|
44
44
|
elsif defined? Rails::Configuration
|
45
45
|
Rails::Configuration.class_eval do
|
46
46
|
include Choices::Rails
|
@@ -0,0 +1,32 @@
|
|
1
|
+
gem 'minitest'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'choices'
|
4
|
+
require 'pathname'
|
5
|
+
|
6
|
+
describe Choices do
|
7
|
+
before do
|
8
|
+
path = Pathname.new(__FILE__)
|
9
|
+
@without_local = path + '../settings/without_local.yml'
|
10
|
+
@with_local = path + '../settings/with_local.yml'
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'when loading a settings file' do
|
14
|
+
it 'should return a mash for the specified environment' do
|
15
|
+
Choices.load_settings(@without_local, 'defaults').name.must_equal 'Defaults'
|
16
|
+
Choices.load_settings(@without_local, 'production').name.must_equal 'Production'
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should load the local settings' do
|
20
|
+
Choices.load_settings(@with_local, 'defaults').name.must_equal 'Defaults'
|
21
|
+
Choices.load_settings(@with_local, 'development').name.must_equal 'Development'
|
22
|
+
Choices.load_settings(@with_local, 'production').name.must_equal 'Production local'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should raise an exception if the environment does not exist' do
|
26
|
+
error = lambda {
|
27
|
+
Choices.load_settings(@with_local, 'nonexistent')
|
28
|
+
}.must_raise(IndexError)
|
29
|
+
error.message.must_equal %{Missing key for "nonexistent" in `#{@with_local}'}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
metadata
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
name: choices
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.4.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Mislav Marohnić
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-11-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
prerelease: false
|
@@ -27,6 +27,22 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 0.4.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
prerelease: false
|
32
|
+
type: :development
|
33
|
+
version_requirements: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ~>
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 5.0.6
|
39
|
+
name: minitest
|
40
|
+
requirement: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 5.0.6
|
30
46
|
description:
|
31
47
|
email: mislav.marohnic@gmail.com
|
32
48
|
executables: []
|
@@ -36,6 +52,10 @@ files:
|
|
36
52
|
- Rakefile
|
37
53
|
- lib/choices/rails.rb
|
38
54
|
- lib/choices.rb
|
55
|
+
- test/settings/with_local.local.yml
|
56
|
+
- test/settings/with_local.yml
|
57
|
+
- test/settings/without_local.yml
|
58
|
+
- test/test_load_settings.rb
|
39
59
|
- README.md
|
40
60
|
- MIT-LICENSE
|
41
61
|
homepage: https://github.com/mislav/choices
|
@@ -63,4 +83,5 @@ rubygems_version: 1.8.23
|
|
63
83
|
signing_key:
|
64
84
|
specification_version: 3
|
65
85
|
summary: Easy settings for your app
|
66
|
-
test_files:
|
86
|
+
test_files:
|
87
|
+
- test/test_load_settings.rb
|