Module: Flutterby::Node::Tree

Included in:
Flutterby::Node
Defined in:
lib/flutterby/node.rb

Instance Method Summary collapse

Instance Method Details

#create(name, **args) ⇒ Object

Creates a new node, using the specified arguments, as a child of this node.



119
120
121
122
# File 'lib/flutterby/node.rb', line 119

def create(name, **args)
  args[:parent] = self
  Node.new(name.to_s, **args)
end

#emit_child(name) ⇒ Object



85
86
87
# File 'lib/flutterby/node.rb', line 85

def emit_child(name)
  # Override this to dynamically create child nodes.
end

#find(path, opts = {}) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/flutterby/node.rb', line 131

def find(path, opts = {})
  path = path.to_s
  return self if path.empty?

  # remove duplicate slashes
  path = path.gsub(%r{/+}, "/")

  case path
  # ./foo/...
  when %r{^\./?} then
    parent ? parent.find($', opts) : root.find($', opts)

  # /foo/...
  when %r{^/} then
    root.find($', opts)

  # foo/...
  when %r{^([^/]+)/?} then
    # Use the next path part to find a child by that name.
    # If no child can't be found, try to emit a child, but
    # not if the requested name starts with an underscore.
    if child = find_child($1, opts) || (emit_child($1) unless $1.start_with?("_"))
      # Depending on the tail of the requested find expression,
      # either return the found node, or ask it to find the tail.
      $'.empty? ? child : child.find($', opts)
    end
  end
end

#find!(path, *args) ⇒ Object

Like #find, but raises an exception when the specified node could not be found.



127
128
129
# File 'lib/flutterby/node.rb', line 127

def find!(path, *args)
  find(path, *args) || raise("Could not find node for path expression '#{path}'")
end

#find_child(name, opts = {}) ⇒ Object

Among this node’s children, find a node by its name. If the name passed as an argument includes a dot, the name will match against the full name of the children; otherwise, just the base name.

Examples:

# returns the first child called "index"
find_child("index")

# returns the child called "index" with extension "html"
find_child("index.html")


76
77
78
79
80
81
82
83
# File 'lib/flutterby/node.rb', line 76

def find_child(name, opts = {})
  name_attr = name.include?(".") ? "full_name" : "name"

  @children.find do |c|
    (c.should_publish? || !opts[:public_only]) &&
      (c.send(name_attr) == name)
  end
end

#pagesObject

Returns all children that will compile to a HTML page.



112
113
114
# File 'lib/flutterby/node.rb', line 112

def pages
  children.select { |c| c.page? }
end

#parent=(new_parent) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/flutterby/node.rb', line 95

def parent=(new_parent)
  # Remove from previous parent
  if @parent
    @parent.children.delete(self)
  end

  # Assign new parent (it may be nil)
  @parent = new_parent

  # Notify new parent
  if @parent
    @parent.children << self
  end
end

#rootObject

Returns the tree’s root node.



43
44
45
# File 'lib/flutterby/node.rb', line 43

def root
  parent ? parent.root : self
end

#root?Boolean

Returns true if this node is also the tree’s root node.

Returns:

  • (Boolean)


49
50
51
# File 'lib/flutterby/node.rb', line 49

def root?
  root == self
end

#sibling(name) ⇒ Object



53
54
55
# File 'lib/flutterby/node.rb', line 53

def sibling(name)
  parent && parent.find(name)
end

#siblingsObject

Returns this node’s siblings (ie. other nodes within the same folder node.)



60
61
62
# File 'lib/flutterby/node.rb', line 60

def siblings
  parent && (parent.children - [self])
end

#tree_sizeObject



89
90
91
92
93
# File 'lib/flutterby/node.rb', line 89

def tree_size
  children.inject(children.length) do |count, child|
    count + child.tree_size
  end
end