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, Charset, EleBody, EleBodyName, EleEnvelope, EleEnvelopeName, EleFault, EleFaultActor, EleFaultActorName, EleFaultCode, EleFaultCodeName, EleFaultDetail, EleFaultDetailName, EleFaultName, EleFaultString, EleFaultStringName, EleHeader, EleHeaderName, EncodingNamespace, EnvelopeNamespace, LiteralNamespace, MediaType, NextActor, PropertyName, RPCRouter, RPCServerException, RPCUtils, SOAPGenerator, SOAPProxy, TypeMap, VERSION, ValueArray, ValueArrayName

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.



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

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.



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

def text
  @text
end

#typeObject

Returns the value of attribute type.



643
644
645
# File 'lib/soap/baseData.rb', line 643

def type
  @type
end

Class Method Details

.decode(elename) ⇒ Object



743
744
745
746
# File 'lib/soap/baseData.rb', line 743

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.


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

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



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

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



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

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



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

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



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

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

#[]=(idx, data) ⇒ Object



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

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.



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

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

#eachObject



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

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

#have_memberObject



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

def have_member
  !@array.empty?
end

#inspectObject



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

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)


700
701
702
# File 'lib/soap/baseData.rb', line 700

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

#membersObject



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

def members
  @array
end

#set(value) ⇒ Object



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

def set(value)
  @text = value
end

#to_objObject



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

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