class XML::Mapping::ChoiceNode

Public Class Methods

new(*args) click to toggle source
Calls superclass method XML::Mapping::Node.new
# 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

Public Instance Methods

is_present_in?(obj) click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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