Module: Cuporter::Node::BaseMethods

Includes:
Numbering, Sorting, Totalling
Defined in:
lib/cuporter/node/node_base.rb

Instance Method Summary collapse

Methods included from Numbering

#number_all_descendants

Methods included from Totalling

#total, #total!

Methods included from Sorting

#<=>, #sort!, #sort_all_descendants!

Instance Method Details

#add_leaf(node, *path) ⇒ Object Also known as: add_to_end



31
32
33
34
35
36
# File 'lib/cuporter/node/node_base.rb', line 31

def add_leaf(node, *path)
  file_node = Node.new_node("file", document, "fs_name" => path.pop)
  file_node << node
  parent = node_at(*path)
  parent.add_child(file_node) 
end

#cuke_nameObject



22
23
24
# File 'lib/cuporter/node/node_base.rb', line 22

def cuke_name
  @cuke_name ||= self['cuke_name'].to_s
end

#cuke_namesObject



18
19
20
# File 'lib/cuporter/node/node_base.rb', line 18

def cuke_names
  children.collect {|c| c.cuke_name }
end

#eql?(other) ⇒ Boolean

value equivalence

Returns:

  • (Boolean)


64
65
66
# File 'lib/cuporter/node/node_base.rb', line 64

def eql?(other)
  node_name == other.node_name && cuke_name == other.cuke_name && children.eql?(other.children)
end

#find(node) ⇒ Object Also known as: has_child?



26
27
28
# File 'lib/cuporter/node/node_base.rb', line 26

def find(node)
  children.find {|c| c.eql? node}
end

#has_children?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/cuporter/node/node_base.rb', line 10

def has_children?
  children.size > 0
end

#namesObject



14
15
16
# File 'lib/cuporter/node/node_base.rb', line 14

def names
  children.collect {|c| c.name }
end

#node_at(*path) ⇒ Object

*path is a list of nodes forming a path to the last one.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cuporter/node/node_base.rb', line 40

def node_at(*path)
  return self if path.empty?

  # recursive loop ends when last path_node is shifted off the array
  attributes = {'fs_name' => path.shift}

  # create and add the child node if it's not found among the immediate
  # children of self
  child = children.find do |c|
    c.node_name == 'dir' && c.fs_name == attributes['fs_name']
  end
  unless child
    child = Node.new_node('dir', document, attributes)
    add_child(child)
  end

  child.node_at(*path)
end

#short_cuke_nameObject



59
60
61
# File 'lib/cuporter/node/node_base.rb', line 59

def short_cuke_name
  @short_cuke_name ||= (cn = cuke_name) ? cn.split(/:\s*/).last : ""
end

#to_csvObject



75
76
77
78
79
# File 'lib/cuporter/node/node_base.rb', line 75

def to_csv
  s = Cuporter::Formatters::Csv.build_line(self)
  s += children.map {|n| n.to_csv}.join
  s
end

#to_textObject Also known as: to_pretty



68
69
70
71
72
# File 'lib/cuporter/node/node_base.rb', line 68

def to_text
  s = Cuporter::Formatters::Text.build_line(self)
  s += children.map {|n| n.to_text}.join
  s
end