gem_config 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YWMxNzQwNzU0OTczZTllMmY3YTYyZjIxMDE2ZWZkYWQ4YzA1MmYwOA==
5
- data.tar.gz: !binary |-
6
- NDU4YjE0M2Q4MDdkMTBlNTJhMTM3MTUwN2FkYmI2ZjNmNzkyMDQ5Zg==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- ODAxNjg0OGNhN2JkMjgxMDZmNjliNmFkNjY5ZTJiZjMzZDIzYjlhYjgzZmFk
10
- YjkxNTYwYTJjZmFhYTkzYzRiYWM0ZDRkMjc0Y2UxMmRhOWNkNzRjNmFmMDYx
11
- M2U1MjJhMWU2YzE2NTU5YjA2ZDIxNTcxMzQ1NTA5M2VhODdjM2U=
12
- data.tar.gz: !binary |-
13
- ZjQ4MjhhMjJjZWVhZThhNjhiMWJhZTg4MTk4ZmJkNDkxNmUyN2EyNGRjMGY5
14
- YjZlZjQ0ZTZjYTBjYTViMjAzZWI0NmI3NWQ2MzFmYjY0ZWRjZjc4MDk0NGQ2
15
- ODYxYmQ0N2U2YmE4YzE4Y2U0YmY2MmQ1ZWExYmRlMTkxMWUwNGE=
2
+ SHA1:
3
+ metadata.gz: 9c0001fb2a37e49f309f7c050560a2ca0b809cba
4
+ data.tar.gz: 23ec574b1a07a169eed85427076150eae6fdf62b
5
+ SHA512:
6
+ metadata.gz: 1dc0f00e99dbf3d5592024f904dd2a22c65a808b21e4c34357c0f86e0d254027f2612d5cf43547fa941872f70398bf81d2e7c3198effab1e721ea1fce4593670
7
+ data.tar.gz: 1e7485ca57cf804347732159e6563457a51600548c612b27efa1ef8d3b1da20bb3d1cec817810bad204a4fcfdac1fea0bb32ba186793c6053057dc433753ecc9
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task default: :spec
data/gem_config.gemspec CHANGED
@@ -18,4 +18,9 @@ Gem::Specification.new do |gem|
18
18
  gem.executables = gem.files.grep(%r(^bin/)).map { |f| File.basename(f) }
19
19
  gem.test_files = gem.files.grep(%r(^(test|spec|features)/))
20
20
  gem.require_paths = ['lib']
21
+
22
+ gem.add_development_dependency 'rake', '>= 0.9.0'
23
+ gem.add_development_dependency 'rspec', '~> 2.13.0'
24
+
25
+ gem.add_runtime_dependency 'activesupport', '~> 3.0'
21
26
  end
data/lib/gem_config.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module GemConfig
2
2
  end
3
3
 
4
- require 'gem_config/configuration_rules'
4
+ require 'gem_config/rules'
5
5
  require 'gem_config/configuration'
6
6
  require 'gem_config/base'
@@ -10,17 +10,11 @@ module GemConfig
10
10
  end
11
11
 
12
12
  def configuration
13
- @configuration ||= Configuration.new(configuration_rules)
13
+ @configuration ||= Configuration.new
14
14
  end
15
15
 
16
16
  def with_configuration(&block)
17
- configuration_rules.instance_eval(&block)
18
- end
19
-
20
- private
21
-
22
- def configuration_rules
23
- @configuration_rules ||= ConfigurationRules.new
17
+ configuration.rules.instance_eval(&block)
24
18
  end
25
19
  end
26
20
  end
@@ -1,30 +1,26 @@
1
1
  module GemConfig
2
2
  class Configuration
3
- def initialize(configuration_rules)
4
- @configuration_rules = configuration_rules
5
- end
6
-
7
3
  def rules
8
- @configuration_rules
4
+ @rules ||= Rules.new
9
5
  end
10
6
 
11
7
  def current
12
- @configuration_rules.keys.each_with_object({}) do |key, hash|
8
+ self.rules.keys.each_with_object({}) do |key, hash|
13
9
  hash[key] = get(key)
14
10
  end
15
11
  end
16
12
 
17
13
  def reset
18
- @configuration_rules.keys.each do |key|
14
+ self.rules.keys.each do |key|
19
15
  set key, nil
20
16
  end
21
17
  end
22
18
 
23
19
  def method_missing(method, *args, &block)
24
20
  case
25
- when @configuration_rules.keys.include?(method.to_sym)
21
+ when self.rules.keys.include?(method.to_sym)
26
22
  get method
27
- when (match = method.to_s.match(/\A(?<key>\w+)=\z/)) && @configuration_rules.keys.include?(match[:key].to_sym)
23
+ when (match = method.to_s.match(/\A(?<key>\w+)=\z/)) && self.rules.keys.include?(match[:key].to_sym)
28
24
  set match[:key], args.first
29
25
  else
30
26
  super method, *args, block
@@ -38,7 +34,7 @@ module GemConfig
38
34
  end
39
35
 
40
36
  def get(key)
41
- self.instance_variable_get("@#{key}") || @configuration_rules[key.to_sym][:default]
37
+ self.instance_variable_get("@#{key}") || self.rules[key.to_sym][:default]
42
38
  end
43
39
  end
44
40
  end
@@ -0,0 +1,46 @@
1
+ require 'active_support/core_ext/hash/keys'
2
+
3
+ module GemConfig
4
+ class Rules < Hash
5
+ def has(key, attrs = {})
6
+ check_attributes attrs
7
+ self[key.to_sym] = attrs
8
+ end
9
+
10
+ private
11
+
12
+ def check_attributes(attrs)
13
+ attrs.assert_valid_keys :classes, :values, :default
14
+
15
+ if attrs.has_key?(:classes)
16
+ other_than_class = Array(attrs[:classes]).any? do |value|
17
+ !value.is_a?(Class)
18
+ end
19
+ raise ArgumentError, 'Value of :classes parameter must be a single class or an array of classes.' if other_than_class
20
+ end
21
+
22
+ if attrs.has_key?(:classes) && attrs.has_key?(:values)
23
+ value_not_in_classes = Array(attrs[:values]).any? do |value|
24
+ Array(attrs[:classes]).none? do |klass|
25
+ value.is_a?(klass)
26
+ end
27
+ end
28
+ raise ArgumentError, 'Values of :values parameter must all have the type of one of the defined :classes.' if value_not_in_classes
29
+ end
30
+
31
+ if attrs.has_key?(:default)
32
+ if attrs.has_key?(:classes)
33
+ default_not_in_classes = Array(attrs[:classes]).none? do |klass|
34
+ attrs[:default].is_a?(klass)
35
+ end
36
+ raise ArgumentError, 'Value of :default parameter must have the type of one of the defined :classes.' if default_not_in_classes
37
+ end
38
+
39
+ if attrs.has_key?(:values)
40
+ default_not_in_values = !Array(attrs[:values]).include?(attrs[:default])
41
+ raise ArgumentError, 'Value of :default parameter must have the type of one of the defined :classes.' if default_not_in_values
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,3 +1,3 @@
1
1
  module GemConfig
2
- VERSION = '0.0.2'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'a module with configuration rules' do
4
+ subject do
5
+ Module.new do
6
+ include GemConfig::Base
7
+
8
+ with_configuration do
9
+ has :foo, classes: String
10
+ has :bar, classes: Numeric, default: 1
11
+ has :baz, values: %w(lorem ipsum dolor), default: 'lorem'
12
+ end
13
+ end
14
+ end
15
+
16
+ it 'can be configured' do
17
+ expect do
18
+ subject.configure do |config|
19
+ config.foo = 'bar'
20
+ config.baz = 'ipsum'
21
+ end
22
+ end.to_not raise_error
23
+
24
+ subject.configuration.foo.should eq('bar')
25
+ subject.configuration.baz.should eq('ipsum')
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe GemConfig::Base do
4
+ it 'provides a class method `with_configuration` when included' do
5
+ m = Module.new do
6
+ include GemConfig::Base
7
+ end
8
+
9
+ m.should respond_to(:with_configuration)
10
+ end
11
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe GemConfig::Configuration do
4
+ subject do
5
+ GemConfig::Configuration.new.tap do |configuration|
6
+ configuration.rules.has :foo
7
+ configuration.rules.has :count
8
+ configuration.foo = 'bar'
9
+ configuration.count = 123
10
+ configuration
11
+ end
12
+ end
13
+
14
+ describe '#rules' do
15
+ it 'returns a Rules object' do
16
+ subject.rules.is_a?(GemConfig::Rules)
17
+ end
18
+ end
19
+
20
+ describe '#current' do
21
+ it 'returns the current configuration' do
22
+ subject.current.should eq(foo: 'bar', count: 123)
23
+ end
24
+ end
25
+
26
+ describe '#reset' do
27
+ it 'resets the configuration' do
28
+ subject.tap(&:reset).current.should eq(foo: nil, count: nil)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ describe GemConfig::Rules do
4
+ subject { GemConfig::Rules.new }
5
+
6
+ describe '#has' do
7
+ it 'sets a rule' do
8
+ subject.has :foo
9
+ subject.should have_key(:foo)
10
+ end
11
+
12
+ it 'sets the parameters of a rule' do
13
+ params = { classes: String, values: %w(foo bar), default: 'foo' }
14
+ subject.has :foo, params
15
+ subject[:foo].should eq(params)
16
+ end
17
+
18
+ it 'only allows certain parameters' do
19
+ expect do
20
+ subject.has :foo, classes: String
21
+ end.to_not raise_error(ArgumentError)
22
+
23
+ expect do
24
+ subject.has :foo, values: 'bar'
25
+ end.to_not raise_error(ArgumentError)
26
+
27
+ expect do
28
+ subject.has :foo, default: 'bar'
29
+ end.to_not raise_error(ArgumentError)
30
+
31
+ expect do
32
+ subject.has :foo, foo: 'bar'
33
+ end.to raise_error(ArgumentError)
34
+ end
35
+
36
+ describe 'parameter :classes' do
37
+ it 'only allows a class or an array of classes as the value' do
38
+ expect do
39
+ subject.has :foo, classes: String
40
+ end.to_not raise_error(ArgumentError)
41
+
42
+ expect do
43
+ subject.has :foo, classes: [String, Numeric]
44
+ end.to_not raise_error(ArgumentError)
45
+
46
+ expect do
47
+ subject.has :foo, classes: 'foo'
48
+ end.to raise_error(ArgumentError)
49
+ end
50
+ end
51
+
52
+ describe 'parameter :values' do
53
+ context 'when :classes are defined' do
54
+ it 'only allows values of one of the specified classes' do
55
+ expect do
56
+ subject.has :foo, classes: String, values: 'foo'
57
+ end.to_not raise_error(ArgumentError)
58
+
59
+ expect do
60
+ subject.has :foo, classes: Numeric, values: 1
61
+ end.to_not raise_error(ArgumentError)
62
+
63
+ expect do
64
+ subject.has :foo, classes: [String, Numeric], values: ['foo', 1]
65
+ end.to_not raise_error(ArgumentError)
66
+
67
+ expect do
68
+ subject.has :foo, classes: String, values: ['foo', 1]
69
+ end.to raise_error(ArgumentError)
70
+ end
71
+ end
72
+ end
73
+
74
+ describe 'parameter :default' do
75
+ context 'when :classes are defined' do
76
+ it 'only allows a value of one of the specified classes' do
77
+ expect do
78
+ subject.has :foo, classes: String, default: 'foo'
79
+ end.to_not raise_error(ArgumentError)
80
+
81
+ expect do
82
+ subject.has :foo, classes: Numeric, default: 1
83
+ end.to_not raise_error(ArgumentError)
84
+
85
+ expect do
86
+ subject.has :foo, classes: [String, Numeric], default: 'foo'
87
+ end.to_not raise_error(ArgumentError)
88
+
89
+ expect do
90
+ subject.has :foo, classes: Numeric, default: 'foo'
91
+ end.to raise_error(ArgumentError)
92
+ end
93
+ end
94
+
95
+ context 'when :values are defined' do
96
+ it 'only allows one of the specified values' do
97
+ expect do
98
+ subject.has :foo, values: 'foo', default: 'foo'
99
+ end.to_not raise_error(ArgumentError)
100
+
101
+ expect do
102
+ subject.has :foo, values: ['foo', 1], default: 1
103
+ end.to_not raise_error(ArgumentError)
104
+
105
+ expect do
106
+ subject.has :foo, values: ['foo', 1], default: 'bar'
107
+ end.to raise_error(ArgumentError)
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,13 @@
1
+ require 'gem_config'
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+
8
+ # Run specs in random order to surface order dependencies. If you find an
9
+ # order dependency and want to debug it, you can fix the order by providing
10
+ # the seed, which is printed after each run.
11
+ # --seed 1234
12
+ config.order = 'random'
13
+ end
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gem_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Meurer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-01 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2013-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.13.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 2.13.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
13
55
  description: A nifty way to make your gem configurable.
14
56
  email: manuel.meurer@gmail.com
15
57
  executables: []
@@ -19,12 +61,18 @@ files:
19
61
  - Gemfile
20
62
  - LICENSE.txt
21
63
  - README.md
64
+ - Rakefile
22
65
  - gem_config.gemspec
23
66
  - lib/gem_config.rb
24
67
  - lib/gem_config/base.rb
25
68
  - lib/gem_config/configuration.rb
26
- - lib/gem_config/configuration_rules.rb
69
+ - lib/gem_config/rules.rb
27
70
  - lib/gem_config/version.rb
71
+ - spec/features/configuration_spec.rb
72
+ - spec/lib/base_spec.rb
73
+ - spec/lib/configuration_spec.rb
74
+ - spec/lib/rules_spec.rb
75
+ - spec/spec_helper.rb
28
76
  homepage: https://github.com/krautcomputing/gem_config
29
77
  licenses:
30
78
  - MIT
@@ -35,18 +83,23 @@ require_paths:
35
83
  - lib
36
84
  required_ruby_version: !ruby/object:Gem::Requirement
37
85
  requirements:
38
- - - ! '>='
86
+ - - '>='
39
87
  - !ruby/object:Gem::Version
40
88
  version: '0'
41
89
  required_rubygems_version: !ruby/object:Gem::Requirement
42
90
  requirements:
43
- - - ! '>='
91
+ - - '>='
44
92
  - !ruby/object:Gem::Version
45
93
  version: '0'
46
94
  requirements: []
47
95
  rubyforge_project:
48
- rubygems_version: 2.0.3
96
+ rubygems_version: 2.0.2
49
97
  signing_key:
50
98
  specification_version: 4
51
99
  summary: A nifty way to make your gem configurable.
52
- test_files: []
100
+ test_files:
101
+ - spec/features/configuration_spec.rb
102
+ - spec/lib/base_spec.rb
103
+ - spec/lib/configuration_spec.rb
104
+ - spec/lib/rules_spec.rb
105
+ - spec/spec_helper.rb
@@ -1,7 +0,0 @@
1
- module GemConfig
2
- class ConfigurationRules < Hash
3
- def has(key, attrs = {})
4
- self[key.to_sym] = attrs
5
- end
6
- end
7
- end