zen_config 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -22,10 +22,12 @@ Or install it yourself as:
22
22
 
23
23
  Instantiate ZenConfig with a configuration hash :
24
24
 
25
- config_hash = { :foo => "foo value" }
25
+ config_hash = { :foo => "foo value", :bar => { :baz => "baz value" } }
26
26
  MyConfig = ZenConfig.new config_hash
27
27
  MyConfig.foo
28
28
  => "foo value"
29
+ MyConfig.bar.baz
30
+ => "baz value"
29
31
 
30
32
  By default, ZenConfig is read only :
31
33
 
@@ -35,10 +37,21 @@ By default, ZenConfig is read only :
35
37
  But changes can be allowed on build time :
36
38
 
37
39
  MyConfig = ZenConfig.new config_hash, true
38
- MyConfig.foo = "bar value"
39
- => "bar value"
40
+ MyConfig.foo = "new foo value"
41
+ => "new foo value"
40
42
  MyConfig.foo
41
- => "bar value"
43
+ => "new foo value"
44
+
45
+ ZenConfigs can be converted to hashs :
46
+
47
+ MyConfig.to_hash
48
+ => {:foo=>"new foo value", :bar=>{:baz=>"baz value"}}
49
+
50
+ Config keys can be deleted (if ZenConfig is unlocked) :
51
+
52
+ MyConfig.delete :bar
53
+ MyConfig.to_hash
54
+ => {:foo=>"new foo value"}
42
55
 
43
56
  Then the object can be locked to read only again :
44
57
 
@@ -88,17 +101,6 @@ Count keys :
88
101
  MyConfig.bar.count
89
102
  => 1
90
103
 
91
- ZenConfigs can be converted to hashs :
92
-
93
- MyConfig.to_hash
94
- => {:foo=>"bar value", :bar=>{:baz=>"baz value"}}
95
-
96
- And finally, config keys can be deleted (if ZenConfig is unlocked) :
97
-
98
- MyConfig.delete :foo
99
- MyConfig.to_hash
100
- => {:bar=>{:baz=>"baz value"}}
101
-
102
104
  Note : ZenConfig methods are reserved words that can not be used as config keys.
103
105
  They'll probably be renamed with a leading underscore in future versions.
104
106
 
@@ -112,5 +114,4 @@ They'll probably be renamed with a leading underscore in future versions.
112
114
 
113
115
  ## Known bugs
114
116
 
115
- - Nested hashs raise an error on init
116
117
  - Merging doesn't always work
@@ -1,3 +1,3 @@
1
1
  class ZenConfig
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/zen_config.rb CHANGED
@@ -8,27 +8,36 @@ class ZenConfig
8
8
  def_delegators :@data, :each, :<<
9
9
 
10
10
  def initialize hash, allow_modifications = false
11
- @allow_modifications = !!allow_modifications
11
+ @allow_modifications = true
12
12
  @loaded_section = nil
13
13
  @index = 0
14
14
  @data = {}
15
15
 
16
- hash.each do |key, value|
17
- key = key.to_sym
16
+ load_hash hash
18
17
 
19
- if value.is_a? Hash
20
- @data[key] = self.new value, @allow_modifications
21
- else
22
- @data[key] = value
23
- end
24
-
25
- set_key_parent key
18
+ if allow_modifications
19
+ read_only
26
20
  end
21
+ end
27
22
 
28
- update_count
23
+ # Keys
24
+
25
+ def count
26
+ @count
29
27
  end
30
28
 
31
- # Accessors
29
+ def delete key
30
+ if @allow_modifications
31
+ backup = @data[key]
32
+ @data.delete key
33
+ update_count
34
+ return backup
35
+ end
36
+ end
37
+
38
+ def exists? key
39
+ @data.has_key? key
40
+ end
32
41
 
33
42
  def get name, default = nil
34
43
  result = default
@@ -40,6 +49,19 @@ class ZenConfig
40
49
  return result
41
50
  end
42
51
 
52
+ def new key, force = false
53
+ key = key.to_sym
54
+
55
+ if @allow_modifications
56
+ if (!exists? key) || force
57
+ @data[key] = ZenConfig.new({}, true)
58
+ set_key_parent key
59
+ else
60
+ raise "'#{key}' key already exists."
61
+ end
62
+ end
63
+ end
64
+
43
65
  def set key, value
44
66
  if @allow_modifications
45
67
  if value.is_a? Hash
@@ -55,40 +77,27 @@ class ZenConfig
55
77
  end
56
78
  end
57
79
 
58
- # Data
59
-
60
- def count
61
- @count
62
- end
80
+ #Global
63
81
 
64
- def delete key
82
+ def load_hash hash
65
83
  if @allow_modifications
66
- backup = @data[key]
67
- @data.delete key
68
- update_count
69
- return backup
70
- end
71
- end
72
-
73
- def exists? key
74
- @data.has_key? key
75
- end
84
+ hash.each do |key, value|
85
+ puts key.to_s + " : " + value.to_s
86
+ key = key.to_sym
76
87
 
77
- def new key, force = false
78
- key = key.to_sym
88
+ if value.is_a? Hash
89
+ @data[key] = ZenConfig.new value, @allow_modifications
90
+ else
91
+ @data[key] = value
92
+ end
79
93
 
80
- if @allow_modifications
81
- if (!exists? key) || force
82
- @data[key] = ZenConfig.new({}, true)
83
94
  set_key_parent key
84
- else
85
- raise "'#{key}' key already exists."
86
95
  end
96
+
97
+ update_count
87
98
  end
88
99
  end
89
100
 
90
- #Global
91
-
92
101
  def merge merge
93
102
  raise "Merged configuration must be a ZenConfig" unless merge.kind_of? ZenConfig
94
103
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zen_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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-04-09 00:00:00.000000000 Z
12
+ date: 2012-04-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec