class Rubyvis::Dom

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

@class Represets a DOM operator for the specified map. This allows easy 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 {@link pv.Dom.Node}.

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

@param map a map from which to construct a DOM.

Public Class Methods

Node(value) click to toggle source
# File lib/rubyvis/dom.rb, line 69
def self.Node(value)
  Node.new(value)
end
new(map) click to toggle source
# File lib/rubyvis/dom.rb, line 28
def initialize(map)
  @_map=map
  @leaf=lambda {|n| !n.respond_to? :each }
end

Public Instance Methods

leaf(f=nil) click to toggle source

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.

@param {function} f the new leaf function. @returns the current leaf function, or this.

# File lib/rubyvis/dom.rb, line 39
def leaf(f=nil)
  if !f.nil?
    @leaf=f
    self
  end
  @leaf
end
nodes() click to toggle source

Applies the DOM operator, returning the array of all nodes in preorder traversal. @returns {array} the array of nodes in preorder traversal.

# File lib/rubyvis/dom.rb, line 65
def nodes
  self.root.nodes
end
root(node_name=nil) click to toggle source

Applies the DOM operator, returning the root node.

# 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) click to toggle source
# 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