Class: Swordfish::Node::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/swordfish/nodes/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Initialize with a blank stylesheet and no children



12
13
14
15
# File 'lib/swordfish/nodes/base.rb', line 12

def initialize
  @style = Swordfish::Stylesheet.new []
  @children = []
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



8
9
10
# File 'lib/swordfish/nodes/base.rb', line 8

def children
  @children
end

#contentObject

Returns the value of attribute content.



7
8
9
# File 'lib/swordfish/nodes/base.rb', line 7

def content
  @content
end

#styleObject

Returns the value of attribute style.



9
10
11
# File 'lib/swordfish/nodes/base.rb', line 9

def style
  @style
end

Instance Method Details

#append(node) ⇒ Object

Append a node or nodes to this node as a child



18
19
20
21
22
# File 'lib/swordfish/nodes/base.rb', line 18

def append(node)
  @children ||= []
  @children << node
  @children.flatten!
end

#clear_childrenObject

Delete all child nodes



56
57
58
# File 'lib/swordfish/nodes/base.rb', line 56

def clear_children
  @children = []
end

#find_nodes_by_type(klass) ⇒ Object

Find all descendant nodes of a given type



72
73
74
75
76
# 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) ⇒ Object

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



49
50
51
52
53
# 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) ⇒ Object

Replace a child node at a given index



25
26
27
# File 'lib/swordfish/nodes/base.rb', line 25

def replace(node, idx)
  @children[idx] = node
end

#replace_with(klass) ⇒ Object

Return a clone of this node with a different class



79
80
81
82
83
84
85
# 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) ⇒ Object

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



30
31
32
33
34
35
36
37
38
39
40
# 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_htmlObject

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

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/swordfish/nodes/base.rb', line 43

def to_html
  raise NotImplementedError
end

#wrap_children(child_class, wrapper_class) ⇒ Object

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



61
62
63
64
65
66
67
68
69
# 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