has_configuration 0.2.4 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/has_configuration.rb +15 -18
- data/lib/has_configuration/configuration.rb +7 -6
- data/lib/version.rb +3 -3
- data/spec/lib/has_configuration/configuration_spec.rb +26 -29
- data/spec/lib/has_configuration_spec.rb +4 -8
- data/spec/spec_helper.rb +3 -5
- data/spec/support/rails_mock.rb +7 -2
- metadata +20 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 40f44c4cea757f02eefd7f88c4ebe57191f7fd66
|
4
|
+
data.tar.gz: 8f43c0b7f09647f1b64a81bb087bb137340973ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 702a7f3bb8b1c8f8f369a66a07d1f2ae9979282fd8aa501a8885dfa58a377dd8169bc726e96151a953feb69a37b91d196edcd68adfa7ff09bc23122c9ad192b6
|
7
|
+
data.tar.gz: 7e565b1b17efaa841c5fa15fa9f2377807df8b1b67a1c6ca1dadf94333fcbf73684234d56af5b3fe34fff860dd431c5005b72c9c9e68996ea69a964b1682cc5d
|
data/lib/has_configuration.rb
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
require 'has_configuration/configuration'
|
2
2
|
|
3
3
|
module HasConfiguration #:nodoc:
|
4
|
-
|
5
4
|
def self.included(base)
|
6
5
|
base.extend(ClassMethods)
|
7
6
|
end
|
8
7
|
|
9
|
-
module ClassMethods
|
10
|
-
|
8
|
+
module ClassMethods #:nodoc:
|
11
9
|
# Load configuration settings from a yaml file and adds a class and an instance
|
12
10
|
# method +configuration+ to the object.
|
13
11
|
#
|
@@ -22,9 +20,10 @@ module HasConfiguration #:nodoc:
|
|
22
20
|
# ==== Integration Examples
|
23
21
|
#
|
24
22
|
# has_configuration
|
25
|
-
# # => loads setting without environment processing from the
|
23
|
+
# # => loads setting without environment processing from the
|
24
|
+
# # file #{self.class.name.downcase}.yml
|
26
25
|
#
|
27
|
-
# has_configuration :
|
26
|
+
# has_configuration file: Rails.root.join('config', 'example.yml'), env: 'staging'
|
28
27
|
# # => loads settings for staging environment from RAILS_ROOT/config/example.yml file
|
29
28
|
#
|
30
29
|
# ==== YAML File Example
|
@@ -50,21 +49,24 @@ module HasConfiguration #:nodoc:
|
|
50
49
|
# If the example above was loaded into a class +Foo+ in +production+ environment:
|
51
50
|
#
|
52
51
|
# Foo.configuration # => <HasConfiguration::Configuration:0x00...>
|
53
|
-
#
|
52
|
+
# Foo.new.configuration # => <HasConfiguration::Configuration:0x00...>
|
54
53
|
#
|
55
54
|
# # convenient getter methods
|
56
55
|
# Foo.configuration.some.nested # => "value"
|
57
56
|
#
|
58
57
|
# # to_h returns a HashWithIndifferentAccess
|
59
|
-
# Foo.configuration.to_h # => { :user => "root",
|
58
|
+
# Foo.configuration.to_h # => { :user => "root",
|
59
|
+
# # :password => "prod-secret",
|
60
60
|
# # :some => { :nested => "value" } }
|
61
61
|
# Foo.configuration.to_h[:some][:nested] # => "value"
|
62
62
|
# Foo.configuration.to_h[:some]['nested'] # => "value"
|
63
63
|
#
|
64
64
|
# # force a special key type (when merging with other hashes)
|
65
|
-
# Foo.configuration.to_h(:symbolized) # => { :user => "root",
|
65
|
+
# Foo.configuration.to_h(:symbolized) # => { :user => "root",
|
66
|
+
# # :password => "prod-secret",
|
66
67
|
# # :some => { :nested => "value" } }
|
67
|
-
# Foo.configuration.to_h(:stringify) # => { 'user' => "root",
|
68
|
+
# Foo.configuration.to_h(:stringify) # => { 'user' => "root",
|
69
|
+
# # 'password' => "prod-secret",
|
68
70
|
# # 'some' => { 'nested' => "value" } }
|
69
71
|
#
|
70
72
|
def has_configuration(options = {})
|
@@ -72,26 +74,21 @@ module HasConfiguration #:nodoc:
|
|
72
74
|
include Getter
|
73
75
|
end
|
74
76
|
|
75
|
-
|
76
|
-
|
77
|
+
# Adds getters for the configuration
|
78
|
+
module Getter
|
77
79
|
def self.included(base)
|
78
80
|
base.extend(ClassMethods)
|
79
81
|
end
|
80
82
|
|
81
|
-
module ClassMethods
|
82
|
-
|
83
|
-
@configuration
|
84
|
-
end
|
83
|
+
module ClassMethods #:nodoc:
|
84
|
+
attr_reader :configuration
|
85
85
|
end
|
86
86
|
|
87
87
|
def configuration
|
88
88
|
self.class.configuration
|
89
89
|
end
|
90
|
-
|
91
90
|
end
|
92
|
-
|
93
91
|
end
|
94
|
-
|
95
92
|
end
|
96
93
|
|
97
94
|
class Object #:nodoc:
|
@@ -2,9 +2,8 @@ require 'active_support/core_ext/hash/indifferent_access'
|
|
2
2
|
require 'ostruct'
|
3
3
|
require 'yaml'
|
4
4
|
|
5
|
-
module HasConfiguration #:nodoc:
|
6
|
-
class Configuration
|
7
|
-
|
5
|
+
module HasConfiguration #:nodoc:
|
6
|
+
class Configuration #:nodoc:
|
8
7
|
def initialize(klass, options = {})
|
9
8
|
@class_name = klass.name
|
10
9
|
@options = options
|
@@ -51,7 +50,8 @@ module HasConfiguration #:nodoc:all
|
|
51
50
|
filename = "#{@class_name.downcase}.yml"
|
52
51
|
defined?(Rails) ? Rails.root.join('config', filename).to_s : filename
|
53
52
|
else
|
54
|
-
|
53
|
+
fail ArgumentError,
|
54
|
+
'Unable to resolve filename, please add :file parameter to has_configuration'
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
@@ -71,7 +71,9 @@ module HasConfiguration #:nodoc:all
|
|
71
71
|
end
|
72
72
|
|
73
73
|
def deep_symbolized_hash
|
74
|
-
@deep_symbolized_hash ||= deep_transform_keys(@hash)
|
74
|
+
@deep_symbolized_hash ||= deep_transform_keys(@hash) do |key|
|
75
|
+
key.respond_to?(:to_sym) ? key.to_sym : key
|
76
|
+
end
|
75
77
|
end
|
76
78
|
|
77
79
|
def deep_stringified_hash
|
@@ -86,6 +88,5 @@ module HasConfiguration #:nodoc:all
|
|
86
88
|
end if hash
|
87
89
|
result
|
88
90
|
end
|
89
|
-
|
90
91
|
end
|
91
92
|
end
|
data/lib/version.rb
CHANGED
@@ -11,35 +11,35 @@ RSpec.describe HasConfiguration::Configuration do
|
|
11
11
|
).to receive(:raw_file).and_return(File.read(fixture))
|
12
12
|
end
|
13
13
|
|
14
|
-
describe
|
14
|
+
describe '.new' do
|
15
15
|
let(:fixture) { 'spec/fixtures/class.yml' }
|
16
16
|
|
17
|
-
context
|
18
|
-
let(:file) {
|
17
|
+
context 'when no filename provided' do
|
18
|
+
let(:file) { '/RAILS_ROOT/config/class.yml' }
|
19
19
|
|
20
|
-
it
|
20
|
+
it 'loads default file' do
|
21
21
|
expect_any_instance_of(Configuration).to receive(:raw_file).with(file)
|
22
22
|
HasConfiguration::Configuration.new(klass)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
-
context
|
27
|
-
let(:file) {
|
26
|
+
context 'when filename provided' do
|
27
|
+
let(:file) { 'foo/bar.yml' }
|
28
28
|
|
29
|
-
it
|
29
|
+
it 'loads provided file' do
|
30
30
|
expect_any_instance_of(Configuration).to receive(:raw_file).with(file)
|
31
|
-
HasConfiguration::Configuration.new(klass, :
|
31
|
+
HasConfiguration::Configuration.new(klass, file: file)
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
context
|
36
|
+
context 'when initialized' do
|
37
37
|
let(:environment) { nil }
|
38
38
|
|
39
|
-
context
|
39
|
+
context 'environment' do
|
40
40
|
let(:fixture) { 'spec/fixtures/class.yml' }
|
41
41
|
|
42
|
-
context
|
42
|
+
context 'without env option' do
|
43
43
|
subject(:hash) { HasConfiguration::Configuration.new(klass).to_h }
|
44
44
|
|
45
45
|
it 'return the expected hash' do
|
@@ -47,19 +47,18 @@ RSpec.describe HasConfiguration::Configuration do
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
context
|
50
|
+
context 'with env option' do
|
51
51
|
let(:environment) { 'production' }
|
52
52
|
|
53
|
-
subject(:hash) { HasConfiguration::Configuration.new(klass, :
|
53
|
+
subject(:hash) { HasConfiguration::Configuration.new(klass, env: environment).to_h }
|
54
54
|
|
55
55
|
it 'return the expected hash' do
|
56
56
|
expect(hash).to eq('env' => environment)
|
57
57
|
end
|
58
58
|
end
|
59
|
-
|
60
59
|
end
|
61
60
|
|
62
|
-
context
|
61
|
+
context 'yaml defaults' do
|
63
62
|
let(:fixture) { 'spec/fixtures/with_defaults.yml' }
|
64
63
|
|
65
64
|
subject(:hash) { HasConfiguration::Configuration.new(klass).to_h }
|
@@ -69,7 +68,7 @@ RSpec.describe HasConfiguration::Configuration do
|
|
69
68
|
end
|
70
69
|
end
|
71
70
|
|
72
|
-
context
|
71
|
+
context 'with erb' do
|
73
72
|
let(:fixture) { 'spec/fixtures/with_erb.yml' }
|
74
73
|
|
75
74
|
subject(:hash) { HasConfiguration::Configuration.new(klass).to_h }
|
@@ -78,39 +77,37 @@ RSpec.describe HasConfiguration::Configuration do
|
|
78
77
|
expect(hash).to eq('erb' => Rails.env)
|
79
78
|
end
|
80
79
|
end
|
81
|
-
|
82
80
|
end
|
83
81
|
|
84
|
-
describe
|
82
|
+
describe '#to_h' do
|
85
83
|
let(:fixture) { 'spec/fixtures/with_nested_attributes.yml' }
|
86
84
|
|
87
|
-
context
|
85
|
+
context '#to_h' do
|
88
86
|
subject { HasConfiguration::Configuration.new(klass).to_h }
|
89
87
|
it { should be_a(HashWithIndifferentAccess) }
|
90
88
|
end
|
91
89
|
|
92
|
-
context
|
90
|
+
context '#to_h(:stringify)' do
|
93
91
|
subject { HasConfiguration::Configuration.new(klass).to_h(:stringify) }
|
94
92
|
it { should eq('env' => 'test', 'nested' => { 'foo' => 'bar', 'baz' => true }) }
|
95
93
|
end
|
96
94
|
|
97
|
-
context
|
95
|
+
context '#to_h(:symbolized)' do
|
98
96
|
subject { HasConfiguration::Configuration.new(klass).to_h(:symbolized) }
|
99
|
-
it { should eq(:
|
97
|
+
it { should eq(env: 'test', nested: { foo: 'bar', baz: true }) }
|
100
98
|
end
|
101
|
-
|
102
99
|
end
|
103
100
|
|
104
|
-
describe
|
101
|
+
describe 'struct methods' do
|
105
102
|
let(:configuration) { HasConfiguration::Configuration.new(klass) }
|
106
103
|
let(:fixture) { 'spec/fixtures/with_nested_attributes.yml' }
|
107
104
|
|
108
|
-
it
|
109
|
-
expect(configuration.to_h[:env]
|
110
|
-
expect(configuration.env
|
105
|
+
it 'is structified' do
|
106
|
+
expect(configuration.to_h[:env]).to eql('test')
|
107
|
+
expect(configuration.env).to eql('test')
|
111
108
|
|
112
|
-
expect(configuration.to_h[:nested]['foo']
|
113
|
-
expect(configuration.nested.foo
|
109
|
+
expect(configuration.to_h[:nested]['foo']).to eql('bar')
|
110
|
+
expect(configuration.nested.foo).to eql('bar')
|
114
111
|
end
|
115
112
|
end
|
116
113
|
end
|
@@ -1,15 +1,13 @@
|
|
1
1
|
RSpec.describe HasConfiguration do
|
2
|
-
|
3
|
-
context "when declared" do
|
4
|
-
|
2
|
+
context 'when declared' do
|
5
3
|
before(:all) do
|
6
4
|
Dummy = Class.new do
|
7
5
|
require 'has_configuration'
|
8
|
-
has_configuration :
|
6
|
+
has_configuration file: 'spec/fixtures/class.yml'
|
9
7
|
end
|
10
8
|
end
|
11
9
|
|
12
|
-
context
|
10
|
+
context 'the class' do
|
13
11
|
subject(:dummy) { Dummy }
|
14
12
|
|
15
13
|
it { should respond_to(:configuration) }
|
@@ -19,7 +17,7 @@ RSpec.describe HasConfiguration do
|
|
19
17
|
end
|
20
18
|
end
|
21
19
|
|
22
|
-
context
|
20
|
+
context 'an instance' do
|
23
21
|
subject(:dummy) { Dummy.new }
|
24
22
|
|
25
23
|
it { should respond_to(:configuration) }
|
@@ -28,7 +26,5 @@ RSpec.describe HasConfiguration do
|
|
28
26
|
expect(dummy.configuration).to be
|
29
27
|
end
|
30
28
|
end
|
31
|
-
|
32
29
|
end
|
33
|
-
|
34
30
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
Coveralls.wear!
|
4
|
-
end
|
1
|
+
require 'coveralls'
|
2
|
+
Coveralls.wear!
|
5
3
|
|
6
4
|
# This file was generated by the `rspec --init` command. Conventionally, all
|
7
5
|
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
@@ -56,7 +54,7 @@ RSpec.configure do |config|
|
|
56
54
|
# For more details, see:
|
57
55
|
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
58
56
|
expectations.syntax = :expect
|
59
|
-
|
57
|
+
end
|
60
58
|
|
61
59
|
# These two settings work together to allow you to limit a spec run
|
62
60
|
# to individual examples or groups you care about by tagging them with
|
data/spec/support/rails_mock.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_configuration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Spickermann
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 2.
|
19
|
+
version: 3.2.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 2.
|
26
|
+
version: 3.2.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,8 +66,23 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
|
70
|
-
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: |2
|
84
|
+
Loads configuration setting from a yml file and adds a configuation method
|
85
|
+
to class and instances
|
71
86
|
email:
|
72
87
|
- spickermann@gmail.com
|
73
88
|
executables: []
|