configru 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog.md ADDED
@@ -0,0 +1,5 @@
1
+ # Change Log
2
+
3
+ ## 0.2.0 (19 July 2011)
4
+
5
+ * Added loading defaults from file
data/README.md CHANGED
@@ -139,6 +139,11 @@ Configru.load do
139
139
  end
140
140
  ```
141
141
 
142
+ ### Verifying options
143
+
144
+ Configru provides a way to verify that configuration options meet certain
145
+ requirements. This is done using a `verify` block in `Configru.load`.
146
+
142
147
  ## License
143
148
 
144
149
  Copyright (c) 2011, Curtis McEnroe <programble@gmail.com>
data/lib/configru/dsl.rb CHANGED
@@ -40,6 +40,12 @@ module Configru
40
40
  @verify_hash = HashDSL.new(block).hash
41
41
  end
42
42
  end
43
+
44
+ def options(&block)
45
+ hashes = DoubleHashDSL.new(block)
46
+ @defaults_hash = hashes.hash2
47
+ @verify_hash = hashes.hash1
48
+ end
43
49
  end
44
50
 
45
51
  class HashDSL
@@ -59,5 +65,29 @@ module Configru
59
65
  end
60
66
  end
61
67
  end
68
+
69
+ class DoubleHashDSL
70
+ attr_reader :hash1, :hash2
71
+
72
+ def initialize(block)
73
+ @hash1 = {}
74
+ @hash2 = {}
75
+ instance_eval(&block)
76
+ end
77
+
78
+ def method_missing(method, *args, &block)
79
+ key = method.to_s.gsub('_', '-')
80
+ if block
81
+ child = DoubleHashDSL.new(block)
82
+ @hash1[key] = child.hash1
83
+ @hash2[key] = child.hash2
84
+ else
85
+ # Simulate method requiring 2 arguments
86
+ raise ArgumentError, "wrong number of arguments(#{args.length} for 2)" unless args.length == 2
87
+ @hash1[key] = args[0]
88
+ @hash2[key] = args[1]
89
+ end
90
+ end
91
+ end
62
92
  end
63
93
  end
@@ -1,3 +1,3 @@
1
1
  module Configru
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/configru.rb CHANGED
@@ -26,15 +26,20 @@ module Configru
26
26
 
27
27
  def self.reload
28
28
  @config = ConfigHash.new(@defaults)
29
+ @loaded_files = []
29
30
 
30
31
  case @load_method
31
32
  when :first
32
33
  if file = @files.find {|file| File.file?(file)} # Intended
33
34
  @config.merge!(YAML.load_file(file) || {})
35
+ @loaded_files << file
34
36
  end
35
37
  when :cascade
36
38
  @files.reverse_each do |file|
37
- @config.merge!(YAML.load_file(file) || {}) if File.file?(file)
39
+ if File.file?(file)
40
+ @config.merge!(YAML.load_file(file) || {})
41
+ @loaded_files << file
42
+ end
38
43
  end
39
44
  end
40
45
 
@@ -64,6 +69,10 @@ module Configru
64
69
  @verify_stack.shift
65
70
  end
66
71
  end
72
+
73
+ def self.loaded_files
74
+ @loaded_files
75
+ end
67
76
 
68
77
  def self.[](key)
69
78
  @config[key]
data/test/hashdsl_test.rb CHANGED
@@ -39,3 +39,45 @@ context 'HashDSL - keys with underscores' do
39
39
 
40
40
  asserts_topic.equals({'foo-bar' => 1, 'baz-quux' => 2})
41
41
  end
42
+
43
+ context 'DoubleHashDSL - flat' do
44
+ setup do
45
+ block = proc do
46
+ foo 1, 2
47
+ bar 3, 4
48
+ end
49
+ Configru::DSL::DoubleHashDSL.new(block)
50
+ end
51
+
52
+ asserts(:hash1).equals({'foo' => 1, 'bar' => 3})
53
+ asserts(:hash2).equals({'foo' => 2, 'bar' => 4})
54
+ end
55
+
56
+ context 'DoubleHashDSL - nested' do
57
+ setup do
58
+ block = proc do
59
+ foo 1, 2
60
+ bar do
61
+ baz 3, 4
62
+ quux 5, 6
63
+ end
64
+ end
65
+ Configru::DSL::DoubleHashDSL.new(block)
66
+ end
67
+
68
+ asserts(:hash1).equals({'foo' => 1, 'bar' => {'baz' => 3, 'quux' => 5}})
69
+ asserts(:hash2).equals({'foo' => 2, 'bar' => {'baz' => 4, 'quux' => 6}})
70
+ end
71
+
72
+ context 'DoubleHashDSL - keys with underscores' do
73
+ setup do
74
+ block = proc do
75
+ foo_bar 1, 2
76
+ baz_quux 3, 4
77
+ end
78
+ Configru::DSL::DoubleHashDSL.new(block)
79
+ end
80
+
81
+ asserts(:hash1).equals({'foo-bar' => 1, 'baz-quux' => 3})
82
+ asserts(:hash2).equals({'foo-bar' => 2, 'baz-quux' => 4})
83
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Curtis McEnroe
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-07-19 00:00:00 -04:00
17
+ date: 2011-07-30 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -29,6 +29,7 @@ extra_rdoc_files: []
29
29
 
30
30
  files:
31
31
  - .gitignore
32
+ - ChangeLog.md
32
33
  - Gemfile
33
34
  - README.md
34
35
  - Rakefile