Class: SOAP::SOAPElement

Inherits:
Object show all
Includes:
Enumerable, SOAPCompoundtype
Defined in:
lib/soap/baseData.rb

Overview

SOAPElement is not typed so it is not derived from NSDBase.

Direct Known Subclasses

SOAPRawData

Constant Summary

Constants included from SOAP

AttrActor, AttrActorName, AttrArrayType, AttrArrayTypeName, AttrEncodingStyle, AttrEncodingStyleName, AttrMustUnderstand, AttrMustUnderstandName, AttrOffset, AttrOffsetName, AttrPosition, AttrPositionName, AttrRoot, AttrRootName, Base64Literal, Charset, EleBody, EleBodyName, EleEnvelope, EleEnvelopeName, EleFault, EleFaultActor, EleFaultActorName, EleFaultCode, EleFaultCodeName, EleFaultDetail, EleFaultDetailName, EleFaultName, EleFaultString, EleFaultStringName, EleHeader, EleHeaderName, EncodingNamespace, EnvelopeNamespace, LiteralNamespace, MediaType, NS, NextActor, PropertyName, RPCRouter, RPCServerException, RPCUtils, SOAPNamespaceTag, SOAPProxy, TypeMap, VERSION, ValueArray, ValueArrayName, XSDNamespaceTag, XSINamespaceTag

Instance Attribute Summary collapse

Attributes included from SOAPCompoundtype

#qualified

Attributes included from SOAPType

#definedtype, #elename, #encodingstyle, #extraattr, #id, #parent, #position, #precedents, #root

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SOAPType

#rootnode

Methods included from Enumerable

#inject

Constructor Details

#initialize(elename, text = nil) ⇒ SOAPElement

Returns a new instance of SOAPElement.


581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/soap/baseData.rb', line 581

def initialize(elename, text = nil)
  super()
  if !elename.is_a?(XSD::QName)
    elename = XSD::QName.new(nil, elename)
  end
  @encodingstyle = LiteralNamespace
  @elename = elename
  @type = nil

  @array = []
  @data = []
  @text = text
end

Instance Attribute Details

#textObject Also known as: data

Text interface.


578
579
580
# File 'lib/soap/baseData.rb', line 578

def text
  @text
end

#typeObject

Returns the value of attribute type


576
577
578
# File 'lib/soap/baseData.rb', line 576

def type
  @type
end

Class Method Details

.decode(elename) ⇒ Object


670
671
672
673
# File 'lib/soap/baseData.rb', line 670

def self.decode(elename)
  o = SOAPElement.new(elename)
  o
end

.from_obj(obj, namespace = nil) ⇒ Object

when obj is a Hash or an Array:

when key starts with "xmlattr_":
  value is added as an XML attribute with the key name however the
  "xmlattr_" is dropped from the name.
when key starts with "xmlele_":
  value is added as an XML element with the key name however the
  "xmlele_" is dropped from the name.
if else:
  value is added as an XML element with the key name.

697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# File 'lib/soap/baseData.rb', line 697

def self.from_obj(obj, namespace = nil)
  o = SOAPElement.new(nil)
  case obj
  when nil
    o.text = nil
  when Hash, Array
    obj.each do |name, value|
      addname, is_attr = parse_name(name, namespace)
      if value.is_a?(Array)
        value.each do |subvalue|
          if is_attr
            o.extraattr[addname] = subvalue
          else
            child = from_obj(subvalue, namespace)
            child.elename = addname
            o.add(child)
          end
        end
      else
        if is_attr
          o.extraattr[addname] = value
        else
          child = from_obj(value, namespace)
          child.elename = addname
          o.add(child)
        end
      end
    end
  else
    o.text = obj.to_s
  end
  o
end

.from_objs(objs) ⇒ Object


675
676
677
678
679
680
681
682
683
684
685
686
# File 'lib/soap/baseData.rb', line 675

def self.from_objs(objs)
  objs.collect { |value|
    if value.is_a?(SOAPElement)
      value
    else
      k, v = value
      ele = from_obj(v)
      ele.elename = XSD::QName.new(nil, k)
      ele
    end
  }
end

.parse_name(obj, namespace = nil) ⇒ Object


731
732
733
734
735
736
737
738
739
740
741
742
# File 'lib/soap/baseData.rb', line 731

def self.parse_name(obj, namespace = nil)
  qname = to_qname(obj, namespace)
  if /\Axmlele_/ =~ qname.name
    qname = XSD::QName.new(qname.namespace, qname.name.sub(/\Axmlele_/, ''))
    return qname, false
  elsif /\Axmlattr_/ =~ qname.name
    qname = XSD::QName.new(qname.namespace, qname.name.sub(/\Axmlattr_/, ''))
    return qname, true
  else
    return qname, false
  end
end

.to_qname(obj, namespace = nil) ⇒ Object


744
745
746
747
748
749
750
751
752
# File 'lib/soap/baseData.rb', line 744

def self.to_qname(obj, namespace = nil)
  if obj.is_a?(XSD::QName)
    obj
  elsif /\A(.+):([^:]+)\z/ =~ obj.to_s
    XSD::QName.new($1, $2)
  else
    XSD::QName.new(namespace, obj.to_s)
  end
end

Instance Method Details

#[](idx) ⇒ Object


610
611
612
613
614
615
616
# File 'lib/soap/baseData.rb', line 610

def [](idx)
  if @array.include?(idx)
    @data[@array.index(idx)]
  else
    nil
  end
end

#[]=(idx, data) ⇒ Object


618
619
620
621
622
623
624
625
# File 'lib/soap/baseData.rb', line 618

def []=(idx, data)
  if @array.include?(idx)
    data.parent = self if data.respond_to?(:parent=)
    @data[@array.index(idx)] = data
  else
    add(data)
  end
end

#add(value) ⇒ Object

Element interfaces.


606
607
608
# File 'lib/soap/baseData.rb', line 606

def add(value)
  add_member(value.elename.name, value)
end

#eachObject


662
663
664
665
666
667
668
# File 'lib/soap/baseData.rb', line 662

def each
  idx = 0
  while idx < @array.length
    yield(@array[idx], @data[idx])
    idx += 1
  end
end

#have_memberObject


635
636
637
# File 'lib/soap/baseData.rb', line 635

def have_member
  !@array.empty?
end

#inspectObject


595
596
597
598
599
# File 'lib/soap/baseData.rb', line 595

def inspect
  sprintf("#<%s:0x%x %s>", self.class.name, __id__, self.elename) +
    (@text ? " #{@text.inspect}" : '') +
    @data.collect { |ele| "\n#{ele.inspect}" }.join.gsub(/^/, '  ')
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)

627
628
629
# File 'lib/soap/baseData.rb', line 627

def key?(name)
  @array.include?(name)
end

#membersObject


631
632
633
# File 'lib/soap/baseData.rb', line 631

def members
  @array
end

#set(value) ⇒ Object


601
602
603
# File 'lib/soap/baseData.rb', line 601

def set(value)
  @text = value
end

#to_objObject


639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/soap/baseData.rb', line 639

def to_obj
  if members.empty?
    @text
  else
    hash = {}
    proptype = {}
    each do |k, v|
      value = v.respond_to?(:to_obj) ? v.to_obj : v.to_s
      case proptype[k]
      when :single
        hash[k] = [hash[k], value]
        proptype[k] = :multi
      when :multi
        hash[k] << value
      else
        hash[k] = value
        proptype[k] = :single
      end
    end
    hash
  end
end