Class: DocbookFiles::FileRef
- Inherits:
-
Object
- Object
- DocbookFiles::FileRef
- Defined in:
- lib/docbook_files/file_ref.rb
Overview
A FileRef represents a file inclusion (<xi:include href=‘…’) or reference (<imagedata filref==‘…’) in DocBook. It points to a FileData instance, that represents the actual file. Multiple FileRefs can point to the same FileData.
A FileRef contains
-
the parent FileRef
-
the kind of relation (included or referenced)
-
the FileData instance
Instance Attribute Summary collapse
-
#file_data ⇒ Object
TODO file and line?.
-
#includes ⇒ Object
Returns the value of attribute includes.
-
#parent ⇒ Object
TODO file and line?.
-
#refs ⇒ Object
Returns the value of attribute refs.
-
#rel_type ⇒ Object
TODO file and line?.
Instance Method Summary collapse
-
#find_non_existing_files ⇒ Object
Return the names and parent files of non-existing files.
-
#initialize(name, parent_dir = ".", parent_file = nil) ⇒ FileRef
constructor
A new instance of FileRef.
- #method_missing(name, *args, &block) ⇒ Object
-
#names ⇒ Object
Return a tree-like array with all names.
-
#to_hash(props, type) ⇒ Object
Return a hash with the values for the passed symbols.
-
#traverse(props = [], type = FileRefTypes::TYPE_MAIN) ⇒ Object
Return a tree-like array of maps with the requested properties (symbols).
-
#traverse_as_table(props, level = 0, type = FileRefTypes::TYPE_MAIN) ⇒ Object
Return a table-like array of maps with the requested properties (symbols).
Constructor Details
#initialize(name, parent_dir = ".", parent_file = nil) ⇒ FileRef
Returns a new instance of FileRef.
20 21 22 23 24 25 |
# File 'lib/docbook_files/file_ref.rb', line 20 def initialize(name,parent_dir=".",parent_file=nil) @file_data = FileData.for(name,parent_dir) @parent = (parent_file.nil? ? nil : parent_file.name) @includes = [] @refs = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
27 28 29 |
# File 'lib/docbook_files/file_ref.rb', line 27 def method_missing(name, *args, &block) @file_data.send(name,*args, &block) end |
Instance Attribute Details
#file_data ⇒ Object
TODO file and line?
17 18 19 |
# File 'lib/docbook_files/file_ref.rb', line 17 def file_data @file_data end |
#includes ⇒ Object
Returns the value of attribute includes.
18 19 20 |
# File 'lib/docbook_files/file_ref.rb', line 18 def includes @includes end |
#parent ⇒ Object
TODO file and line?
17 18 19 |
# File 'lib/docbook_files/file_ref.rb', line 17 def parent @parent end |
#refs ⇒ Object
Returns the value of attribute refs.
18 19 20 |
# File 'lib/docbook_files/file_ref.rb', line 18 def refs @refs end |
#rel_type ⇒ Object
TODO file and line?
17 18 19 |
# File 'lib/docbook_files/file_ref.rb', line 17 def rel_type @rel_type end |
Instance Method Details
#find_non_existing_files ⇒ Object
Return the names and parent files of non-existing files
69 70 71 72 |
# File 'lib/docbook_files/file_ref.rb', line 69 def find_non_existing_files files = traverse([:name, :status, :parent]) files.flatten.reject{|f| f[:status] != FileData::STATUS_NOT_FOUND}.map{|f| f.delete(:status); f} end |
#names ⇒ Object
Return a tree-like array with all names
75 76 77 |
# File 'lib/docbook_files/file_ref.rb', line 75 def names self.traverse([:name]) end |
#to_hash(props, type) ⇒ Object
Return a hash with the values for the passed symbols. The type is added.
Example: to_hash([:name, :mime]) would return
{:type => "main", :name => "name", :mime => "application/xml"}.
47 48 49 50 51 |
# File 'lib/docbook_files/file_ref.rb', line 47 def to_hash(props,type) me_hash = {:type => type} props.each {|p| me_hash[p] = self.send(p)} me_hash end |
#traverse(props = [], type = FileRefTypes::TYPE_MAIN) ⇒ Object
Return a tree-like array of maps with the requested properties (symbols)
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/docbook_files/file_ref.rb', line 55 def traverse(props=[],type=FileRefTypes::TYPE_MAIN) me = self.to_hash(props,type) me2 = [me] unless self.refs.empty?() me2 += self.refs.map {|r| r.to_hash(props,FileRefTypes::TYPE_REFERENCE)} end if self.includes.empty?() me2 else me2 + self.includes.map {|i| i.traverse(props,FileRefTypes::TYPE_INCLUDE)} end end |
#traverse_as_table(props, level = 0, type = FileRefTypes::TYPE_MAIN) ⇒ Object
Return a table-like array of maps with the requested properties (symbols). Each entry gets a level indicator (:level) to show the tree-level.
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/docbook_files/file_ref.rb', line 84 def traverse_as_table(props,level=0,type=FileRefTypes::TYPE_MAIN) me = self.to_hash(props,type) me[:level] = level me2 = [me] unless self.refs.empty?() me2 += self.refs.map {|r| x = r.to_hash(props,FileRefTypes::TYPE_REFERENCE); x[:level] = level+1; x} end unless self.includes.empty?() me2 += self.includes.map {|i| i.traverse_as_table(props,level+1,FileRefTypes::TYPE_INCLUDE)} end me2.flatten end |