fabulator 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,12 @@
1
- === 0.0.5
1
+ === 0.0.6 2010-08-27
2
+
3
+ * 4 minor enhancement:
4
+ * Better regex for matching quoted strings in expressions
5
+ * Less duplication of compile-time contexts
6
+ * Template add_caption call can take a hash or a node context
7
+ * set_value can take a node as a source instead of only an expression
8
+
9
+ === 0.0.5 2010-08-18
2
10
 
3
11
  * 2 major enhancements:
4
12
  * unified predicate execution so numeric indexes and boolean logic
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
@@ -71,7 +71,8 @@ Then /I should get the type (.*)/ do |t|
71
71
  end
72
72
 
73
73
  Then /I should get (\d+) items?/ do |count|
74
- @result.length.should == count
74
+ #puts "result types: #{@result.collect{|r| r.class.name}.join(', ')}"
75
+ @result.size.should == count
75
76
  end
76
77
 
77
78
  Then /item (\d+) should be (\[.*\])/ do |i,t|
@@ -175,13 +175,13 @@ module Fabulator
175
175
  end
176
176
  end
177
177
  end
178
- common = d1.keys & d2.keys
179
- if ordered && common.include?(t2.join(''))
180
- return d1[t2.join('')]
181
- elsif !common.empty?
182
- return d1[common.sort_by{ |c| d1[c][:w] * d2[c][:w] / d1[c][:path].size / d2[c][:path].size }.reverse.first]
183
- end
178
+ r = self._select_type_path(d1, d2, t2, ordered)
179
+ return r unless r.nil?
184
180
  end
181
+ return self._select_type_path(d1, d2, t2, ordered)
182
+ end
183
+
184
+ def self._select_type_path(d1, d2, t2, ordered)
185
185
  common = d1.keys & d2.keys
186
186
  if ordered && common.include?(t2.join(''))
187
187
  return d1[t2.join('')]
@@ -205,7 +205,7 @@ module Fabulator
205
205
 
206
206
  def compile_action(e, c)
207
207
  if self.class.method_defined? "action:#{e.name}"
208
- send "action:#{e.name}", e, c.merge(e)
208
+ send "action:#{e.name}", e, c #.merge(e)
209
209
  end
210
210
  end
211
211
 
@@ -66,8 +66,7 @@ module Fabulator
66
66
  return sense.call(paths) if select.nil?
67
67
  opts = @select.run(ctx).collect { |o| o.to_s }
68
68
  if !opts.include?(ctx.root.to_s)
69
- paths[0] -= [ ctx.root.path ]
70
- paths[1] += [ ctx.root.path ]
69
+ invalidate_path(paths, ctx.root)
71
70
  end
72
71
  return sense.call(paths)
73
72
  when 'all':
@@ -94,8 +93,7 @@ module Fabulator
94
93
  end
95
94
  end
96
95
  if !calc_values.include?(ctx.root.value)
97
- paths[0] -= [ ctx.root.path ]
98
- paths[1] += [ ctx.root.path ]
96
+ invalidate_path(paths, ctx.root)
99
97
  end
100
98
  return sense.call(paths)
101
99
  end
@@ -103,8 +101,7 @@ module Fabulator
103
101
  fl = (@params['floor'].run(ctx) rescue nil)
104
102
  ce = (@params['ceiling'].run(ctx) rescue nil)
105
103
  if !fl.nil? && fl > c.value || !ce.nil? && ce < c.value
106
- paths[0] -= [ c.path ]
107
- paths[1] += [ c.path ]
104
+ invalidate_path(paths, c)
108
105
  end
109
106
  return sense.call(r)
110
107
  else
@@ -112,6 +109,13 @@ module Fabulator
112
109
  end
113
110
  end
114
111
  end
112
+
113
+ protected
114
+
115
+ def invalidate_path(paths, root)
116
+ paths[0] -= [ root.path ]
117
+ paths[1] += [ root.path ]
118
+ end
115
119
  end
116
120
  end
117
121
  end
@@ -1,7 +1,7 @@
1
1
  module Fabulator
2
2
  module Core
3
3
  class Group < Fabulator::Action
4
- attr_accessor :name, :params, :tags
4
+ attr_accessor :name, :params, :tags, :required_params
5
5
 
6
6
  namespace Fabulator::FAB_NS
7
7
 
@@ -91,33 +91,6 @@ module Fabulator
91
91
  end
92
92
  ret
93
93
  end
94
-
95
- def test_constraints(context)
96
- passed = [ ]
97
- failed = [ ]
98
- @context.with(context) do |ctx|
99
- roots = self.get_context(ctx)
100
- roots.each do |root|
101
- @params.each do |param|
102
- p_ctx = param.get_context(ctx.with_root(root))
103
- if !p_ctx.nil? && !p_ctx.empty?
104
- p_ctx.each do |p|
105
- @constraints.each do |c|
106
- r = c.test_constraint(ctx.with_root(p))
107
- passed += r[0]
108
- failed += r[1]
109
- end
110
- end
111
- end
112
- end
113
- end
114
- end
115
- if failed.empty?
116
- return [ passed.uniq, [] ]
117
- else
118
- return [ (passed - failed).uniq, failed ]
119
- end
120
- end
121
94
  end
122
95
  end
123
96
  end
@@ -16,6 +16,10 @@ module Fabulator
16
16
  [ @name ]
17
17
  end
18
18
 
19
+ def names
20
+ [ @name ]
21
+ end
22
+
19
23
  def compile_xml(xml, context)
20
24
  super
21
25
 
@@ -101,28 +105,6 @@ module Fabulator
101
105
  return res
102
106
  end
103
107
 
104
- def test_constraints(context)
105
- @context.with(context) do |ctx|
106
- me = ctx.traverse_path(@name)
107
- return [ [ me.collect{ |m| m.path } ], [] ] if @constraints.empty?
108
- paths = [ [], [] ]
109
- if @all_constraints
110
- @constraints.each do |c|
111
- p = c.test_constraints(ctx, me)
112
- paths[0] += p[0]
113
- paths[1] += p[1]
114
- end
115
- return [ (paths[0] - paths[1]).uniq, paths[1].uniq ]
116
- else
117
- @constraints.each do |c|
118
- p = c.test_constraints(ctx, me)
119
- paths[0] += p[0]
120
- paths[1] += p[1]
121
- end
122
- return [ paths[0].uniq, (paths[1] - paths[0]).uniq ]
123
- end
124
- end
125
- end
126
108
  end
127
109
  end
128
110
  end
@@ -39,28 +39,7 @@ module Fabulator
39
39
  case e.name
40
40
  # TODO: handle parameters when inheriting
41
41
  when 'params':
42
- p_ctx = @context.merge(e)
43
- @select = p_ctx.get_select('/')
44
-
45
- e.each_element do |ee|
46
- next unless ee.namespaces.namespace.href == FAB_NS
47
- case ee.name
48
- when 'group':
49
- if !inheriting
50
- g = Group.new.compile_xml(ee, p_ctx)
51
- @params << g
52
- else
53
- tags = (ee.attributes.get_attribute_ns(FAB_NS, 'tag').value rescue '').split(/\s+/)
54
- end
55
- when 'param':
56
- if !inheriting
57
- p = Parameter.new.compile_xml(ee, p_ctx)
58
- @params << p
59
- else
60
- tags = (ee.attributes.get_attribute_ns(FAB_NS, 'tag').value rescue '').split(/\s+/)
61
- end
62
- end
63
- end
42
+ @params << Group.new.compile_xml(e, @context)
64
43
  end
65
44
  end
66
45
  self
@@ -126,10 +105,10 @@ module Fabulator
126
105
  res[:valid].delete(k)
127
106
  end
128
107
 
129
- res[:score] = (res[:valid].size+1)*(params.size)
130
- res[:score] = res[:score] / (res[:missing].size + 1)
131
- res[:score] = res[:score] / (res[:invalid].size + 1)
132
- res[:score] = res[:score] / (res[:unknown].size + 1)
108
+ res[:score] = (res[:valid].size+1)*(params.size) /
109
+ (res[:missing].size + 1) /
110
+ (res[:invalid].size + 1) /
111
+ (res[:unknown].size + 1)
133
112
  return res
134
113
  end
135
114
 
@@ -239,17 +239,18 @@ module Fabulator
239
239
  v = parser.parse(v,self) if v.is_a?(String)
240
240
  end
241
241
 
242
- return [] if p.nil?
243
-
242
+ #return [] if p.nil?
243
+ p = [ self.root ] if p.nil?
244
+
244
245
  p = [ p ] unless p.is_a?(Array)
245
246
 
246
247
  ret = [ ]
247
248
 
248
249
  p.each do |pp|
249
- tgts = pp.run(self, true)
250
+ tgts = pp.is_a?(Fabulator::Expr::Node) ? [ pp ] : pp.run(self, true)
250
251
  src = nil
251
252
  if !v.nil?
252
- src = v.run(self)
253
+ src = v.is_a?(Fabulator::Expr::Node) ? [ v ] : v.run(self)
253
254
  end
254
255
 
255
256
  tgts.each do |tgt|
@@ -305,7 +306,9 @@ module Fabulator
305
306
  else
306
307
  root_context = root_context.first
307
308
  end
308
- if d.is_a?(Array)
309
+ if d.is_a?(Fabulator::Expr::Node)
310
+ self.set_value(p, d)
311
+ elsif d.is_a?(Array)
309
312
  node_name = root_context.name
310
313
  root_context = root_context.parent
311
314
  # get rid of empty children so we don't have problems later
@@ -322,7 +325,7 @@ module Fabulator
322
325
  d.each_pair do |k,v|
323
326
  bits = k.split('.')
324
327
  c = self.with_root(root_context).traverse_path(bits,true).first
325
- if v.is_a?(Hash) || v.is_a?(Array)
328
+ if v.is_a?(Hash) || v.is_a?(Array) || v.is_a?(Fabulator::Expr::Node)
326
329
  self.with_root(c).merge_data(v)
327
330
  else
328
331
  c.value = v
@@ -84,59 +84,6 @@ module Fabulator
84
84
  node
85
85
  end
86
86
 
87
- def merge_data(d,p = nil)
88
- # we have a hash or array based on root (r)
89
- if p.nil?
90
- root_context = [ self ]
91
- else
92
- root_context = self.traverse_path(p,true)
93
- end
94
- if root_context.size > 1
95
- # see if we need to prune
96
- new_rc = [ ]
97
- root_context.each do |c|
98
- if c.children.size == 0 && c.value.nil?
99
- c.parent.prune(c) if c.parent
100
- else
101
- new_rc << c
102
- end
103
- end
104
- if new_rc.size > 0
105
- raise "Unable to merge data into multiple places simultaneously"
106
- else
107
- root_context = new_rc
108
- end
109
- else
110
- root_context = root_context.first
111
- end
112
- if d.is_a?(Array)
113
- node_name = root_context.name
114
- root_context = root_context.parent
115
- # get rid of empty children so we don't have problems later
116
- root_context.children.each do |c|
117
- if c.children.size == 0 && c.name == node_name && c.value.nil?
118
- c.parent.prune(c)
119
- end
120
- end
121
- d.each do |i|
122
- c = root_context.create_child(node_name)
123
- c.merge_data(i)
124
- end
125
- elsif d.is_a?(Hash)
126
- d.each_pair do |k,v|
127
- bits = k.split('.')
128
- c = root_context.traverse_path(bits,true).first
129
- if v.is_a?(Hash) || v.is_a?(Array)
130
- c.merge_data(v)
131
- else
132
- c.value = v
133
- end
134
- end
135
- else
136
- c = root_context.parent.create_child(root_context.name, d)
137
- end
138
- end
139
-
140
87
  def parent=(p)
141
88
  @parent = p
142
89
  @axis = p.axis
@@ -9,7 +9,7 @@ module Fabulator
9
9
  module Expr
10
10
  class Parser < Racc::Parser
11
11
 
12
- module_eval(<<'...end xsm_expression_parser.racc/module_eval...', 'xsm_expression_parser.racc', 179)
12
+ module_eval(<<'...end xsm_expression_parser.racc/module_eval...', 'xsm_expression_parser.racc', 187)
13
13
  require 'fabulator/expr'
14
14
  require 'rational'
15
15
  require 'bigdecimal'
@@ -41,6 +41,7 @@ module_eval(<<'...end xsm_expression_parser.racc/module_eval...', 'xsm_expressio
41
41
  :axis_name => %r{(?:attribute|child|child-or-self|descendant|descendant-or-self|method|self)},
42
42
  :namespace_name => %r{(?:context|global|local|session|universal)},
43
43
  :number => %r{(-?\d+(?:\.\d+)?|\.\d+)},
44
+ :literal => %r{((?:"(?:[^\\"]*(?:\\.[^\\"]*)*)")|(?:'(?:[^\\']*(?:\\.[^\\']*)*)'))},
44
45
  }
45
46
 
46
47
  @@regex[:axis] = %r{(#{@@regex[:ncname]})\s*(?=::)}
@@ -88,7 +89,7 @@ module_eval(<<'...end xsm_expression_parser.racc/module_eval...', 'xsm_expressio
88
89
  == & && ||
89
90
  }.split(/\s*/), '[', ',', '$' ].each { |t| @@preceding_tokens[t] = true }
90
91
 
91
- @@regex[:general] = Regexp.compile(%{^(?:#{@@regex[:function_name]}|#{@@regex[:axis]}|#{@@regex[:name_colon_star]}|#{@@regex[:qname]}|('[^']*'|"[^"]*")|#{@@regex[:number]}|#{@@regex[:dollar_qname]}|#{@@regex[:dollar_int]}|(#{@@regex[:simple_tokens]}))})
92
+ @@regex[:general] = Regexp.compile(%{^(?:#{@@regex[:function_name]}|#{@@regex[:axis]}|#{@@regex[:name_colon_star]}|#{@@regex[:qname]}|#{@@regex[:literal]}|#{@@regex[:number]}|#{@@regex[:dollar_qname]}|#{@@regex[:dollar_int]}|(#{@@regex[:simple_tokens]}))})
92
93
 
93
94
  def next_token
94
95
  @token = nil
@@ -1410,21 +1411,28 @@ module_eval(<<'.,.,', 'xsm_expression_parser.racc', 158)
1410
1411
  end
1411
1412
  .,.,
1412
1413
 
1413
- module_eval(<<'.,.,', 'xsm_expression_parser.racc', 159)
1414
+ module_eval(<<'.,.,', 'xsm_expression_parser.racc', 160)
1414
1415
  def _reduce_90(val, _values, result)
1415
- result = Fabulator::Expr::Function.new(@context, val[0], val[1])
1416
+ result = Fabulator::Expr::Function.new(@context, val[0], val[1])
1417
+ # when 'any' : Fabulator::Expr::AnyExpr.new(@context, val[1])
1418
+ # when 'all' : Fabulator::Expr::AllExpr.new(@context, val[1])
1419
+ # when 'one' : Fabulator::Expr::OneExpr.new(@context, val[1])
1420
+ # when 'none': Fabulator::Expr::NoneExpr.new(@context, val[1])
1421
+ # else Fabulator::Expr::Function.new(@context, val[0], val[1])
1422
+ # end
1423
+
1416
1424
  result
1417
1425
  end
1418
1426
  .,.,
1419
1427
 
1420
- module_eval(<<'.,.,', 'xsm_expression_parser.racc', 161)
1428
+ module_eval(<<'.,.,', 'xsm_expression_parser.racc', 169)
1421
1429
  def _reduce_91(val, _values, result)
1422
1430
  result = Fabulator::Expr::List.new(val[1])
1423
1431
  result
1424
1432
  end
1425
1433
  .,.,
1426
1434
 
1427
- module_eval(<<'.,.,', 'xsm_expression_parser.racc', 164)
1435
+ module_eval(<<'.,.,', 'xsm_expression_parser.racc', 172)
1428
1436
  def _reduce_92(val, _values, result)
1429
1437
  result = [ ]
1430
1438
  result
@@ -1433,14 +1441,14 @@ module_eval(<<'.,.,', 'xsm_expression_parser.racc', 164)
1433
1441
 
1434
1442
  # reduce 93 omitted
1435
1443
 
1436
- module_eval(<<'.,.,', 'xsm_expression_parser.racc', 167)
1444
+ module_eval(<<'.,.,', 'xsm_expression_parser.racc', 175)
1437
1445
  def _reduce_94(val, _values, result)
1438
1446
  result = [ val[0] ]
1439
1447
  result
1440
1448
  end
1441
1449
  .,.,
1442
1450
 
1443
- module_eval(<<'.,.,', 'xsm_expression_parser.racc', 168)
1451
+ module_eval(<<'.,.,', 'xsm_expression_parser.racc', 176)
1444
1452
  def _reduce_95(val, _values, result)
1445
1453
  result = val[0] + [ val[2] ]
1446
1454
  result
@@ -1449,14 +1457,14 @@ module_eval(<<'.,.,', 'xsm_expression_parser.racc', 168)
1449
1457
 
1450
1458
  # reduce 96 omitted
1451
1459
 
1452
- module_eval(<<'.,.,', 'xsm_expression_parser.racc', 171)
1460
+ module_eval(<<'.,.,', 'xsm_expression_parser.racc', 179)
1453
1461
  def _reduce_97(val, _values, result)
1454
1462
  result = val[0].to_s
1455
1463
  result
1456
1464
  end
1457
1465
  .,.,
1458
1466
 
1459
- module_eval(<<'.,.,', 'xsm_expression_parser.racc', 172)
1467
+ module_eval(<<'.,.,', 'xsm_expression_parser.racc', 180)
1460
1468
  def _reduce_98(val, _values, result)
1461
1469
  result = val[1]
1462
1470
  result
@@ -55,15 +55,5 @@ module Fabulator
55
55
  return possible
56
56
  end
57
57
  end
58
-
59
- class IndexPredicate
60
- def initialize(l)
61
- @indices = l
62
- end
63
-
64
- def run(context)
65
- @indices.collect { |e| e.run(context).collect{ |i| i.to([FAB_NS, 'numeric']).value.to_i } }.flatten
66
- end
67
- end
68
58
  end
69
59
  end
@@ -95,18 +95,25 @@ module Fabulator::Template
95
95
  each_form_element do |el|
96
96
  id = el_id(el)
97
97
  next if id == ''
98
- next unless captions.has_key?(id)
98
+ caption = nil
99
+ if captions.is_a?(Hash)
100
+ caption = captions[id]
101
+ else
102
+ caption = captions.traverse_path(id.split('.')).first.to_s
103
+ end
104
+
105
+ next if caption.nil?
99
106
 
100
107
  is_grid = false
101
108
  if el.name == 'grid'
102
109
  else
103
- caption = el.find_first('./caption')
104
- if caption.nil?
105
- el << text_node('caption', captions[id])
110
+ cap = el.find_first('./caption')
111
+ if cap.nil?
112
+ el << text_node('caption', caption)
106
113
  else
107
- caption.content = captions[id]
108
- caption.parent << text_node('caption', captions[id])
109
- caption.remove!
114
+ cap.content = caption
115
+ cap.parent << text_node('caption', caption)
116
+ cap.remove!
110
117
  end
111
118
  end
112
119
  end
@@ -157,7 +157,15 @@ rule
157
157
  | tuple
158
158
  | LITERAL { result = Fabulator::Expr::Literal.new(val[0], [ Fabulator::FAB_NS, 'string' ]) }
159
159
  | NUMBER { result = Fabulator::Expr::Literal.new(val[0] =~ /\./ ? val[0].to_d.to_r : val[0].to_i.to_r, [ Fabulator::FAB_NS, 'numeric' ]) }
160
- | FUNCTION_NAME list { result = Fabulator::Expr::Function.new(@context, val[0], val[1]) }
160
+ | FUNCTION_NAME list {
161
+ result = Fabulator::Expr::Function.new(@context, val[0], val[1])
162
+ # when 'any' : Fabulator::Expr::AnyExpr.new(@context, val[1])
163
+ # when 'all' : Fabulator::Expr::AllExpr.new(@context, val[1])
164
+ # when 'one' : Fabulator::Expr::OneExpr.new(@context, val[1])
165
+ # when 'none': Fabulator::Expr::NoneExpr.new(@context, val[1])
166
+ # else Fabulator::Expr::Function.new(@context, val[0], val[1])
167
+ # end
168
+ }
161
169
 
162
170
  list: LP opt_args RP { result = Fabulator::Expr::List.new(val[1]) }
163
171
 
@@ -207,6 +215,7 @@ end
207
215
  :axis_name => %r{(?:attribute|child|child-or-self|descendant|descendant-or-self|method|self)},
208
216
  :namespace_name => %r{(?:context|global|local|session|universal)},
209
217
  :number => %r{(-?\d+(?:\.\d+)?|\.\d+)},
218
+ :literal => %r{((?:"(?:[^\\"]*(?:\\.[^\\"]*)*)")|(?:'(?:[^\\']*(?:\\.[^\\']*)*)'))},
210
219
  }
211
220
 
212
221
  @@regex[:axis] = %r{(#{@@regex[:ncname]})\s*(?=::)}
@@ -254,7 +263,7 @@ end
254
263
  == & && ||
255
264
  }.split(/\s*/), '[', ',', '$' ].each { |t| @@preceding_tokens[t] = true }
256
265
 
257
- @@regex[:general] = Regexp.compile(%{^(?:#{@@regex[:function_name]}|#{@@regex[:axis]}|#{@@regex[:name_colon_star]}|#{@@regex[:qname]}|('[^']*'|"[^"]*")|#{@@regex[:number]}|#{@@regex[:dollar_qname]}|#{@@regex[:dollar_int]}|(#{@@regex[:simple_tokens]}))})
266
+ @@regex[:general] = Regexp.compile(%{^(?:#{@@regex[:function_name]}|#{@@regex[:axis]}|#{@@regex[:name_colon_star]}|#{@@regex[:qname]}|#{@@regex[:literal]}|#{@@regex[:number]}|#{@@regex[:dollar_qname]}|#{@@regex[:dollar_int]}|(#{@@regex[:simple_tokens]}))})
258
267
 
259
268
  def next_token
260
269
  @token = nil
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fabulator
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - James Smith
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-18 00:00:00 +00:00
18
+ date: 2010-08-27 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency