Class: JSON::LD::API::REXML::NodeProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/json/ld/html/rexml.rb

Overview

Proxy class to implement uniform element accessors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, parent = nil) ⇒ NodeProxy

Returns a new instance of NodeProxy.



23
24
25
26
# File 'lib/json/ld/html/rexml.rb', line 23

def initialize(node, parent = nil)
  @node = node
  @parent = parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Proxy for everything else to @node



128
129
130
# File 'lib/json/ld/html/rexml.rb', line 128

def method_missing(method, *args)
  @node.send(method, *args)
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



20
21
22
# File 'lib/json/ld/html/rexml.rb', line 20

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



21
22
23
# File 'lib/json/ld/html/rexml.rb', line 21

def parent
  @parent
end

Instance Method Details

#ancestorsObject

Ancestors of this element, in order



66
67
68
# File 'lib/json/ld/html/rexml.rb', line 66

def ancestors
  @ancestors ||= parent ? parent.ancestors + [parent] : []
end

#at_xpath(*args) ⇒ Object



122
123
124
# File 'lib/json/ld/html/rexml.rb', line 122

def at_xpath(*args)
  xpath(*args).first
end

#attribute_nodesObject



91
92
93
94
95
96
# File 'lib/json/ld/html/rexml.rb', line 91

def attribute_nodes
  attrs = @node.attributes.dup.keep_if do |name, attr|
    !name.start_with?('xmlns')
  end
  @attribute_nodes ||= (attrs.empty? ? attrs : NodeSetProxy.new(attrs, self))
end

#baseString

Return xml:base on element, if defined

Returns:

  • (String)


32
33
34
# File 'lib/json/ld/html/rexml.rb', line 32

def base
  @node.attribute("base", RDF::XML.to_s) || @node.attribute('xml:base')
end

#blank?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/json/ld/html/rexml.rb', line 110

def blank?
  @node.is_a?(::REXML::Text) && @node.empty?
end

#childrenNodeSetProxy

Children of this node

Returns:



61
62
63
# File 'lib/json/ld/html/rexml.rb', line 61

def children
  NodeSetProxy.new(@node.children, self)
end

#display_pathObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/json/ld/html/rexml.rb', line 36

def display_path
  @display_path ||= begin
    path = []
    path << parent.display_path if parent
    path << @node.name
    case @node
    when ::REXML::Element   then path.join("/")
    when ::REXML::Attribute then path.join("@")
    else path.join("?")
    end
  end
end

#element?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/json/ld/html/rexml.rb', line 106

def element?
  @node.is_a?(::REXML::Element)
end

#inner_htmlString

Inner text of an element



87
88
89
# File 'lib/json/ld/html/rexml.rb', line 87

def inner_html
  @node.children.map(&:to_s).join
end

#inner_textString

Inner text of an element



75
76
77
78
79
80
# File 'lib/json/ld/html/rexml.rb', line 75

def inner_text
  coder = HTMLEntities.new
  ::REXML::XPath.match(@node,'.//text()').map { |e|
    coder.decode(e)
  }.join
end

#text?Boolean

Node type accessors

Returns:

  • (Boolean)


102
103
104
# File 'lib/json/ld/html/rexml.rb', line 102

def text?
  @node.is_a?(::REXML::Text)
end

#text_content?Array<:text, :element, :attribute>

Return true of all child elements are text

Returns:

  • (Array<:text, :element, :attribute>)


53
54
55
# File 'lib/json/ld/html/rexml.rb', line 53

def text_content?
  @node.children.all? {|c| c.is_a?(::REXML::Text)}
end

#to_sObject



114
# File 'lib/json/ld/html/rexml.rb', line 114

def to_s; @node.to_s; end

#xpath(*args) ⇒ Object



116
117
118
119
120
# File 'lib/json/ld/html/rexml.rb', line 116

def xpath(*args)
  ::REXML::XPath.match(@node, *args).map do |n|
    NodeProxy.new(n, parent)
  end
end