funfig 0.0.4 → 0.0.5

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.
Files changed (3) hide show
  1. data/lib/funfig/group.rb +62 -10
  2. data/lib/funfig/version.rb +1 -1
  3. metadata +2 -2
data/lib/funfig/group.rb CHANGED
@@ -23,7 +23,7 @@ module Funfig
23
23
  def update(hash)
24
24
  if hash.respond_to?(:each)
25
25
  hash.each{|k, v|
26
- k = k.gsub('-', '_')
26
+ k = k.to_s.gsub('-', '_')
27
27
  self.send("#{k}=", v)
28
28
  }
29
29
  end
@@ -65,6 +65,24 @@ module Funfig
65
65
  h
66
66
  end
67
67
 
68
+ # Imitate aget
69
+ def [](k)
70
+ if self.class._params[k.to_sym]
71
+ send(k)
72
+ else
73
+ raise "Attempt to get not existed param #{_sub_name(k)}"
74
+ end
75
+ end
76
+
77
+ # Imitate aset
78
+ def []=(k, v)
79
+ if self.class._params[k.to_sym]
80
+ send("#{k}=", v)
81
+ else
82
+ raise "Attempt to set not existed param #{_sub_name(k)}"
83
+ end
84
+ end
85
+
68
86
  def inspect
69
87
  "<#{self.class.name} #{each.map{|k,v| "#{k}=#{v.inspect}"}.join(' ')}>"
70
88
  end
@@ -87,6 +105,10 @@ module Funfig
87
105
  self.class._path
88
106
  end
89
107
 
108
+ def method_missing(name, *args)
109
+ raise "Not existed param #{_sub_name(name.to_s.sub(/=$/,''))}"
110
+ end
111
+
90
112
  def self._params
91
113
  @params ||= {}
92
114
  end
@@ -133,7 +155,7 @@ module Funfig
133
155
 
134
156
  define_method(name) do |*args, &block|
135
157
  instance_variable_get(vname) ||
136
- instance_variable_set(vname, klass.new(self))
158
+ instance_variable_set(vname, self.class._params[name].new(self))
137
159
  end
138
160
 
139
161
  define_method("#{name}=") do |hash|
@@ -158,14 +180,16 @@ module Funfig
158
180
  vname = :"@#{name}"
159
181
  name = name.to_sym
160
182
 
161
- block ||= proc{
162
- begin
163
- value.dup
164
- rescue TypeError
165
- block = proc { value }
166
- value
167
- end
168
- }
183
+ unless block || value == NOT_SET
184
+ block = proc{
185
+ begin
186
+ value.dup
187
+ rescue TypeError
188
+ block = proc { value }
189
+ value
190
+ end
191
+ }
192
+ end
169
193
 
170
194
  define_method(name) do |*args|
171
195
  if instance_variable_defined?(vname)
@@ -179,6 +203,8 @@ module Funfig
179
203
  end
180
204
  end
181
205
 
206
+ alias_method :"#{name}?", name
207
+
182
208
  define_method("#{name}=") do |v|
183
209
  _._cache_clear!
184
210
  instance_variable_set(vname, v)
@@ -201,5 +227,31 @@ module Funfig
201
227
  new.class_exec &block if block_given?
202
228
  new
203
229
  end
230
+
231
+ def self.drained
232
+ klone = clone
233
+ for par, val in _params
234
+ if val.is_a?(Class) && Group > val
235
+ klone._params[par] = val.drained
236
+ else
237
+ klone.param(par)
238
+ end
239
+ end
240
+ klone
241
+ end
242
+
243
+ def self.as_root
244
+ params = _params
245
+ new_root = Funfig.new do
246
+ for par, val in params
247
+ if val.is_a?(Class) && Group > val
248
+ _params[par] = val.drained
249
+ group(par){ }
250
+ else
251
+ param(par)
252
+ end
253
+ end
254
+ end
255
+ end
204
256
  end
205
257
  end
@@ -1,3 +1,3 @@
1
1
  module Funfig
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: funfig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
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-06-08 00:00:00.000000000 Z
12
+ date: 2012-06-15 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Defines configuration schema with calculable defaults
15
15
  email: