Class: Clerq::Entities::Node
- Inherits:
-
Object
- Object
- Clerq::Entities::Node
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/clerq/entities/node.rb
Overview
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#id ⇒ String
When the node id starts with ‘.’, the method will prefix the node id with parent id.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #<<(node) ⇒ Object
-
#each {|_self| ... } ⇒ Object
see Enumerable#each.
-
#initialize(id: '', title: '', body: '', meta: {}) ⇒ Node
constructor
A new instance of Node.
-
#item(id) ⇒ Node?
Child node by provided id; when id not found it will return ni.
-
#items ⇒ Array<Node>
List of child nodes; when the node metadate has :order_index arrtibute, the list will be ordered according the attribute value.
-
#links ⇒ Array<String>
Macro links in the node #body.
-
#nesting_level ⇒ Integer
The node level in the node hierarchy.
-
#node(id) ⇒ Node
Find the first node in the node hierarchy by its id.
-
#order_index ⇒ Array<String>
Of ids from meta.
-
#orphan! ⇒ Object
Break of the node from parent hierarhy.
-
#root ⇒ Node
The root node in the node hierarchy.
Constructor Details
#initialize(id: '', title: '', body: '', meta: {}) ⇒ Node
Returns a new instance of Node.
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 .is_a? Hash id = .delete(:id) if id.empty? && [:id] .delete(:id) unless id.empty? @parent = nil @items = [] @id = id @title = title @body = body @meta = end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
27 28 29 |
# File 'lib/clerq/entities/node.rb', line 27 def body @body end |
#id ⇒ String
When the node id starts with ‘.’, the method will
prefix the node id with parent id
122 123 124 |
# File 'lib/clerq/entities/node.rb', line 122 def id @id.start_with?('.') && @parent ? @parent.id + @id : @id end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
31 32 33 |
# File 'lib/clerq/entities/node.rb', line 31 def @meta end |
#parent ⇒ Object
Returns the value of attribute parent.
20 21 22 |
# File 'lib/clerq/entities/node.rb', line 20 def parent @parent end |
#title ⇒ Object (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
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
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.
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 |
#items ⇒ Array<Node>
Returns 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 |
#links ⇒ Array<String>
Returns 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_level ⇒ Integer
Returns 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?
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_index ⇒ Array<String>
Returns 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
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 |