zen_config 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -50,7 +50,7 @@ Restrict config scope in your classes :
50
50
 
51
51
  ## Usage
52
52
 
53
- Instantiate ZenConfig with a configuration hash :
53
+ ###Instantiate ZenConfig with a configuration hash :
54
54
 
55
55
  config_hash = { :foo => "foo value", :bar => { :baz => "baz value" } }
56
56
  MyConfig = ZenConfig.new config_hash
@@ -59,31 +59,20 @@ Instantiate ZenConfig with a configuration hash :
59
59
  MyConfig.bar.baz
60
60
  => "baz value"
61
61
 
62
- By default, ZenConfig is read only :
62
+ ###By default, ZenConfig is read only :
63
63
 
64
64
  MyConfig.foo = "bar value"
65
65
  NoMethodError: undefined method `foo=' for #<ZenConfig:0x00000002ee52f8>
66
66
 
67
- But changes can be allowed on build time :
67
+ ###But changes can be allowed on build time :
68
68
 
69
69
  MyConfig = ZenConfig.new config_hash, true
70
70
  MyConfig.foo = "new foo value"
71
71
  => "new foo value"
72
72
  MyConfig.foo
73
73
  => "new foo value"
74
-
75
- ZenConfigs can be converted to hashs :
76
-
77
- MyConfig.to_hash
78
- => {:foo=>"new foo value", :bar=>{:baz=>"baz value"}}
79
-
80
- Config keys can be deleted (if ZenConfig is unlocked) :
81
-
82
- MyConfig.delete :bar
83
- MyConfig.to_hash
84
- => {:foo=>"new foo value"}
85
-
86
- Then the object can be locked to read only again :
74
+
75
+ ###Then the object can be locked to read only again :
87
76
 
88
77
  MyConfig.read_only
89
78
  MyConfig.read_only?
@@ -91,48 +80,106 @@ Then the object can be locked to read only again :
91
80
  MyConfig.foo = "foo value"
92
81
  NoMethodError: undefined method `foo=' for #<ZenConfig:0x00000002ee52f8>
93
82
 
94
- And there's no way to unlock write.
83
+ __And there's no way to unlock write.__
84
+
95
85
  This guarantees that ZenConfig data hasn't been altered since read-only lock has been set.
96
86
  You should not use unlocked ZenConfig in your application code, since you don't know when and where it has been modified.
97
87
  Dynamic persistent writes functions will come in future versions.
98
88
 
99
- Sub configurations can be nested (if ZenConfig object is not locked) :
89
+ ###Sub configurations can be nested (if ZenConfig is unlocked) :
100
90
 
101
91
  MyConfig.new :bar
102
92
  MyConfig.bar.baz = "baz value"
103
93
 
104
- Nested configurations are ZenConfigs. This allow accessing configuration on a specific context :
94
+ ###Nested configurations are ZenConfigs :
105
95
 
106
96
  MyBarConfig = MyConfig.bar
107
97
  MyBarConfig.class
108
98
  => ZenConfig
109
99
  MyBarConfig.baz
110
100
  => "baz value"
101
+
102
+ This allow accessing configuration on a specific context.
111
103
 
112
- Nested ZenConfigs can access their parent :
104
+ ###Nested ZenConfigs can access their parent :
113
105
 
114
106
  MyBarConfig.parent.foo
115
107
  => "bar value"
116
108
 
117
- Of course, root ZenConfig has no parent :
109
+ ###Of course, root ZenConfig has no parent :
118
110
 
119
111
  MyConfig.parent
120
112
  => nil
121
113
 
122
- You can check if a config key exists :
114
+ ###ZenConfigs can be converted to hashs :
123
115
 
124
- MyConfig.foo_exists?
125
- => true
116
+ MyConfig.to_hash
117
+ => {:foo=>"new foo value", :bar=>{:baz=>"baz value"}}
118
+
119
+ ###ZenConfigs subkeys can be parsed :
126
120
 
127
- Count keys :
121
+ MyConfig.each do |key, value|
122
+ puts key.to_s + ":" + value.class.to_s
123
+ end
124
+ foo:String
125
+ bar:ZenConfig
126
+ => {:foo=>"foo value", :bar=>{:baz=>"baz value"}}
127
+
128
+ ###Counted :
128
129
 
129
130
  MyConfig.count
130
131
  => 2
131
132
  MyConfig.bar.count
132
133
  => 1
133
134
 
134
- Note : ZenConfig methods are reserved words that can not be used as config keys.
135
- They'll probably be renamed with a leading underscore in future versions.
135
+ ###Checked :
136
+
137
+ MyConfig.exists? :bar
138
+ => true
139
+
140
+ or
141
+
142
+ MyConfig.bar_exists?
143
+ => true
144
+
145
+ ###Deleted (if ZenConfig is unlocked) :
146
+
147
+ MyConfig.delete :bar
148
+ MyConfig.to_hash
149
+ => {:foo=>"new foo value"}
150
+
151
+ or
152
+
153
+ MyConfig.delete_bar
154
+ MyConfig.to_hash
155
+ => {:foo=>"new foo value"}
156
+
157
+ ## Important note on reserved words :
158
+
159
+ Some words are reserved by Ruby or ZenConfig (public methods or attributes).
160
+
161
+ The best practice is to avoid using one of these reserved words as keys in your config files, but you can call them by adding an underscore in front of the key.
162
+
163
+ Using these reserved words can result in strange behaviors :
164
+
165
+ MyConfig = ZenConfig.new({ :reject => { :default => 10 } })
166
+ [...]
167
+ MyConfig.to_hash
168
+ => {:reject=>{:value=>"10"}}
169
+
170
+ Config is successfully loaded but :
171
+
172
+ MyConfig.reject.value
173
+ NoMethodError: undefined method `value' for #<Enumerator:0x000000026fd5b8>
174
+
175
+ Solution :
176
+
177
+ MyConfig._reject.value
178
+ => 10
179
+
180
+ Reserved words could be reduced by making ZenConfig a BasicObject subclass, but it's not possible as ZenConfig uses Enumerable.
181
+
182
+ __Anyone knowing how to get the best of both worlds is welcome !__
136
183
 
137
184
  ## Goals
138
185
 
data/lib/zen_config.rb CHANGED
@@ -28,6 +28,7 @@ class ZenConfig
28
28
 
29
29
  def delete key
30
30
  if @allow_modifications
31
+ key = key.to_sym
31
32
  backup = @data[key]
32
33
  @data.delete key
33
34
  update_count
@@ -36,14 +37,15 @@ class ZenConfig
36
37
  end
37
38
 
38
39
  def exists? key
39
- @data.has_key? key
40
+ @data.has_key? key.to_sym
40
41
  end
41
42
 
42
- def get name, default = nil
43
+ def get key, default = nil
44
+ key = key.to_sym
43
45
  result = default
44
46
 
45
- if @data.has_key? name
46
- result = @data[name]
47
+ if @data.has_key? key
48
+ result = @data[key]
47
49
  end
48
50
 
49
51
  return result
@@ -63,6 +65,8 @@ class ZenConfig
63
65
  end
64
66
 
65
67
  def set key, value
68
+ key = key.to_sym
69
+
66
70
  if @allow_modifications
67
71
  if value.is_a? Hash
68
72
  @data[key] = self.new value, true
@@ -125,11 +129,11 @@ class ZenConfig
125
129
  value = args.first
126
130
 
127
131
  # Checks if a key exists
128
- if /\A(.*)_exists\?\Z/.match method
132
+ if /\A([a-zA-Z1-9]+)_exists\?\Z/.match method
129
133
  exists? $1
130
134
 
131
135
  # Delete a key
132
- elsif /\Adelete_(.*)\Z/.match method
136
+ elsif /\Adelete_([a-zA-Z1-9]+)\Z/.match method
133
137
  delete $1 if exists? $1
134
138
 
135
139
  # Writes a key value
@@ -139,6 +143,7 @@ class ZenConfig
139
143
 
140
144
  # Reads a key value
141
145
  elsif (args.count == 0)
146
+ method = method[1..-1] if method[0] == '_'
142
147
  get method
143
148
 
144
149
  # Unknown method
@@ -1,3 +1,3 @@
1
1
  class ZenConfig
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
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.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: