Class: Fabulator::Expr::Context
- Inherits:
-
Object
- Object
- Fabulator::Expr::Context
- Defined in:
- lib/fabulator/expr/context.rb
Instance Method Summary collapse
- #attribute(ns, attr, popts = { }) ⇒ Object
- #compile_action(e) ⇒ Object
- #compile_actions(xml) ⇒ Object
- #compile_structural(e) ⇒ Object
- #compile_structurals(xml) ⇒ Object
- #each_namespace(&block) ⇒ Object
- #eval_expression(selection) ⇒ Object
- #get_ns(n) ⇒ Object
- #get_select(default = nil) ⇒ Object
- #get_values(ln = nil) ⇒ Object
- #get_var(v) ⇒ Object
- #in_context {|ctx| ... } ⇒ Object
-
#initialize(parent_c = nil, xml = nil) ⇒ Context
constructor
A new instance of Context.
- #last=(l) ⇒ Object
- #last? ⇒ Boolean
- #line_num ⇒ Object
- #merge(s = nil) ⇒ Object
- #merge_data(d, p = nil) ⇒ Object
- #position ⇒ Object
- #position=(p) ⇒ Object
- #root ⇒ Object
- #root=(r) ⇒ Object
- #run(action, autovivify = false) ⇒ Object
- #run_filter(ns, name) ⇒ Object
- #set_ns(n, h) ⇒ Object
- #set_value(p, v) ⇒ Object
- #set_var(v, vv) ⇒ Object
- #traverse_path(path, autovivify = false) ⇒ Object
- #with(ctx2) {|ctx| ... } ⇒ Object
- #with_root(r) ⇒ Object
- #with_roots(items, &block) ⇒ Object
Constructor Details
#initialize(parent_c = nil, xml = nil) ⇒ Context
Returns a new instance of Context.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/fabulator/expr/context.rb', line 5 def initialize(parent_c = nil, xml = nil) @parent = parent_c @run_time_parent = nil @ns = { } @attributes = { } @variables = { } @position = nil @last = nil @line_num = nil if parent_c.nil? if xml.nil? || (xml.root rescue nil).nil? roots = { } roots['data'] = Fabulator::Expr::Node.new('data', roots, nil, []) @root = roots['data'] end end if !xml.nil? if xml.is_a?(self.class) @run_time_parent = xml else @line_num = xml.line_num parser = Fabulator::Expr::Parser.new xml.namespaces.each do |ns| @ns[ns.prefix] = ns.href end begin @ns[''] = xml.namespaces.default.href rescue end xml.each_attr do |attr| v = attr.value if !v.nil? @attributes[attr.ns.href.to_s] ||= {} @attributes[attr.ns.href.to_s][attr.name.to_s] = v end end end end end |
Instance Method Details
#attribute(ns, attr, popts = { }) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/fabulator/expr/context.rb', line 80 def attribute(ns, attr, popts = { }) opts = { :static => !@run_time_parent.nil? && !self.root.nil? }.update(popts) value = nil if @attributes.nil? || @attributes[ns].nil? || @attributes[ns].empty? || @attributes[ns][attr].nil? if opts[:inherited] value = @parent.nil? ? nil : @parent.attribute(ns, attr, opts) end else value = @attributes[ns][attr] end if value.nil? && !opts[:default].nil? value = opts[:default] end if !value.nil? && value.is_a?(String) e = nil if !opts[:eval] if value =~ /^\{(.*)\}$/ e = $1 end else e = value end if !e.nil? p = Fabulator::Expr::Parser.new value = p.parse(e, self) else value = Fabulator::Expr::Literal.new(value, [ FAB_NS, value =~ /^\d+$/ ? 'numeric' : value =~ /^\d*\.\d+$/ || value =~ /^\d+\.\d*$/ ? 'numeric' : 'string' ]) end if opts[:static] value = value.run(self).collect{ |v| v.value } if value.empty? value = nil elsif value.size == 1 value = value.first end end end value end |
#compile_action(e) ⇒ Object
363 364 365 366 367 |
# File 'lib/fabulator/expr/context.rb', line 363 def compile_action(e) ns = e.namespaces.namespace.href return unless Fabulator::TagLib.namespaces.include?(ns) Fabulator::TagLib.namespaces[ns].compile_action(e, self) end |
#compile_actions(xml) ⇒ Object
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/fabulator/expr/context.rb', line 344 def compile_actions(xml) actions = Fabulator::Expr::StatementList.new return actions if xml.nil? local_ctx = self.merge(xml) xml.each_element do |e| ns = e.namespaces.namespace.href next unless Fabulator::TagLib.namespaces.include?(ns) if ns == FAB_NS && e.name == 'ensure' actions.add_ensure(local_ctx.compile_actions(e)) elsif ns == FAB_NS && e.name == 'catch' actions.add_catch(local_ctx.compile_action(e)) else actions.add_statement(local_ctx.compile_action(e)) # rescue nil) end end return actions end |
#compile_structural(e) ⇒ Object
387 388 389 390 391 |
# File 'lib/fabulator/expr/context.rb', line 387 def compile_structural(e) ns = e.namespaces.namespace.href return unless Fabulator::TagLib.namespaces.include?(ns) Fabulator::TagLib.namespaces[ns].compile_structural(e, self) end |
#compile_structurals(xml) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/fabulator/expr/context.rb', line 369 def compile_structurals(xml) #local_ctx = self.merge(xml) structs = { } our_ns = xml.namespaces.namespace.href our_nom = xml.name xml.each_element do |e| ns = e.namespaces.namespace.href nom = e.name.to_sym allowed = (Fabulator::TagLib.namespaces[our_ns].structural_class(our_nom).accepts_structural?(ns, nom) rescue false) next unless allowed structs[ns] ||= { } structs[ns][nom] ||= [ ] structs[ns][nom] << self.compile_structural(e) structs[ns][nom] -= [ nil ] end return structs end |
#each_namespace(&block) ⇒ Object
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/fabulator/expr/context.rb', line 174 def each_namespace(&block) if !@parent.nil? @parent.each_namespace do |k,v| yield k, v end end @ns.each_pair do |k,v| yield k, v end end |
#eval_expression(selection) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/fabulator/expr/context.rb', line 185 def eval_expression(selection) if selection.is_a?(String) p = Fabulator::Expr::Parser.new selection = p.parse(selection, self) end if selection.nil? res = [ ] else # run selection against current context res = selection.run(self) end return res end |
#get_ns(n) ⇒ Object
164 165 166 167 168 |
# File 'lib/fabulator/expr/context.rb', line 164 def get_ns(n) return @ns[n] if @ns.has_key?(n) return @parent.get_ns(n) unless @parent.nil? return nil end |
#get_select(default = nil) ⇒ Object
122 123 124 |
# File 'lib/fabulator/expr/context.rb', line 122 def get_select(default = nil) self.attribute(FAB_NS, 'select', { :eval => true, :static => false, :default => default }) end |
#get_values(ln = nil) ⇒ Object
279 280 281 282 |
# File 'lib/fabulator/expr/context.rb', line 279 def get_values(ln = nil) return [] if ln.nil? self.eval_expression(ln).collect{ |c| c.value} - [ nil ] end |
#get_var(v) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/fabulator/expr/context.rb', line 144 def get_var(v) if !@variables.has_key?(v) if @run_time_parent.nil? if @parent.nil? nil else @parent.get_var(v) end else @run_time_parent.get_var(v) end else @variables[v] end end |
#in_context {|ctx| ... } ⇒ Object
393 394 395 396 |
# File 'lib/fabulator/expr/context.rb', line 393 def in_context(&block) ctx = self.merge yield ctx end |
#last=(l) ⇒ Object
56 57 58 |
# File 'lib/fabulator/expr/context.rb', line 56 def last=(l) @last = !!l end |
#last? ⇒ Boolean
50 51 52 53 54 |
# File 'lib/fabulator/expr/context.rb', line 50 def last? return @last unless @last.nil? return @last if @run_time_parent.nil? return @run_time_parent.last? end |
#line_num ⇒ Object
60 61 62 63 64 |
# File 'lib/fabulator/expr/context.rb', line 60 def line_num return @line_num unless @line_num.nil? return @parent.line_num unless @parent.nil? return 0 end |
#merge(s = nil) ⇒ Object
76 77 78 |
# File 'lib/fabulator/expr/context.rb', line 76 def merge(s = nil) self.class.new(self, s) end |
#merge_data(d, p = nil) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/fabulator/expr/context.rb', line 284 def merge_data(d,p = nil) # we have a hash or array based on root (r) if p.nil? root_context = [ self.root ] else root_context = self.traverse_path(p,true) end if root_context.size > 1 # see if we need to prune new_rc = [ ] root_context.each do |c| if c.children.size == 0 && c.value.nil? c.parent.prune(c) if c.parent else new_rc << c end end if new_rc.size > 0 raise "Unable to merge data into multiple places simultaneously" else root_context = new_rc end else root_context = root_context.first end if d.is_a?(Fabulator::Expr::Node) self.set_value(p, d) elsif d.is_a?(Array) node_name = root_context.name root_context = root_context.parent # get rid of empty children so we don't have problems later root_context.children.each do |c| if c.children.size == 0 && c.name == node_name && c.value.nil? c.parent.prune(c) end end d.each do |i| next if i.nil? if i.is_a?(Array) || i.is_a?(Hash) c = root_context.create_child(node_name) self.with_root(c).merge_data(i) else root_context.create_child(node_name, i) end end elsif d.is_a?(Hash) d.each_pair do |k,v| bits = k.split('.') c = self.with_root(root_context).traverse_path(bits,true).first if v.is_a?(Hash) || v.is_a?(Array) || v.is_a?(Fabulator::Expr::Node) self.with_root(c).merge_data(v) else c.value = v end end else c = root_context.parent.create_child(root_context.name, d) end end |
#position ⇒ Object
66 67 68 69 70 |
# File 'lib/fabulator/expr/context.rb', line 66 def position return @position unless @position.nil? return @position if @run_time_parent.nil? return @run_time_parent.position end |
#position=(p) ⇒ Object
72 73 74 |
# File 'lib/fabulator/expr/context.rb', line 72 def position=(p) @position = p end |
#root ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/fabulator/expr/context.rb', line 132 def root if @root.nil? return @run_time_parent.nil? ? ( @parent.nil? ? nil : @parent.root ) : @run_time_parent.root end @root end |
#root=(r) ⇒ Object
140 141 142 |
# File 'lib/fabulator/expr/context.rb', line 140 def root=(r) @root = r end |
#run(action, autovivify = false) ⇒ Object
200 201 202 |
# File 'lib/fabulator/expr/context.rb', line 200 def run(action, autovivify = false) action.run(self, autovivify) end |
#run_filter(ns, name) ⇒ Object
414 415 416 417 418 |
# File 'lib/fabulator/expr/context.rb', line 414 def run_filter(ns, name) handler = Fabulator::TagLib.namespaces[ns] return [] if handler.nil? handler.run_filter(self, name) end |
#set_ns(n, h) ⇒ Object
170 171 172 |
# File 'lib/fabulator/expr/context.rb', line 170 def set_ns(n,h) @ns[n] = h end |
#set_value(p, v) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/fabulator/expr/context.rb', line 235 def set_value(p, v) if p.is_a?(String) || v.is_a?(String) parser = Fabulator::Expr::Parser.new p = parser.parse(p,self) if p.is_a?(String) v = parser.parse(v,self) if v.is_a?(String) end #return [] if p.nil? p = [ self.root ] if p.nil? p = [ p ] unless p.is_a?(Array) ret = [ ] p.each do |pp| tgts = pp.is_a?(Fabulator::Expr::Node) ? [ pp ] : pp.run(self, true) src = nil if !v.nil? src = v.is_a?(Fabulator::Expr::Node) ? [ v ] : v.run(self) end tgts.each do |tgt| tgt.prune if src.nil? || src.empty? tgt.value = nil ret << tgt elsif src.size == 1 tgt.copy(src.first) ret << tgt else pp = tgt.parent nom = tgt.name pp.prune(pp.children(nom)) src.each do |s| tgt = pp.create_child(nom,nil) tgt.copy(s) ret << tgt end end end end ret end |
#set_var(v, vv) ⇒ Object
160 161 162 |
# File 'lib/fabulator/expr/context.rb', line 160 def set_var(v,vv) @variables[v] = vv end |
#traverse_path(path, autovivify = false) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/fabulator/expr/context.rb', line 204 def traverse_path(path, autovivify = false) return [ self.root ] if path.nil? || path.is_a?(Array) && path.empty? path = [ path ] unless path.is_a?(Array) current = [ self.root ] path.each do |c| set = [ ] current.each do |cc| if c.is_a?(String) cset = cc.children(c) else cset = c.run(self.with_root(cc), autovivify) end if cset.nil? || cset.empty? if autovivify if c.is_a?(String) cset = [ cc.create_child(c) ] else cset = [ c.create_node(cc) ] end end end set = set + cset end current = set end return current end |
#with(ctx2) {|ctx| ... } ⇒ Object
398 399 400 401 |
# File 'lib/fabulator/expr/context.rb', line 398 def with(ctx2, &block) ctx = self.merge(ctx2) yield ctx end |
#with_root(r) ⇒ Object
126 127 128 129 130 |
# File 'lib/fabulator/expr/context.rb', line 126 def with_root(r) ctx = self.class.new(self) ctx.root = r ctx end |
#with_roots(items, &block) ⇒ Object
403 404 405 406 407 408 409 410 411 412 |
# File 'lib/fabulator/expr/context.rb', line 403 def with_roots(items, &block) idx = 1 items.each do |i| ctx = self.with_root(i) ctx.position = idx ctx.last = idx == items.size yield ctx idx += 1 end end |