configru 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Change Log
2
2
 
3
+ ## 3.1.0 (28 October 2012)
4
+
5
+ * Added required options
6
+
3
7
  ## 3.0.0 (22 September 2012)
4
8
 
5
9
  * Added option arrays
data/Gemfile CHANGED
@@ -3,7 +3,7 @@ source :rubygems
3
3
  group :development do
4
4
  gem "rake", "~> 0.9.0"
5
5
  gem "rspec", "~> 2.11.0"
6
- gem "simplecov", "~> 0.6.0"
6
+ gem "simplecov", "~> 0.7.0"
7
7
  end
8
8
 
9
9
  gemspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- configru (3.0.0)
4
+ configru (3.1.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -17,10 +17,10 @@ GEM
17
17
  rspec-expectations (2.11.3)
18
18
  diff-lcs (~> 1.1.3)
19
19
  rspec-mocks (2.11.2)
20
- simplecov (0.6.4)
20
+ simplecov (0.7.1)
21
21
  multi_json (~> 1.0)
22
- simplecov-html (~> 0.5.3)
23
- simplecov-html (0.5.3)
22
+ simplecov-html (~> 0.7.1)
23
+ simplecov-html (0.7.1)
24
24
 
25
25
  PLATFORMS
26
26
  ruby
@@ -29,4 +29,4 @@ DEPENDENCIES
29
29
  configru!
30
30
  rake (~> 0.9.0)
31
31
  rspec (~> 2.11.0)
32
- simplecov (~> 0.6.0)
32
+ simplecov (~> 0.7.0)
data/README.md CHANGED
@@ -15,7 +15,7 @@ require 'configru'
15
15
 
16
16
  Configru.load('config.yml') do
17
17
  option :username, String, 'example_user'
18
- option :token, Fixnum, 1234
18
+ option_required :token, Fixnum
19
19
  option_group :connection do
20
20
  option :server, String, 'example.com'
21
21
  option :port, Fixnum, 42
@@ -24,22 +24,27 @@ Configru.load('config.yml') do
24
24
  transform {|x| File.expand_path(x) }
25
25
  validate {|x| File.directory?(x) }
26
26
  end
27
+ option_array :channels, String, ['foo', 'bar']
27
28
  end
28
29
 
29
30
  example = Example.new(Configru.connection.server, Configru.connection.port)
30
31
  example.login(Configru.username, Configru.token)
31
- example.sync(Configru.path)
32
+ Configru.channels.each do |x|
33
+ example.sync(x, Configru.path)
34
+ end
32
35
  ```
33
36
 
34
37
  These defaults are equivalent to the following YAML:
35
38
 
36
39
  ```yaml
37
40
  username: example_user
38
- token: 1234
39
41
  connection:
40
42
  server: example.com
41
43
  port: 42
42
44
  path: ~
45
+ channels:
46
+ - foo
47
+ - bar
43
48
  ```
44
49
 
45
50
  # License
data/lib/configru/dsl.rb CHANGED
@@ -16,6 +16,12 @@ module Configru
16
16
  @options[name.to_s] = option
17
17
  end
18
18
 
19
+ def option_required(name, type = Object, validation = nil, &block)
20
+ option = Configru::RequiredOption.new(type, validation, nil)
21
+ RequiredOption.new(option, &block) if block
22
+ @options[name.to_s] = option
23
+ end
24
+
19
25
  def option_array(name, type = Object, default = [], validation = nil, &block)
20
26
  option = Configru::OptionArray.new(type, default, validation, nil)
21
27
  Option.new(option, &block) if block
@@ -27,7 +33,7 @@ module Configru
27
33
  end
28
34
  end
29
35
 
30
- class Option
36
+ class RequiredOption
31
37
  def initialize(option, &block)
32
38
  @option = option
33
39
  instance_eval(&block)
@@ -37,10 +43,6 @@ module Configru
37
43
  @option.type = t
38
44
  end
39
45
 
40
- def default(d)
41
- @option.default = d
42
- end
43
-
44
46
  def validate(v = nil, &block)
45
47
  @option.validation = v || block
46
48
  end
@@ -49,5 +51,11 @@ module Configru
49
51
  @option.transformation = block
50
52
  end
51
53
  end
54
+
55
+ class Option < RequiredOption
56
+ def default(d)
57
+ @option.default = d
58
+ end
59
+ end
52
60
  end
53
61
  end
@@ -1,5 +1,5 @@
1
1
  module Configru
2
- class Option < Struct.new(:type, :default, :validation, :transformation)
2
+ module OptionMethods
3
3
  def type?(value)
4
4
  value.is_a?(self.type)
5
5
  end
@@ -15,6 +15,14 @@ module Configru
15
15
  end
16
16
  end
17
17
 
18
+ class Option < Struct.new(:type, :default, :validation, :transformation)
19
+ include OptionMethods
20
+ end
21
+
22
+ class RequiredOption < Struct.new(:type, :validation, :transformation)
23
+ include OptionMethods
24
+ end
25
+
18
26
  class OptionArray < Option
19
27
  def type?(values)
20
28
  return false unless values.is_a?(Array)
@@ -1,3 +1,3 @@
1
1
  module Configru
2
- VERSION = "3.0.0"
2
+ VERSION = "3.1.0"
3
3
  end
data/lib/configru.rb CHANGED
@@ -11,6 +11,12 @@ module Configru
11
11
  end
12
12
  end
13
13
 
14
+ class OptionRequiredError < OptionError
15
+ def initialize(path)
16
+ super(path, 'option required')
17
+ end
18
+ end
19
+
14
20
  class OptionTypeError < OptionError
15
21
  def initialize(path, expected, got)
16
22
  super(path, "expected #{expected}, got #{got}")
@@ -76,6 +82,8 @@ module Configru
76
82
  elsif output.include? key # option has already been set
77
83
  @option_path.pop
78
84
  next
85
+ elsif option.is_a? RequiredOption
86
+ raise OptionRequiredError.new(@option_path)
79
87
  else # option has not been set
80
88
  value = option.default
81
89
  end
@@ -169,4 +169,12 @@ describe Configru do
169
169
 
170
170
  Configru.example.should == [3, 5, 7, 8]
171
171
  end
172
+
173
+ it 'requires required options' do
174
+ expect do
175
+ Configru.load(example_file :a) do
176
+ option_required :required
177
+ end
178
+ end.to raise_error(Configru::OptionRequiredError)
179
+ end
172
180
  end
data/spec/dsl_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
- describe Configru::DSL::Option do
1
+ describe Configru::DSL::RequiredOption do
2
2
  before do
3
- @option = Configru::Option.new(:type, :default, :validation, :transformation)
3
+ @option = Configru::RequiredOption.new(:type, :validation, :transformation)
4
4
  end
5
5
 
6
6
  it 'sets option type' do
@@ -11,14 +11,6 @@ describe Configru::DSL::Option do
11
11
  @option.type.should == String
12
12
  end
13
13
 
14
- it 'sets option default' do
15
- described_class.new(@option) do
16
- default 'Example'
17
- end
18
-
19
- @option.default.should == 'Example'
20
- end
21
-
22
14
  it 'sets option validate value' do
23
15
  described_class.new(@option) do
24
16
  validate /example/
@@ -50,6 +42,20 @@ describe Configru::DSL::Option do
50
42
  end
51
43
  end
52
44
 
45
+ describe Configru::DSL::Option do
46
+ before do
47
+ @option = Configru::Option.new(:type, :default, :validation, :transformation)
48
+ end
49
+
50
+ it 'sets option default' do
51
+ described_class.new(@option) do
52
+ default 'Example'
53
+ end
54
+
55
+ @option.default.should == 'Example'
56
+ end
57
+ end
58
+
53
59
  describe Configru::DSL::OptionGroup do
54
60
  it 'creates an option' do
55
61
  group = described_class.new do
@@ -72,6 +78,26 @@ describe Configru::DSL::OptionGroup do
72
78
  group.options.should have_key('example')
73
79
  end
74
80
 
81
+ it 'creates a required option' do
82
+ group = described_class.new do
83
+ option_required 'example'
84
+ end
85
+
86
+ group.options.should have_key('example')
87
+ group.options['example'].should be_a(Configru::RequiredOption)
88
+ group.options['example'].type.should == Object
89
+ group.options['example'].validation.should be_nil
90
+ group.options['example'].transformation.should be_nil
91
+ end
92
+
93
+ it 'converts required option names to strings' do
94
+ group = described_class.new do
95
+ option_required :example
96
+ end
97
+
98
+ group.options.should have_key('example')
99
+ end
100
+
75
101
  it 'creates an option array' do
76
102
  group = described_class.new do
77
103
  option_array 'example'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: configru
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-22 00:00:00.000000000 Z
12
+ date: 2012-10-28 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: YAML configuration file loader
15
15
  email:
@@ -59,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
59
  version: '0'
60
60
  segments:
61
61
  - 0
62
- hash: 2949675092247533978
62
+ hash: -933194907027225468
63
63
  required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  none: false
65
65
  requirements:
@@ -68,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  version: '0'
69
69
  segments:
70
70
  - 0
71
- hash: 2949675092247533978
71
+ hash: -933194907027225468
72
72
  requirements: []
73
73
  rubyforge_project:
74
74
  rubygems_version: 1.8.24