conf 0.0.3 → 0.0.4

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.
data/README.rdoc CHANGED
@@ -29,11 +29,15 @@ Simple configuration that supports inheritance.
29
29
  c.single #=> "pair"
30
30
  c.properties.like.syntax #=> :nice
31
31
 
32
- c.yet.another :value
33
- c.yet.another #=> :value
34
-
35
- c.freeze
36
- c.yet.another :changed #=> can't modify frozen config
32
+ # can't modify locked config
33
+ c.nested.ruby.block true #=> InvalidStateError
34
+
35
+ # unlock it first
36
+ c.unlocked do
37
+ nested.ruby.block true
38
+ end
39
+
40
+ c.nested.ruby.block #=> true
37
41
 
38
42
 
39
43
  == Note on Patches/Pull Requests
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/conf.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{conf}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jari Bakken"]
data/lib/conf.rb CHANGED
@@ -2,6 +2,9 @@ class Conf
2
2
 
3
3
  class InvalidKeyError < StandardError
4
4
  end
5
+
6
+ class InvalidStateError < StandardError
7
+ end
5
8
 
6
9
  def self.configs
7
10
  @configs ||= {}
@@ -20,7 +23,7 @@ class Conf
20
23
  conf = configs[name] ||= Configuration.new(parent)
21
24
 
22
25
  conf.instance_eval(&blk)
23
- conf.freeze
26
+ conf.lock!
24
27
  conf
25
28
  end
26
29
 
@@ -34,18 +37,32 @@ class Conf
34
37
  raise TypeError, "expected #{self.class}, got #{parent.inspect}:#{parent.class}"
35
38
  end
36
39
 
37
- @parent = parent
38
- @data = {}
40
+ @parent = parent
41
+ @data = {}
39
42
  @current_nesting = []
43
+ @locked = false
40
44
  end
41
45
 
42
46
  def key?(key)
43
47
  @data.key?(key) || (@parent && @parent.key?(key))
44
48
  end
45
49
 
46
- def freeze
47
- @parent && @parent.freeze
48
- super
50
+ def lock!
51
+ @locked = true
52
+ end
53
+
54
+ def unlock!
55
+ @locked = false
56
+ end
57
+
58
+ def edit(&blk)
59
+ edit!
60
+ instance_eval(&blk)
61
+ done!
62
+ end
63
+
64
+ def locked?
65
+ @locked
49
66
  end
50
67
 
51
68
  def section(start_key)
@@ -81,11 +98,11 @@ class Conf
81
98
  m = meth.to_s
82
99
 
83
100
  if m =~ /^(\w+)=/ || args.size == 1
84
- check_frozen
101
+ check_lock
85
102
  key = $1 || m
86
103
  self[key] = args.first
87
104
  elsif blk
88
- check_frozen
105
+ check_lock
89
106
  @current_nesting << m
90
107
  instance_eval(&blk)
91
108
  @current_nesting.pop
@@ -96,7 +113,7 @@ class Conf
96
113
  obj
97
114
  else
98
115
  @current_nesting << m
99
- validate_nesting if frozen?
116
+ validate_nesting if locked?
100
117
  self
101
118
  end
102
119
  end
@@ -112,10 +129,10 @@ class Conf
112
129
  end
113
130
  end
114
131
 
115
- def check_frozen
116
- if frozen?
132
+ def check_lock
133
+ if locked?
117
134
  @current_nesting.clear
118
- raise "can't modify frozen config"
135
+ raise InvalidStateError, "config is locked"
119
136
  end
120
137
  end
121
138
  end
data/spec/conf_spec.rb CHANGED
@@ -80,7 +80,7 @@ describe "Conf" do
80
80
  it "cannot be changed after being defined" do
81
81
  conf = Conf.define(:tmp) { foo "bar" }
82
82
  conf.foo.should == "bar"
83
- lambda { conf.foo "baz" }.should raise_error
83
+ lambda { conf.foo "baz" }.should raise_error(Conf::InvalidStateError)
84
84
  end
85
85
 
86
86
  it "raises a TypeError if parent is not nil or Configuration instance" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jari Bakken