nested_config 0.5.0 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a357315a32e5b011e722c36ca2e97bf0f45d86d0
4
- data.tar.gz: f60e835020536810df8a3eda0c5de853c1a54da6
3
+ metadata.gz: 7b2d8e043d622bfb78abda999e982036170a52e7
4
+ data.tar.gz: 5163d83682df800346302dbd35d8ba7e797a551d
5
5
  SHA512:
6
- metadata.gz: a68a27481299c850f378c21e6e3ad0315d35307bf26a1134ff7a9ceace977f86ef6efbf283d5dd2255a121642ab6060bd76f01851132693f9a04ff6617650689
7
- data.tar.gz: 95f6909ff632d697c5cc0deb443980e970a456e4bc808564182860a752df8c803472d7518fc0cd684d523926e54c8dd76f8d8db1861053de4ac4780bfe71d86d
6
+ metadata.gz: 13a62a6670d52e45fb14a9bc13b9bda31212101925f8f12c7e29e6a09f3437c7176059cce22f8ceae172ac7d52c1cf3e2056e1965fb05d15aec806bc8aaedc4c
7
+ data.tar.gz: ab6e5d35e35e697f39345367af8199b4003c8ca10c67ce4435781867b96d4eff73ff380d1b544fa3550b142e4ae82d211bc3a6f9602ba4244b09b143863a0e3e
data/.simplecov CHANGED
@@ -1,7 +1,9 @@
1
- require 'simplecov-rcov'
1
+ if ENV['COVERAGE']
2
+ require 'simplecov-rcov'
2
3
 
3
- SimpleCov.coverage_dir "tmp/coverage"
4
- SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter if ENV['CI']
5
- SimpleCov.start do
6
- add_filter '/test/'
4
+ SimpleCov.coverage_dir "tmp/coverage"
5
+ SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter if ENV['CI']
6
+ SimpleCov.start do
7
+ add_filter '/test/'
8
+ end
7
9
  end
data/.travis.yml CHANGED
@@ -1,7 +1,29 @@
1
1
  language: ruby
2
+ sudo: false
3
+ cache: bundler
2
4
  rvm:
5
+ - ruby-head
6
+ - 2.2
3
7
  - 2.1
4
8
  - 2.0
5
9
  - 1.9.3
6
- - jruby-19mode
7
- - rbx-19mode
10
+ - jruby
11
+ - jruby-head
12
+ - rbx-2
13
+ env:
14
+ global:
15
+ - CODECLIMATE_REPO_TOKEN=a682282424953217391072dbe4c6b0f793a6c40312a182bc1eb3e26337dbf0f1
16
+ - JRUBY_OPTS='--dev -J-Xmx1024M'
17
+ matrix:
18
+ fast_finish: true
19
+ allow_failures:
20
+ - rvm: ruby-head
21
+ - rvm: jruby
22
+ - rvm: jruby-head
23
+ notifications:
24
+ webhooks:
25
+ urls:
26
+ - https://webhooks.gitter.im/e/ed57a44a47758f95c362
27
+ on_success: change # options: [always|never|change] default: always
28
+ on_failure: always # options: [always|never|change] default: always
29
+ on_start: false # default: false
data/Gemfile CHANGED
@@ -8,3 +8,7 @@ if ENV['COVERAGE']
8
8
  gem 'simplecov-rcov', :require => false
9
9
  end
10
10
  end
11
+
12
+ if ENV['CODECLIMATE_REPO_TOKEN']
13
+ gem "codeclimate-test-reporter", :group => :test, :require => nil
14
+ end
data/README.md ADDED
@@ -0,0 +1,168 @@
1
+ [github]: https://github.com/neopoly/nested_config
2
+ [doc]: http://rubydoc.info/github/neopoly/nested_config/master/file/README.md
3
+ [gem]: https://rubygems.org/gems/nested_config
4
+ [travis]: https://travis-ci.org/neopoly/nested_config
5
+ [codeclimate]: https://codeclimate.com/github/neopoly/nested_config
6
+ [inchpages]: https://inch-ci.org/github/neopoly/nested_config
7
+
8
+ # nested_config
9
+
10
+ [![Travis](https://img.shields.io/travis/neopoly/nested_config.svg?branch=master)][travis]
11
+ [![Gem Version](https://img.shields.io/gem/v/nested_config.svg)][gem]
12
+ [![Code Climate](https://img.shields.io/codeclimate/github/neopoly/nested_config.svg)][codeclimate]
13
+ [![Test Coverage](https://codeclimate.com/github/neopoly/nested_config/badges/coverage.svg)][codeclimate]
14
+ [![Inline docs](https://inch-ci.org/github/neopoly/nested_config.svg?branch=master&style=flat)][inchpages]
15
+
16
+ [Gem][gem] |
17
+ [Source][github] |
18
+ [Documentation][doc]
19
+
20
+ Simple, static, nested application configuration
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require 'nested_config'
26
+
27
+ class MyApp
28
+ def self.configure
29
+ yield config
30
+ end
31
+
32
+ def self.config
33
+ @config ||= MyConfig.new
34
+ end
35
+
36
+ class MyConfig < NestedConfig
37
+ end
38
+ end
39
+ ```
40
+
41
+ ### Basic
42
+
43
+ ```ruby
44
+ MyApp.configure do |config|
45
+ config.coins = 1000
46
+ config.user do |user|
47
+ user.max = 5
48
+ end
49
+ config.queue do |queue|
50
+ queue.workers do |workers|
51
+ workers.max = 2
52
+ workers.timeout = 60.seconds
53
+ end
54
+ end
55
+ end
56
+
57
+ MyApp.config.coins # => 1000
58
+ MyApp.config.queue.workers.timeout # => 60
59
+ ```
60
+
61
+ ### Special config keys
62
+
63
+ Sometimes you need to define config keys which contains spaces, dashes or other special chars.
64
+ In those case you can use `NestedConfig#_`:
65
+
66
+ ```ruby
67
+ config = NestedConfig.new.tap do |config|
68
+ config._ "mix & match" do |mix_and_match|
69
+ mix_and_match.name = "Mix & match"
70
+ # ...
71
+ end
72
+ end
73
+
74
+ config["mix & match"].name # => Mix & match
75
+ ```
76
+
77
+ ### EvaluateOnce
78
+
79
+ With the module `EvaluateOnce` you can define config value which will be
80
+ evaluated lazily. Once.
81
+
82
+ ```ruby
83
+ class MyConfig
84
+ include NestedConfig::EvaluateOnce
85
+ end
86
+
87
+ MyApp.configure do |config|
88
+ config.country_list = proc { Country.all }
89
+ end
90
+
91
+ MyApp.config.country_list # => [List of countries]
92
+ MyApp.config.country_list # => [List of countries] (cached)
93
+ ```
94
+
95
+ The initial access to key `country_list` calls (via `call` method) the proc
96
+ and replaces the value. Subsequent calls just fetch the replaced value.
97
+
98
+ ### Modifying config using NestedConfig::WithConfig
99
+
100
+ Values of NestedConfig can be temporalily modified.
101
+
102
+ This can be used in tests modifying a global application config inside a
103
+ block:
104
+
105
+ ```ruby
106
+ require 'nested_config/with_config'
107
+
108
+ class MyCase < MiniTest::TestCase
109
+ include NestedConfig::WithConfig
110
+
111
+ def app_config
112
+ MyApp.config # global
113
+ end
114
+ end
115
+
116
+ class SomeCase < MyCase
117
+ def setup
118
+ app_config.tap do |config|
119
+ config.coins = 1000
120
+ config.queue do |queue|
121
+ queue.workers do |workers|
122
+ workers.max = 5
123
+ end
124
+ end
125
+ end
126
+ end
127
+
128
+ def test_with_basic_value
129
+ with_config(app_config) do |config|
130
+ config.coins = 500
131
+ end
132
+ # global config reset to previous config
133
+ end
134
+
135
+ def test_queue_with_changed_workers
136
+ with_config(app_config, :queue, :workers) do |workers|
137
+ workers.max = 1
138
+ # do stuff with modified config max value
139
+ end
140
+ # global config reset to previous config
141
+ end
142
+ end
143
+ ```
144
+
145
+ ### Key names
146
+
147
+ **Note**: NestedConfig is not a blank slate so you CANNOT use defined method
148
+ names as keys like `object_id`.
149
+
150
+ ## Installation
151
+
152
+ gem install nested_config
153
+
154
+ ## Test
155
+
156
+ rake test
157
+ COVERAGE=1 bundle exec rake test
158
+
159
+ ## TODO
160
+
161
+ * Make NestedConfig a blank slate
162
+
163
+
164
+ ## Release
165
+
166
+ edit lib/nested_config/version.rb
167
+ rake release
168
+
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ require 'rake/testtask'
8
8
  Rake::TestTask.new(:test) do |test|
9
9
  test.test_files = FileList.new('test/**/*_test.rb')
10
10
  test.libs << 'test'
11
- test.verbose = true
11
+ test.warning = true
12
12
  end
13
13
 
14
14
  # RDoc
data/lib/nested_config.rb CHANGED
@@ -55,10 +55,26 @@ class NestedConfig
55
55
  @hash = hash
56
56
  end
57
57
 
58
+ # Define a config with special names
59
+ #
60
+ # Example:
61
+ # config = NestedConfig.new.tap do |config|
62
+ # config._ "mix & match" do |mix_and_match|
63
+ # mix_and_match.name = "Mix & match"
64
+ # # ...
65
+ # end
66
+ # end
67
+ #
68
+ # config["mix & match"].name # => Mix & match
69
+ def _(name)
70
+ raise ArgumentError, "provide missing block" unless block_given?
71
+ config = self[name] ||= self.class.new
72
+ yield config
73
+ end
74
+
58
75
  def method_missing(name, *args)
59
76
  if block_given?
60
- config = self[name] ||= self.class.new
61
- yield config
77
+ _(name, &Proc.new)
62
78
  else
63
79
  key = name.to_s.gsub(/=$/, '')
64
80
  if $& == '='
@@ -1,3 +1,3 @@
1
1
  class NestedConfig
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -42,8 +42,8 @@ class NestedConfig
42
42
  # end
43
43
  module WithConfig
44
44
  def with_config(config, *keys, &block)
45
- current = keys.inject(config) do |config, key|
46
- config[key] or raise KeyNotFound.new(key, keys)
45
+ current = keys.inject(config) do |acc, key|
46
+ acc[key] or raise KeyNotFound.new(key, keys)
47
47
  end
48
48
  current.respond_to?(:__hash__) or raise ValueNotCloneable.new(current)
49
49
 
@@ -17,6 +17,6 @@ Gem::Specification.new do |s|
17
17
  s.require_paths = ["lib"]
18
18
 
19
19
  s.add_development_dependency "rake"
20
- s.add_development_dependency "minitest"
21
- s.add_development_dependency "rdoc"
20
+ s.add_development_dependency "minitest", "~> 5.8.4"
21
+ s.add_development_dependency "rdoc", "~> 4.2.2"
22
22
  end
data/test/helper.rb CHANGED
@@ -1,5 +1,7 @@
1
- require 'rubygems'
2
- require 'bundler/setup'
1
+ if ENV['CODECLIMATE_REPO_TOKEN']
2
+ require "codeclimate-test-reporter"
3
+ CodeClimate::TestReporter.start
4
+ end
3
5
 
4
6
  require 'simplecov' if ENV['COVERAGE']
5
7
 
@@ -21,25 +21,25 @@ class NestedConfigTest < NestedConfigSpec
21
21
  end
22
22
 
23
23
  test "inspect" do
24
- c = config.tap { |c| c.foo = :bar }
24
+ c = config.tap { |config| config.foo = :bar }
25
25
 
26
26
  hash = c.__hash__.inspect
27
27
  assert_match %r{#<NestedConfig:0x[0-9a-z]+ @hash=#{hash}>}, c.inspect
28
28
  end
29
29
 
30
30
  test "cannot use defined method names as keys" do
31
- c = config.tap do |c|
32
- c.object_id = :foo
33
- c.class = :bar
31
+ c = config.tap do |config|
32
+ config.object_id = :foo
33
+ config.class = :bar
34
34
  end
35
35
  refute_equal :foo, c.object_id
36
36
  refute_equal :bar, c.class
37
37
  end
38
38
 
39
39
  test "sets values" do
40
- c = config.tap do |c|
41
- c.foo = :bar
42
- c.bar = :baz
40
+ c = config.tap do |config|
41
+ config.foo = :bar
42
+ config.bar = :baz
43
43
  end
44
44
 
45
45
  assert_equal :bar, c.foo
@@ -48,14 +48,14 @@ class NestedConfigTest < NestedConfigSpec
48
48
  end
49
49
 
50
50
  test "sets deep values" do
51
- c = config.tap do |c|
52
- c.deep do |deep|
51
+ c = config.tap do |config|
52
+ config.deep do |deep|
53
53
  deep.key = :foo
54
54
  deep.deeper do |deeper|
55
55
  deeper.key = :bar
56
56
  end
57
57
  end
58
- c.deep2 do |deep2|
58
+ config.deep2 do |deep2|
59
59
  deep2.key = :baz
60
60
  end
61
61
  end
@@ -66,40 +66,60 @@ class NestedConfigTest < NestedConfigSpec
66
66
  end
67
67
 
68
68
  test "sets arrays as value" do
69
- c = config.tap do |c|
70
- c.ary = [ :foo, :bar ]
71
- c.ary2 = :foo, :bar
69
+ c = config.tap do |config|
70
+ config.ary = [ :foo, :bar ]
71
+ config.ary2 = :foo, :bar
72
72
  end
73
73
 
74
74
  assert_equal [ :foo, :bar ], c.ary
75
75
  assert_equal [ :foo, :bar ], c.ary2
76
76
  end
77
77
 
78
+ test "sets nested values via []" do
79
+ c = config.tap do |config|
80
+ config._ "special key" do |o|
81
+ o.key = "special"
82
+ end
83
+
84
+ config._ "special key" do |o|
85
+ o.key2 = "again"
86
+ end
87
+ end
88
+
89
+ assert_equal "special", c["special key"].key
90
+ assert_equal "again", c["special key"].key2
91
+
92
+ e = assert_raises ArgumentError do
93
+ c._ "special key"
94
+ end
95
+ assert_equal "provide missing block", e.message
96
+ end
97
+
78
98
  test "cannot nest nil" do
79
- c = config.tap do |c|
80
- c.key = :foo
99
+ c = config.tap do |config|
100
+ config.key = :foo
81
101
  end
82
102
 
83
103
  e = assert_raises NoMethodError do
84
104
  c.deep.deeper.key
85
105
  end
86
- assert_match /NilClass/, e.message
106
+ assert_match(/NilClass/, e.message)
87
107
  end
88
108
 
89
109
  test "respond_to_missing?" do
90
- c = config.tap do |c|
91
- c.foo = :bar
110
+ c = config.tap do |config|
111
+ config.foo = :bar
92
112
  end
93
113
 
94
114
  assert_respond_to c, :foo
95
115
  end
96
116
 
97
117
  test "re-use already defined nested config" do
98
- c = config.tap do |c|
99
- c.users do |users|
118
+ c = config.tap do |config|
119
+ config.users do |users|
100
120
  users.max = 23
101
121
  end
102
- c.users do |users|
122
+ config.users do |users|
103
123
  users.min = 5
104
124
  end
105
125
  end
@@ -122,8 +142,8 @@ class NestedConfigTest < NestedConfigSpec
122
142
  end
123
143
 
124
144
  test "nests subclassed config" do
125
- c = subclassed_config.tap do |c|
126
- c.users do |users|
145
+ c = subclassed_config.tap do |config|
146
+ config.users do |users|
127
147
  users.max = 23
128
148
  end
129
149
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nested_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Suschlik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-21 00:00:00.000000000 Z
11
+ date: 2016-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -28,30 +28,30 @@ dependencies:
28
28
  name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 5.8.4
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 5.8.4
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rdoc
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 4.2.2
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 4.2.2
55
55
  description: ''
56
56
  email:
57
57
  - ps@neopoly.de
@@ -60,11 +60,10 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
- - ".ruby-version"
64
63
  - ".simplecov"
65
64
  - ".travis.yml"
66
65
  - Gemfile
67
- - README.rdoc
66
+ - README.md
68
67
  - Rakefile
69
68
  - lib/nested_config.rb
70
69
  - lib/nested_config/evaluate_once.rb
@@ -94,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
93
  version: '0'
95
94
  requirements: []
96
95
  rubyforge_project:
97
- rubygems_version: 2.2.2
96
+ rubygems_version: 2.4.8
98
97
  signing_key:
99
98
  specification_version: 4
100
99
  summary: Simple, static, nested config
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.1
data/README.rdoc DELETED
@@ -1,131 +0,0 @@
1
- = nested_config
2
-
3
- {<img src="http://img.shields.io/travis/neopoly/nested_config.svg" alt="Build Status" />}[http://travis-ci.org/neopoly/nested_config] {<img src="http://img.shields.io/gem/v/nested_config.svg" alt="Gem Version" />}[http://rubygems.org/gems/nested_config] {<img src="http://img.shields.io/codeclimate/github/neopoly/nested_config.svg" />}[https://codeclimate.com/github/neopoly/nested_config] {<img src="http://inch-ci.org/github/neopoly/nested_config.svg?branch=master" alt="Inline docs" />}[http://inch-ci.org/github/neopoly/nested_config]
4
-
5
- Simple, static, nested application configuration
6
-
7
- Gem[https://rubygems.org/gems/nested_config] |
8
- Source[https://github.com/neopoly/nested_config] |
9
- Documentation[http://rubydoc.info/github/neopoly/nested_config/master/file/README.rdoc]
10
-
11
- == Usage
12
-
13
- require 'nested_config'
14
-
15
- class MyApp
16
- def self.configure
17
- yield config
18
- end
19
-
20
- def self.config
21
- @config ||= MyConfig.new
22
- end
23
-
24
- class MyConfig < NestedConfig
25
- end
26
- end
27
-
28
- === Basic
29
-
30
- MyApp.configure do |config|
31
- config.coins = 1000
32
- config.user do |user|
33
- user.max = 5
34
- end
35
- config.queue do |queue|
36
- queue.workers do |workers|
37
- workers.max = 2
38
- workers.timeout = 60.seconds
39
- end
40
- end
41
- end
42
-
43
- MyApp.config.coins # => 1000
44
- MyApp.config.queue.workers.timeout # => 60
45
-
46
-
47
- === EvaluateOnce
48
-
49
- With the module +EvaluateOnce+ you can define config value which will be evaluated lazily. Once.
50
-
51
- class MyConfig
52
- include NestedConfig::EvaluateOnce
53
- end
54
-
55
- MyApp.configure do |config|
56
- config.country_list = proc { Country.all }
57
- end
58
-
59
- MyApp.config.country_list # => [List of countries]
60
- MyApp.config.country_list # => [List of countries] (cached)
61
-
62
- The initial access to key +country_list+ calls (via +call+ method) the proc and replaces the value.
63
- Subsequent calls just fetch the replaced value.
64
-
65
-
66
- === Modifying config using NestedConfig::WithConfig
67
-
68
- Values of NestedConfig can be temporalily modified.
69
-
70
- This can be used in tests modifying a global application config inside a block:
71
-
72
- require 'nested_config/with_config'
73
-
74
- class MyCase < MiniTest::TestCase
75
- include NestedConfig::WithConfig
76
-
77
- def app_config
78
- MyApp.config # global
79
- end
80
- end
81
-
82
- class SomeCase < MyCase
83
- def setup
84
- app_config.tap do |config|
85
- config.coins = 1000
86
- config.queue do |queue|
87
- queue.workers do |workers|
88
- workers.max = 5
89
- end
90
- end
91
- end
92
- end
93
-
94
- def test_with_basic_value
95
- with_config(app_config) do |config|
96
- config.coins = 500
97
- end
98
- # global config reset to previous config
99
- end
100
-
101
- def test_queue_with_changed_workers
102
- with_config(app_config, :queue, :workers) do |workers|
103
- workers.max = 1
104
- # do stuff with modified config max value
105
- end
106
- # global config reset to previous config
107
- end
108
- end
109
-
110
-
111
- === Key names
112
-
113
- *Note*: NestedConfig is not a blank slate so you CANNOT use defined method names as keys like +object_id+.
114
-
115
- == Installation
116
-
117
- gem install nested_config
118
-
119
- == Test
120
-
121
- rake test
122
- COVERAGE=1 bundle exec rake test
123
-
124
- == TODO
125
-
126
- * Make NestedConfig a blank slate
127
-
128
- == Release
129
-
130
- edit lib/nested_config/version.rb
131
- rake release