Module: WWW::Delicious::XMLUtils

Included in:
REXML::Element
Defined in:
lib/www/delicious.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#if_attribute_value(xmlattr, &block) ⇒ Object

Returns the xmlattr attribute value for current REXML::Element.

If block is given and attribute value is not nil, the content of the block is executed.

Examples

dom = REXML::Document.new('<a name="1"><b>foo</b><b>bar</b></a>')

dom.root.if_attribute_value(:name)
# => "1"

dom.root.if_attribute_value(:name) { |v| v.to_i }
# => 1

dom.root.if_attribute_value(:foo)
# => nil

dom.root.if_attribute_value(:name) { |v| v.to_i }
# => nil


882
883
884
885
886
887
888
889
890
# File 'lib/www/delicious.rb', line 882

def if_attribute_value(xmlattr, &block) #:nodoc:
  value = if attr = self.attribute(xmlattr.to_s)
      attr.value
    else
      nil
    end
  value = yield value if !value.nil? and block_given?
  value
end

#if_element(expression, &block) ⇒ Object

Executes the content of block on expression child of this element, if it exists. Returns the result or nil if xmlelement doesn’t exist.

Raises:

  • (LocalJumpError)


910
911
912
913
914
915
916
917
# File 'lib/www/delicious.rb', line 910

def if_element(expression, &block)
  raise LocalJumpError, "no block given" unless block_given?
  if element = self.elements[expression.to_s]
    yield element
  else
    nil
  end
end

#if_element_value(expression, &block) ⇒ Object

Returns the value of expression child of this element, if it exists. If blog is given, block is called on expression element value and the result is returned.



897
898
899
900
901
902
903
# File 'lib/www/delicious.rb', line 897

def if_element_value(expression, &block)
  if_element(expression) do |element|
    value = element.text
    value = yield value if block_given?
    value
  end
end