Class: Clerq::Entities::Node

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/clerq/entities/node.rb

Overview

The basic block of hierarchy and the single Clerq entity

Usage

n = Node.new(title: 'Part I')
n << Node.new(title: 'Item 1')
n << Node.new(title: 'Item 2')
n.each{|i| puts i.tile} # and all power of Enumerable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: '', title: '', body: '', meta: {}) ⇒ Node

Returns a new instance of Node.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/clerq/entities/node.rb', line 42

def initialize(id: '', title: '', body: '', meta: {})
  raise ArgumentError, "Invalid argument :id" unless id.is_a? String
  raise ArgumentError, "Invalid argument :title" unless title.is_a? String
  raise ArgumentError, "Invalid argument :body" unless body.is_a? String
  raise ArgumentError, "Invalid argument :meta" unless meta.is_a? Hash
  id = meta.delete(:id) if id.empty? && meta[:id]
  meta.delete(:id) unless id.empty?
  @parent = nil
  @items = []
  @id = id
  @title = title
  @body = body
  @meta = meta
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



27
28
29
# File 'lib/clerq/entities/node.rb', line 27

def body
  @body
end

#idString

When the node id starts with ‘.’, the method will

prefix the node id with parent id

Returns:

  • (String)

    the id of the node



122
123
124
# File 'lib/clerq/entities/node.rb', line 122

def id
  @id.start_with?('.') && @parent ? @parent.id + @id : @id
end

#metaObject (readonly)

Returns the value of attribute meta.



31
32
33
# File 'lib/clerq/entities/node.rb', line 31

def meta
  @meta
end

#parentObject

Returns the value of attribute parent.



20
21
22
# File 'lib/clerq/entities/node.rb', line 20

def parent
  @parent
end

#titleObject (readonly)

Returns the value of attribute title.



23
24
25
# File 'lib/clerq/entities/node.rb', line 23

def title
  @title
end

Instance Method Details

#<<(node) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
61
62
# File 'lib/clerq/entities/node.rb', line 57

def <<(node)
  raise ArgumentError, "Invalid argument :node" unless node.is_a? Node
  node.parent = self
  @items << node
  node
end

#each {|_self| ... } ⇒ Object

see Enumerable#each

Yields:

  • (_self)

Yield Parameters:



95
96
97
98
99
# File 'lib/clerq/entities/node.rb', line 95

def each(&block)
  return to_enum(__callee__) unless block_given?
  yield(self)
  items.each{|n| n.each(&block) }
end

#item(id) ⇒ Node?

Returns child node by provided id; when id not found it will return ni.

Parameters:

  • id (String)

    the id of child node; when it starts with ‘.’, the method will find nodes that id ends with the param

Returns:

  • (Node, nil)

    child node by provided id; when id not found it will return ni



68
69
70
71
# File 'lib/clerq/entities/node.rb', line 68

def item(id)
  return @items.find{|r| r.id.end_with? id[1..-1]} if id.start_with? '.'
  @items.find{|r| r.id.eql? id}
end

#itemsArray<Node>

Returns list of child nodes; when the node metadate has :order_index arrtibute, the list will be ordered according the attribute value.

Returns:

  • (Array<Node>)

    list of child nodes; when the node metadate has :order_index arrtibute, the list will be ordered according the attribute value



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/clerq/entities/node.rb', line 82

def items
  return @items if @items.empty? || order_index.empty?
  [].tap do |ordered|
    source = Array.new(@items)
    order_index.each do |o|
      e = source.delete(item(o))
      ordered << e if e
    end
    ordered.concat(source)
  end
end

Returns macro links in the node #body.

Returns:

  • (Array<String>)

    macro links in the node #body



114
115
116
117
# File 'lib/clerq/entities/node.rb', line 114

def links
  return [] if @body.empty?
  @body.scan(/\[\[([\w\.]*)\]\]/).flatten.uniq
end

#nesting_levelInteger

Returns the node level in the node hierarchy.

Returns:

  • (Integer)

    the node level in the node hierarchy



109
110
111
# File 'lib/clerq/entities/node.rb', line 109

def nesting_level
  @parent.nil? ? 0 : @parent.nesting_level + 1
end

#node(id) ⇒ Node

Find the first node in the node hierarchy by its id.

Add '*' prefix to find id by ends_with?

Parameters:

  • id (String)

    Node id

Returns:

  • (Node)

    or nil when node not found



130
131
132
133
134
135
136
# File 'lib/clerq/entities/node.rb', line 130

def node(id)
  if id.start_with? '*'
    ai = id[1..-1]
    return find {|n| n.id.end_with? ai}
  end
  find{|n| n.id.eql? id}
end

#order_indexArray<String>

Returns of ids from meta.

Returns:

  • (Array<String>)

    of ids from meta



74
75
76
77
# File 'lib/clerq/entities/node.rb', line 74

def order_index
  return [] unless @meta[:order_index]
  @meta[:order_index].strip.gsub(/[\s]{2,}/, ' ').split(/\s/)
end

#orphan!Object

Break of the node from parent hierarhy

Returns:

  • self



140
141
142
143
144
145
# File 'lib/clerq/entities/node.rb', line 140

def orphan!
  return unless @parent
  @parent.delete_item(self)
  @parent = nil
  self
end

#rootNode

Returns the root node in the node hierarchy.

Returns:

  • (Node)

    the root node in the node hierarchy



102
103
104
105
106
# File 'lib/clerq/entities/node.rb', line 102

def root
  n = self
  n = n.parent while n.parent
  n
end