Class: XML::Mapping::ChoiceNode

Inherits:
Node
  • Object
show all
Defined in:
lib/xml/mapping/standard_nodes.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ChoiceNode

Returns a new instance of ChoiceNode.

Raises:



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/xml/mapping/standard_nodes.rb', line 371

def initialize(*args)
  args = super(*args)
  @choices = []
  path=nil
  args.each do |arg|
    next if [:if,:then,:elsif].include? arg
    if path.nil?
      path = (if [:else,:default,:otherwise].include? arg
                :else
              else
                XML::XXPath.new arg
              end)
    else
      raise XML::MappingError, "node expected, found: #{arg.inspect}" unless Node===arg
      @choices << [path,arg]

      # undo what the node factory fcn did -- ugly ugly!  would
      # need some way to lazy-evaluate arg (a proc would be
      # simple but ugly for the user), and then use some
      # mechanism (a flag with dynamic scope probably) to tell
      # the node factory fcn not to add the node to the
      # xml_mapping_nodes
      @owner.xml_mapping_nodes(:mapping=>@mapping).delete arg 
      path=nil
    end
  end

  raise XML::MappingError, "node missing at end of argument list" unless path.nil?
  raise XML::MappingError, "no choices were supplied" if @choices.empty?
  
  []
end

Instance Method Details

#is_present_in?(obj) ⇒ Boolean

(overridden) true if at least one of our nodes is_present_in? obj.

Returns:



432
433
434
435
# File 'lib/xml/mapping/standard_nodes.rb', line 432

def is_present_in? obj
  # TODO: use Enumerable#any?
  @choices.inject(false){|prev,(path,node)| prev or node.is_present_in?(obj)}
end

#obj_initializing(obj, mapping) ⇒ Object



426
427
428
# File 'lib/xml/mapping/standard_nodes.rb', line 426

def obj_initializing(obj,mapping)
  @choices[0][1].obj_initializing(obj,mapping)
end

#obj_to_xml(obj, xml) ⇒ Object

Raises:



414
415
416
417
418
419
420
421
422
423
424
# File 'lib/xml/mapping/standard_nodes.rb', line 414

def obj_to_xml(obj,xml)
  @choices.each do |path,node|
    if node.is_present_in? obj
      node.obj_to_xml(obj,xml)
      path.first(xml, :ensure_created=>true)
      return true
    end
  end
  # @choices[0][1].obj_to_xml(obj,xml)
  raise XML::MappingError, "obj_to_xml: no choice present in object: #{obj.inspect}"
end

#xml_to_obj(obj, xml) ⇒ Object

Raises:



404
405
406
407
408
409
410
411
412
# File 'lib/xml/mapping/standard_nodes.rb', line 404

def xml_to_obj(obj,xml)
  @choices.each do |path,node|
    if path==:else or not(path.all(xml).empty?)
      node.xml_to_obj(obj,xml)
      return true
    end
  end
  raise XML::MappingError, "xml_to_obj: no choice matched in: #{xml}"
end