Class: DocumentNode

Inherits:
Object
  • Object
show all
Defined in:
lib/document_node.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ DocumentNode

Returns a new instance of DocumentNode.



4
5
6
# File 'lib/document_node.rb', line 4

def initialize(filename)
  @filename = filename
end

Instance Method Details

#all_nodesObject

Searches breadth first till name is found



99
100
101
102
103
104
105
106
107
108
# File 'lib/document_node.rb', line 99

def all_nodes
  all = self.children
  to_search = self.children
  while !to_search.empty?
    n = to_search.pop
    to_search += n.children
    all += n.children
  end
  all
end

#childrenObject



8
9
10
11
# File 'lib/document_node.rb', line 8

def children
  files = Dir.glob(File.join(@filename, '*'))
  files.reject {|x| /meta\.txt/.match(x) }.map{ |x| DocumentNode.new(x) }
end

#contentObject

The content of the node, that is: the part after the yaml file.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/document_node.rb', line 61

def content
  out = ""
  passed = false
  File.open(filename) do |f|      
    f.each_line do |line|
      if line == "\n" && !passed
        passed = true 
      else
        out << line if passed
      end
    end
  end
  out
end

#filenameObject

Filename



28
29
30
# File 'lib/document_node.rb', line 28

def filename
  is_leaf? ? @filename : meta_file
end

#find_node(name) ⇒ Object

Searches breadth first till name is found



87
88
89
90
91
92
93
94
95
# File 'lib/document_node.rb', line 87

def find_node(name)
  to_search = self.children
  while !to_search.empty?
    n = to_search.pop
    return n if n.raw_name == name
    to_search += n.children
  end
  nil
end

#is_leaf?Boolean

Is it a directory or just a file?

Returns:

  • (Boolean)


23
24
25
# File 'lib/document_node.rb', line 23

def is_leaf?
  !File.directory?(@filename)
end

#is_root?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/document_node.rb', line 14

def is_root?
  @filename == DocumentTree.root.raw_filename
end

#meta_dataObject



38
39
40
# File 'lib/document_node.rb', line 38

def 
  meta_yaml
end

#meta_fileObject



33
34
35
# File 'lib/document_node.rb', line 33

def meta_file
  File.join(@filename, 'meta.txt')
end

#meta_yamlObject

The meta information of this node. If it’s a file, than the meta.txt of it’s child files is taken, else the file itself is taken. From this file the top part must be the YAML meta data.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/document_node.rb', line 44

def meta_yaml
  if !(!is_leaf? && !File.exist?(meta_file))
    passed = false
    out = ""
    File.open(filename).each_line do |line|
      passed = true if line == "\n"
      break if passed
      out << line
    end
    YAML::load(out)
  else
    {:name => raw_name}
  end
end

#nameObject



111
112
113
# File 'lib/document_node.rb', line 111

def name
  meta_yaml['name'] || raw_name
end

#raw_filenameObject



18
19
20
# File 'lib/document_node.rb', line 18

def raw_filename
  @filename
end

#raw_name(options = {}) ⇒ Object

Raw folder name of this node. Strips out 0001_foldername to make positioning possible



78
79
80
81
82
83
84
# File 'lib/document_node.rb', line 78

def raw_name(options = {})
  out = /.*\/[0-9]*_?(.*)(.txt)?/.match(@filename)[1]
  out.gsub!('.txt', '') unless options[:ext]
  out
  #out.gsub('.txt', '') #
  out
end