Class: Handsoap::XmlMason::Element
- Defined in:
- lib/handsoap/xml_mason.rb
Instance Method Summary collapse
- #append_child(node) ⇒ Object
- #defines_namespace?(prefix) ⇒ Boolean
- #document ⇒ Object
- #find(name) ⇒ Object
- #find_all(name) ⇒ Object
- #full_name ⇒ Object
- #get_namespace(prefix) ⇒ Object
-
#initialize(parent, prefix, node_name, value = nil, flags = []) ⇒ Element
constructor
A new instance of Element.
- #set_attr(name, value) ⇒ Object
- #set_value(value, *flags) ⇒ Object
- #to_s(indentation = '') ⇒ Object
- #value_node? ⇒ Boolean
Methods inherited from Node
Constructor Details
#initialize(parent, prefix, node_name, value = nil, flags = []) ⇒ Element
Returns a new instance of Element.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/handsoap/xml_mason.rb', line 88 def initialize(parent, prefix, node_name, value = nil, flags = []) super() # if prefix.to_s == "" # raise "missing prefix" # end @parent = parent @prefix = prefix @node_name = node_name @children = [] @attributes = {} if not value.nil? set_value value.to_s, *flags end if block_given? yield self end end |
Instance Method Details
#append_child(node) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/handsoap/xml_mason.rb', line 111 def append_child(node) if value_node? raise "Element already has a text value. Can't add nodes" end @children << node return node end |
#defines_namespace?(prefix) ⇒ Boolean
166 167 168 |
# File 'lib/handsoap/xml_mason.rb', line 166 def defines_namespace?(prefix) @attributes.keys.include?("xmlns:#{prefix}") || @parent.defines_namespace?(prefix) end |
#document ⇒ Object
105 106 107 |
# File 'lib/handsoap/xml_mason.rb', line 105 def document @parent.respond_to?(:document) ? @parent.document : @parent end |
#find(name) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/handsoap/xml_mason.rb', line 132 def find(name) name = name.to_s if name.kind_of? Symbol if @node_name == name || full_name == name return self end @children.each do |node| if node.respond_to? :find tmp = node.find(name) if tmp return tmp end end end return nil end |
#find_all(name) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/handsoap/xml_mason.rb', line 147 def find_all(name) name = name.to_s if name.kind_of? Symbol result = [] if @node_name == name || full_name == name result << self end @children.each do |node| if node.respond_to? :find result = result.concat(node.find_all(name)) end end return result end |
#full_name ⇒ Object
108 109 110 |
# File 'lib/handsoap/xml_mason.rb', line 108 def full_name @prefix.nil? ? @node_name : (@prefix + ":" + @node_name) end |
#get_namespace(prefix) ⇒ Object
163 164 165 |
# File 'lib/handsoap/xml_mason.rb', line 163 def get_namespace(prefix) @namespaces[prefix] || @parent.get_namespace(prefix) end |
#set_attr(name, value) ⇒ Object
128 129 130 131 |
# File 'lib/handsoap/xml_mason.rb', line 128 def set_attr(name, value) full_name = parse_ns(name).join(":") @attributes[name] = value end |
#set_value(value, *flags) ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/handsoap/xml_mason.rb', line 118 def set_value(value, *flags) if @children.length > 0 raise "Element already has children. Can't set value" end if flags && flags.include?(:raw) @children = [RawContent.new(value)] else @children = [TextNode.new(value)] end end |
#to_s(indentation = '') ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/handsoap/xml_mason.rb', line 169 def to_s(indentation = '') # todo resolve attribute prefixes aswell if @prefix && (not defines_namespace?(@prefix)) set_attr "xmlns:#{@prefix}", get_namespace(@prefix) end name = XmlMason.html_escape(full_name) attr = (@attributes.any? ? (" " + @attributes.map { |key, value| XmlMason.html_escape(key) + '="' + XmlMason.html_escape(value) + '"' }.join(" ")) : "") if @children.any? if value_node? children = @children[0].to_s(indentation + " ") else children = @children.map { |node| "\n" + node.to_s(indentation + " ") }.join("") + "\n" + indentation end indentation + "<" + name + attr + ">" + children + "</" + name + ">" else indentation + "<" + name + attr + " />" end end |
#value_node? ⇒ Boolean
160 161 162 |
# File 'lib/handsoap/xml_mason.rb', line 160 def value_node? @children.length == 1 && @children[0].kind_of?(TextNode) end |