funfig 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,10 +23,19 @@ module Funfig
23
23
  end
24
24
  end
25
25
 
26
+ class ProxyRef < BasicObject
27
+ def initialize(group)
28
+ @group = group
29
+ end
30
+ def method_missing(name, group, &block)
31
+ @group.reference name, group, &block
32
+ end
33
+ end
34
+
26
35
  class Group
27
36
  # syntax sugar proxy for declaring params
28
37
  #
29
- # :call-seq
38
+ # :call-seq:
30
39
  # conf = Funfig.new do
31
40
  # p.name_of_param :default_value
32
41
  # p.other_param { calculate_default }
@@ -37,7 +46,7 @@ module Funfig
37
46
 
38
47
  # syntax sugar proxy for declaring group
39
48
  #
40
- # :call-seq
49
+ # :call-seq:
41
50
  # conf = Funfig.new do
42
51
  # g.name_of_group do
43
52
  # end
@@ -45,5 +54,20 @@ module Funfig
45
54
  def self.g
46
55
  @group_param ||= ProxyGroup.new(self)
47
56
  end
57
+
58
+ # syntax sugar proxy for declaring references
59
+ #
60
+ # :call-seq:
61
+ # conf = Funfig.new do
62
+ # r.name_of_reference _.path.to.group do
63
+ # end
64
+ # end
65
+ #
66
+ # conf = Funfig.new do
67
+ # r.name_of_reference _.path.to.group
68
+ # end
69
+ def self.r
70
+ @ref_param ||= ProxyRef.new(self)
71
+ end
48
72
  end
49
73
  end
data/lib/funfig/group.rb CHANGED
@@ -26,6 +26,8 @@ module Funfig
26
26
  k = k.to_s.gsub('-', '_')
27
27
  self.send("#{k}=", v)
28
28
  }
29
+ else
30
+ raise "Could not use #{hash} as value for group #{_path}"
29
31
  end
30
32
  self
31
33
  end
@@ -65,6 +67,14 @@ module Funfig
65
67
  h
66
68
  end
67
69
 
70
+ def to_yaml(explicit=false)
71
+ to_hash(explicit).to_yaml
72
+ end
73
+
74
+ def to_json(explicit=false)
75
+ to_hash(explicit).to_json
76
+ end
77
+
68
78
  # Imitate aget
69
79
  def [](k)
70
80
  if self.class._params[k.to_sym]
@@ -133,6 +143,26 @@ module Funfig
133
143
 
134
144
  # :startdoc:
135
145
 
146
+ def self._parent
147
+ @parent
148
+ end
149
+
150
+ def self._root
151
+ @parent._root
152
+ end
153
+
154
+ def self._
155
+ _root
156
+ end
157
+
158
+ def self.method_missing(name, *args)
159
+ (args.empty? && _params[name.to_sym]) || super
160
+ end
161
+
162
+ def self._path_proc
163
+ eval "lambda{ #{_path} }"
164
+ end
165
+
136
166
  # Define named group of values
137
167
  #
138
168
  # :call-seq:
@@ -152,6 +182,7 @@ module Funfig
152
182
  klass.send(:define_singleton_method, :_path) do
153
183
  path
154
184
  end
185
+ klass.instance_variable_set(:@parent, self)
155
186
 
156
187
  define_method(name) do |*args, &block|
157
188
  instance_variable_get(vname) ||
@@ -167,6 +198,7 @@ module Funfig
167
198
  remove_instance_variable(vname) if instance_variable_defined?(vname)
168
199
  end
169
200
  klass.class_exec &block
201
+ klass
170
202
  end
171
203
 
172
204
  # define named parameter
@@ -216,6 +248,39 @@ module Funfig
216
248
  end
217
249
  end
218
250
 
251
+ # Create a reference to other group in config
252
+ #
253
+ # :call-seq:
254
+ # conf = Funfug.new do
255
+ # group :some do
256
+ # group :origin do
257
+ # param :first, 1
258
+ # param :second, 2
259
+ # end
260
+ # end
261
+ # group :awe do
262
+ # reference :copy, _.some.origin do
263
+ # # implicitly added:
264
+ # # param :first do _.some.origin.first end
265
+ # # param :second do _.some.origin.second end
266
+ # param :third, 3
267
+ # end
268
+ # end
269
+ def self.reference(name, refered, &block)
270
+ path_proc = refered._path_proc
271
+ refered_params = refered._params
272
+ group name do
273
+ refered_params.each do |name, value|
274
+ if value == true
275
+ param(name){ instance_exec(&path_proc).send(name) }
276
+ else
277
+ reference(name, value)
278
+ end
279
+ end
280
+ class_exec &block if block
281
+ end
282
+ end
283
+
219
284
  # Create a copy of configuration scheme
220
285
  #
221
286
  # :call-seq:
data/lib/funfig/root.rb CHANGED
@@ -21,11 +21,11 @@ module Funfig
21
21
  alias _ _root
22
22
 
23
23
  def self._path
24
- ""
24
+ "_"
25
25
  end
26
26
 
27
- def self._sub_name(name)
28
- name.to_s
27
+ def self._root
28
+ self
29
29
  end
30
30
  end
31
31
  # :startdoc:
@@ -1,3 +1,3 @@
1
1
  module Funfig
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
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.5
4
+ version: 0.0.6
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-15 00:00:00.000000000 Z
12
+ date: 2012-07-26 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Defines configuration schema with calculable defaults
15
15
  email: