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, SOAPProxy, TypeMap, VERSION, ValueArray, ValueArrayName

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

Methods included from Enumerable

#inject

Constructor Details

#initialize(elename, text = nil) ⇒ SOAPElement

Returns a new instance of SOAPElement


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

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.


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

def text
  @text
end

#typeObject

Returns the value of attribute type


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

def type
  @type
end

Class Method Details

.decode(elename) ⇒ Object


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

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.

772
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
# File 'lib/soap/baseData.rb', line 772

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


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

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


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

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


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

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


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

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

#[]=(idx, data) ⇒ Object


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

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.


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

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

#eachObject


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

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

#have_memberObject


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

def have_member
  !@array.empty?
end

#inspectObject


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

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)

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

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

#membersObject


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

def members
  @array
end

#set(value) ⇒ Object


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

def set(value)
  @text = value
end

#to_objObject


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

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