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


873
874
875
876
877
878
879
880
881
# File 'lib/www/delicious.rb', line 873

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)


901
902
903
904
905
906
907
908
# File 'lib/www/delicious.rb', line 901

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.



888
889
890
891
892
893
894
# File 'lib/www/delicious.rb', line 888

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