Class: SOAP::SOAPElement

Inherits:
Object show all
Includes:
Enumerable, SOAPCompoundtype, SOAPNameAccessible
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, AttrHref, AttrHrefName, AttrId, AttrIdName, AttrMustUnderstand, AttrMustUnderstandName, AttrOffset, AttrOffsetName, AttrPosition, AttrPositionName, AttrRoot, AttrRootName, Base64Literal, EleBody, EleBodyName, EleEnvelope, EleEnvelopeName, EleFault, EleFaultActor, EleFaultActorName, EleFaultCode, EleFaultCodeName, EleFaultDetail, EleFaultDetailName, EleFaultName, EleFaultString, EleFaultStringName, EleHeader, EleHeaderName, EncodingNamespace, EnvelopeNamespace, LiteralNamespace, MediaType, NextActor, PropertyName, SOAPNamespaceTag, SOAPProxy, TypeMap, ValueArray, ValueArrayName, Version, XSDNamespaceTag, XSINamespaceTag

Instance Attribute Summary collapse

Attributes included from SOAPCompoundtype

#qualified

Attributes included from SOAPType

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SOAPType

#rootnode

Constructor Details

#initialize(elename, text = nil) ⇒ SOAPElement

Returns a new instance of SOAPElement.



651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
# File 'lib/soap/baseData.rb', line 651

def initialize(elename, text = nil)
  super()
  if elename.nil?
    elename = XSD::QName::EMPTY
  else
    elename = Mapping.to_qname(elename)
  end
  @encodingstyle = LiteralNamespace
  @elename = elename
  @type = nil

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

Instance Attribute Details

#textObject Also known as: data

Text interface.



648
649
650
# File 'lib/soap/baseData.rb', line 648

def text
  @text
end

#typeObject

Returns the value of attribute type.



646
647
648
# File 'lib/soap/baseData.rb', line 646

def type
  @type
end

Class Method Details

.decode(elename) ⇒ Object



746
747
748
749
# File 'lib/soap/baseData.rb', line 746

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.


773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/soap/baseData.rb', line 773

def self.from_obj(obj, namespace = nil)
  return obj if obj.is_a?(SOAPElement)
  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



751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/soap/baseData.rb', line 751

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



808
809
810
811
812
813
814
815
816
817
818
819
# File 'lib/soap/baseData.rb', line 808

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



821
822
823
824
825
826
827
828
829
# File 'lib/soap/baseData.rb', line 821

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



686
687
688
689
690
691
692
# File 'lib/soap/baseData.rb', line 686

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

#[]=(idx, data) ⇒ Object



694
695
696
697
698
699
700
701
# File 'lib/soap/baseData.rb', line 694

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.



678
679
680
681
682
683
684
# File 'lib/soap/baseData.rb', line 678

def add(value)
  name = value.elename.name
  @array.push(name)
  @data.push(value)
  value.parent = self if value.respond_to?(:parent=)
  value
end

#eachObject



738
739
740
741
742
743
744
# File 'lib/soap/baseData.rb', line 738

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

#have_memberObject



711
712
713
# File 'lib/soap/baseData.rb', line 711

def have_member
  !@array.empty?
end

#inspectObject



667
668
669
670
671
# File 'lib/soap/baseData.rb', line 667

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)


703
704
705
# File 'lib/soap/baseData.rb', line 703

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

#membersObject



707
708
709
# File 'lib/soap/baseData.rb', line 707

def members
  @array
end

#set(value) ⇒ Object



673
674
675
# File 'lib/soap/baseData.rb', line 673

def set(value)
  @text = value
end

#to_objObject



715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
# File 'lib/soap/baseData.rb', line 715

def to_obj
  if !have_member
    @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