class Clerq::Entities::Clerq::Entities::Clerq::Entities::Node

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

Attributes

body[R]

@!attribute [r] body

@return [String] the body of the node
id[W]

@!attribute [w] id

@return [Hash<Symbol, String>] the metadata of the node
meta[R]

@!attribute [r] meta

@return [Hash<Symbol, String>] the metadata of the node
parent[R]

@!attribute [r] parent

@return [Node] the parent of the node
title[R]

@!attribute [r] title

@return [String] the title of the node

Public Class Methods

new(id: '', title: '', body: '', meta: {}) click to toggle source
# 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

Public Instance Methods

<<(node) click to toggle source
# 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| ... } click to toggle source

see Enumerable#each

# 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
id() click to toggle source

When the node id starts with '.', the method will

prefix the node id with parent id

@return [String] the id of the node

# File lib/clerq/entities/node.rb, line 122
def id
  @id.start_with?('.') && @parent ? @parent.id + @id : @id
end
item(id) click to toggle source

@param id [String] the id of child node; when it starts with '.',

the method will find nodes that id ends with the param

@return [Node, nil] child node by provided id; when id not found

it will return ni
# 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() click to toggle source

@return [Array<Node>] list of child nodes; when the node

metadate has :order_index arrtibute, the list will be
ordered according the attribute value
# 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
nesting_level() click to toggle source

@return [Integer] the node level in the node hierarchy

# File lib/clerq/entities/node.rb, line 109
def nesting_level
  @parent.nil? ? 0 : @parent.nesting_level + 1
end
node(id) click to toggle source

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

Add '*' prefix to find id by ends_with?

@param [String] id Node id @return [Node] or nil when node not found

# 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() click to toggle source

@return [Array<String>] of ids from meta

# 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!() click to toggle source

Break of the node from parent hierarhy @return self

# File lib/clerq/entities/node.rb, line 140
def orphan!
  return unless @parent
  @parent.delete_item(self)
  @parent = nil
  self
end
root() click to toggle source

@return [Node] the root node in the node hierarchy

# File lib/clerq/entities/node.rb, line 102
def root
  n = self
  n = n.parent while n.parent
  n
end

Protected Instance Methods

parent=(node) click to toggle source
# File lib/clerq/entities/node.rb, line 148
def parent=(node)
  raise ArgumentError, "Invalid parameter :node" unless node.is_a? Node
  @parent = node
end