choices 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,2 +1,10 @@
1
1
  require 'bundler'
2
+ require 'rake/testtask'
3
+
2
4
  Bundler::GemHelper.install_tasks
5
+
6
+ Rake::TestTask.new do |t|
7
+ end
8
+
9
+ desc 'Run test suite'
10
+ task :default => :test
@@ -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, env))
9
+ mash = Hashie::Mash.new(load_settings_hash(filename))
9
10
 
10
- with_local_settings(filename, env, '.local') do |local|
11
+ with_local_settings(filename, '.local') do |local|
11
12
  mash.update local
12
13
  end
13
-
14
- return mash
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, env)
19
+
20
+ def load_settings_hash(filename)
18
21
  yaml_content = ERB.new(IO.read(filename)).result
19
- yaml_load(yaml_content)[env]
22
+ yaml_load(yaml_content)
20
23
  end
21
24
 
22
- def with_local_settings(filename, env, suffix)
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, env)
28
+ hash = load_settings_hash(local_filename)
26
29
  yield hash if hash
27
30
  end
28
31
  end
@@ -39,8 +39,8 @@ module Choices::Rails
39
39
  end
40
40
  end
41
41
 
42
- if defined? Rails::Application::Configuration
43
- Rails::Application::Configuration.send(:include, Choices::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,5 @@
1
+ development:
2
+ name: 'Development'
3
+
4
+ production:
5
+ name: 'Production local'
@@ -0,0 +1,6 @@
1
+ defaults: &defaults
2
+ name: 'Defaults'
3
+
4
+ production:
5
+ <<: *defaults
6
+
@@ -0,0 +1,13 @@
1
+ defaults: &defaults
2
+ name: 'Defaults'
3
+
4
+ development:
5
+ name: 'Development'
6
+
7
+ production:
8
+ <<: *defaults
9
+ name: 'Production'
10
+
11
+ test:
12
+ name: 'Test'
13
+
@@ -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.3.2
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-07-03 00:00:00.000000000 Z
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