Class: Rubyvis::Dom

Inherits:
Object show all
Defined in:
lib/rubyvis/dom.rb

Overview

Constructs a DOM operator for the specified map. This constructor should not be invoked directly; use pv.dom instead.

transformation of a hierarchical JavaScript object (such as a JSON map) to a W3C Document Object Model hierarchy. For more information on which attributes and methods from the specification are supported, see pv.Dom.Node.

<p>Leaves in the map are determined using an associated leaf function; see #leaf. By default, leaves are any value whose type is not “object”, such as numbers or strings.

Defined Under Namespace

Classes: Node

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map) ⇒ Dom

Returns a new instance of Dom.



28
29
30
31
# File 'lib/rubyvis/dom.rb', line 28

def initialize(map)
  @_map=map
  @leaf=lambda {|n| !n.respond_to? :each }
end

Class Method Details

.Node(value) ⇒ Object



69
70
71
# File 'lib/rubyvis/dom.rb', line 69

def self.Node(value)
  Node.new(value)
end

Instance Method Details

#leaf(f = nil) ⇒ Object

Sets or gets the leaf function for this DOM operator. The leaf function identifies which values in the map are leaves, and which are internal nodes. By default, objects are considered internal nodes, and primitives (such as numbers and strings) are considered leaves.

Parameters:

  • f (function) (defaults to: nil)

    the new leaf function.



39
40
41
42
43
44
45
# File 'lib/rubyvis/dom.rb', line 39

def leaf(f=nil)
  if !f.nil?
    @leaf=f
    self
  end
  @leaf
end

#nodesObject

Applies the DOM operator, returning the array of all nodes in preorder traversal.



65
66
67
# File 'lib/rubyvis/dom.rb', line 65

def nodes
  self.root.nodes
end

#root(node_name = nil) ⇒ Object

Applies the DOM operator, returning the root node.



56
57
58
59
60
# File 'lib/rubyvis/dom.rb', line 56

def root(node_name=nil)
  root=root_recurse(@_map)
  root.node_name=node_name
  root
end

#root_recurse(map) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/rubyvis/dom.rb', line 47

def root_recurse(map)
  n = Rubyvis::Dom::Node.new
  map.each {|k,v|
    n.append_child(@leaf.call(v) ? Rubyvis::Dom::Node.new(v) : root_recurse(v)).node_name = k
  }
  return n
end