class Swordfish::Node::Base

Attributes

children[RW]
content[RW]
style[RW]

Public Class Methods

new() click to toggle source

Initialize with a blank stylesheet and no children

# File lib/swordfish/nodes/base.rb, line 12
def initialize
  @style = Swordfish::Stylesheet.new []
  @children = []
end

Public Instance Methods

append(node) click to toggle source

Append a node or nodes to this node as a child

# File lib/swordfish/nodes/base.rb, line 18
def append(node)
  @children ||= []
  @children << node
  @children.flatten!
end
clear_children() click to toggle source

Delete all child nodes

# File lib/swordfish/nodes/base.rb, line 56
def clear_children
  @children = []
end
find_nodes_by_type(klass) click to toggle source

Find all descendant nodes of a given type

# File lib/swordfish/nodes/base.rb, line 72
def find_nodes_by_type(klass)
  nodes = @children.collect{|n| n.find_nodes_by_type(klass)}.flatten
  nodes << self if self.is_a?(klass)
  nodes.compact
end
inform!(hash) click to toggle source

Given a hash, create instance variables for each key in that hash. This is used for communication between nodes in the hierarchy.

# File lib/swordfish/nodes/base.rb, line 49
def inform!(hash)
  hash.each do |k, v|
    instance_variable_set "@#{k}", v
  end
end
replace(node, idx) click to toggle source

Replace a child node at a given index

# File lib/swordfish/nodes/base.rb, line 25
def replace(node, idx)
  @children[idx] = node
end
replace_with(klass) click to toggle source

Return a clone of this node with a different class

# File lib/swordfish/nodes/base.rb, line 79
def replace_with(klass)
  if klass <= Swordfish::Node::Base
    new_node = klass.new
    new_node.inform!({:style => @style, :children => @children, :content => @content })
    new_node
  end
end
stylize(styles) click to toggle source

Take a style or styles and add them to this node's stylesheet

# File lib/swordfish/nodes/base.rb, line 30
def stylize(styles)
  if styles.is_a? Hash
    # Key/value pairs
    styles.each do |k, v|
      @style.send "#{k}=".to_sym, v
    end
  else
    # Boolean values
    @style.merge styles
  end
end
to_html() click to toggle source

Every subclass must implement to_html in order to be converted to HTML

# File lib/swordfish/nodes/base.rb, line 43
def to_html
  raise NotImplementedError
end
wrap_children(child_class, wrapper_class) click to toggle source

Wrap all children of type child_class with a new node of type wrapper_class

# File lib/swordfish/nodes/base.rb, line 61
def wrap_children(child_class, wrapper_class)
  new_node = wrapper_class.new
  new_node.append @children.select{|n| n.is_a? child_class}
  unless new_node.children.empty?
    idx = @children.find_index(new_node.children[0])
    @children = @children - new_node.children
    @children.insert idx, new_node
  end
end