Class: DocumentNode
- Inherits:
-
Object
- Object
- DocumentNode
- Defined in:
- lib/document_node.rb
Instance Method Summary collapse
-
#all_nodes ⇒ Object
Searches breadth first till name is found.
- #children ⇒ Object
-
#content ⇒ Object
The content of the node, that is: the part after the yaml file.
-
#filename ⇒ Object
Filename.
-
#find_node(name) ⇒ Object
Searches breadth first till name is found.
-
#initialize(filename) ⇒ DocumentNode
constructor
A new instance of DocumentNode.
-
#is_leaf? ⇒ Boolean
Is it a directory or just a file?.
- #is_root? ⇒ Boolean
- #meta_data ⇒ Object
- #meta_file ⇒ Object
-
#meta_yaml ⇒ Object
The meta information of this node.
- #name ⇒ Object
- #raw_filename ⇒ Object
-
#raw_name(options = {}) ⇒ Object
Raw folder name of this node.
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_nodes ⇒ Object
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 |
#children ⇒ Object
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 |
#content ⇒ Object
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 |
#filename ⇒ Object
Filename
28 29 30 |
# File 'lib/document_node.rb', line 28 def filename is_leaf? ? @filename : 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?
23 24 25 |
# File 'lib/document_node.rb', line 23 def is_leaf? !File.directory?(@filename) end |
#is_root? ⇒ Boolean
14 15 16 |
# File 'lib/document_node.rb', line 14 def is_root? @filename == DocumentTree.root.raw_filename end |
#meta_data ⇒ Object
38 39 40 |
# File 'lib/document_node.rb', line 38 def end |
#meta_file ⇒ Object
33 34 35 |
# File 'lib/document_node.rb', line 33 def File.join(@filename, 'meta.txt') end |
#meta_yaml ⇒ Object
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 if !(!is_leaf? && !File.exist?()) 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 |
#name ⇒ Object
111 112 113 |
# File 'lib/document_node.rb', line 111 def name ['name'] || raw_name end |
#raw_filename ⇒ Object
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( = {}) out = /.*\/[0-9]*_?(.*)(.txt)?/.match(@filename)[1] out.gsub!('.txt', '') unless [:ext] out #out.gsub('.txt', '') # out end |