Class: DocbookFiles::FileData
- Inherits:
-
Object
- Object
- DocbookFiles::FileData
- Defined in:
- lib/docbook_files/file_data.rb
Overview
Represents the actual file that is included or referenced in a DocBook project. For every file there should only one FileData instance, so we use a factory method #FileData.for to create new instances.
Constant Summary collapse
- STATUS_OK =
File exists and no error happened
0- STATUS_NOT_FOUND =
File does not exist
1- STATUS_ERR =
Error while processing the file, see #error_string
2- @@Files =
A storage for all FileData instances.
{}
- @@MainDir =
The directory of the main file. All #path names are relative to that
""
Instance Attribute Summary collapse
-
#checksum ⇒ Object
readonly
SHA1 checksum, file size in bytes, mime type, last modified timestamp.
-
#docbook ⇒ Object
XML data: namespace, docbook flag, namespace version, start tag.
-
#error_string ⇒ Object
Returns the value of attribute error_string.
-
#exists ⇒ Object
Returns the value of attribute exists.
-
#full_name ⇒ Object
Returns the value of attribute full_name.
-
#key ⇒ Object
readonly
Unique key (path+checksum).
-
#mime ⇒ Object
readonly
SHA1 checksum, file size in bytes, mime type, last modified timestamp.
-
#name ⇒ Object
Returns the value of attribute name.
-
#namespace ⇒ Object
XML data: namespace, docbook flag, namespace version, start tag.
-
#path ⇒ Object
Returns the value of attribute path.
-
#size ⇒ Object
readonly
SHA1 checksum, file size in bytes, mime type, last modified timestamp.
-
#status ⇒ Object
Returns the value of attribute status.
-
#tag ⇒ Object
XML data: namespace, docbook flag, namespace version, start tag.
-
#ts ⇒ Object
readonly
SHA1 checksum, file size in bytes, mime type, last modified timestamp.
-
#version ⇒ Object
XML data: namespace, docbook flag, namespace version, start tag.
Class Method Summary collapse
-
.files ⇒ Object
Return all existing FileData instances.
-
.for(name, parent_dir = ".") ⇒ Object
Factory method for FileData instances.
-
.reset ⇒ Object
Reset the FileData storage – must be done before every run!.
-
.storage ⇒ Object
Return the FileData storage – for testing only.
Instance Method Summary collapse
-
#add_includes(incs) ⇒ Object
Add included FileDatas.
-
#add_references(incs) ⇒ Object
Add referenced FileDatas.
-
#add_rel(type, target) ⇒ Object
Add a one-way relationship, type and target, to self.
-
#add_rels(type, invtype, targets) ⇒ Object
Add a two-way relationship between self and a number of targets.
-
#exists? ⇒ Boolean
Does the really file exist?.
-
#get_rel_targets(type) ⇒ Object
Get all targets for a relation.
-
#included_by ⇒ Object
Retrieves all FileDatas that include self.
-
#includes ⇒ Object
Retrieves all included FileDatas.
-
#initialize(name, full_name, key, parent_dir = ".") ⇒ FileData
constructor
A new instance of FileData.
-
#referenced_by ⇒ Object
Retrieves all FileDatas that reference self.
-
#references ⇒ Object
Retrieves all referenced files.
-
#relative2main(file_name) ⇒ Object
Try to find the path of file_name that is relative to directory of the _main file_.
-
#to_hash(props) ⇒ Object
Return a hash with the values for the passed symbols.
Constructor Details
#initialize(name, full_name, key, parent_dir = ".") ⇒ FileData
Returns a new instance of FileData.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/docbook_files/file_data.rb', line 63 def initialize(name, full_name, key, parent_dir=".") @path = relative2main(full_name) @full_name = full_name @name = File.basename(name) @key = key @namespace = "" @docbook = false @version = "" @tag = "" @error_string = nil @rels = [] if (File.exists?(@full_name)) @status = STATUS_OK @ts = File.mtime(full_name) @size = File.size(full_name) @checksum = calc_checksum(full_name) @mime = get_mime_type() else @status = STATUS_NOT_FOUND @ts = Time.now @size = 0 @checksum = "" @mime = "" @error_string = "file not found" end end |
Instance Attribute Details
#checksum ⇒ Object (readonly)
SHA1 checksum, file size in bytes, mime type, last modified timestamp
58 59 60 |
# File 'lib/docbook_files/file_data.rb', line 58 def checksum @checksum end |
#docbook ⇒ Object
XML data: namespace, docbook flag, namespace version, start tag
60 61 62 |
# File 'lib/docbook_files/file_data.rb', line 60 def docbook @docbook end |
#error_string ⇒ Object
Returns the value of attribute error_string.
53 54 55 |
# File 'lib/docbook_files/file_data.rb', line 53 def error_string @error_string end |
#exists ⇒ Object
Returns the value of attribute exists.
52 53 54 |
# File 'lib/docbook_files/file_data.rb', line 52 def exists @exists end |
#full_name ⇒ Object
Returns the value of attribute full_name.
54 55 56 |
# File 'lib/docbook_files/file_data.rb', line 54 def full_name @full_name end |
#key ⇒ Object (readonly)
Unique key (path+checksum)
56 57 58 |
# File 'lib/docbook_files/file_data.rb', line 56 def key @key end |
#mime ⇒ Object (readonly)
SHA1 checksum, file size in bytes, mime type, last modified timestamp
58 59 60 |
# File 'lib/docbook_files/file_data.rb', line 58 def mime @mime end |
#name ⇒ Object
Returns the value of attribute name.
52 53 54 |
# File 'lib/docbook_files/file_data.rb', line 52 def name @name end |
#namespace ⇒ Object
XML data: namespace, docbook flag, namespace version, start tag
60 61 62 |
# File 'lib/docbook_files/file_data.rb', line 60 def namespace @namespace end |
#path ⇒ Object
Returns the value of attribute path.
52 53 54 |
# File 'lib/docbook_files/file_data.rb', line 52 def path @path end |
#size ⇒ Object (readonly)
SHA1 checksum, file size in bytes, mime type, last modified timestamp
58 59 60 |
# File 'lib/docbook_files/file_data.rb', line 58 def size @size end |
#status ⇒ Object
Returns the value of attribute status.
53 54 55 |
# File 'lib/docbook_files/file_data.rb', line 53 def status @status end |
#tag ⇒ Object
XML data: namespace, docbook flag, namespace version, start tag
60 61 62 |
# File 'lib/docbook_files/file_data.rb', line 60 def tag @tag end |
#ts ⇒ Object (readonly)
SHA1 checksum, file size in bytes, mime type, last modified timestamp
58 59 60 |
# File 'lib/docbook_files/file_data.rb', line 58 def ts @ts end |
#version ⇒ Object
XML data: namespace, docbook flag, namespace version, start tag
60 61 62 |
# File 'lib/docbook_files/file_data.rb', line 60 def version @version end |
Class Method Details
.files ⇒ Object
Return all existing FileData instances
30 |
# File 'lib/docbook_files/file_data.rb', line 30 def self.files; @@Files.values; end |
.for(name, parent_dir = ".") ⇒ Object
Factory method for FileData instances. Checks if there is already an instance.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/docbook_files/file_data.rb', line 40 def self.for(name,parent_dir=".") full_name = get_full_name(name, parent_dir) # Initialize the main dir name for path construction @@MainDir = File.dirname(full_name) if @@Files.size == 0 key = full_name if (@@Files[key].nil?) @@Files[key] = FileData.new(name, full_name, key, parent_dir) end @@Files[key] end |
.reset ⇒ Object
Reset the FileData storage – must be done before every run!
33 34 35 36 |
# File 'lib/docbook_files/file_data.rb', line 33 def self.reset @@Files={} @@MainDir = "" end |
.storage ⇒ Object
Return the FileData storage – for testing only
27 |
# File 'lib/docbook_files/file_data.rb', line 27 def self.storage; @@Files; end |
Instance Method Details
#add_includes(incs) ⇒ Object
Add included FileDatas. Establishes a two way relationship between self and the included files:
self -> TYPE_INCLUDE -> target self <- TYPE_INCLUDED_BY <- target
TODO: should be ‘add_includes’
119 120 121 |
# File 'lib/docbook_files/file_data.rb', line 119 def add_includes(incs) add_rels(FileRefTypes::TYPE_INCLUDE, FileRefTypes::TYPE_INCLUDED_BY, incs) end |
#add_references(incs) ⇒ Object
Add referenced FileDatas. Establishes a two way relationship between self and the referenced files:
self -> TYPE_REFERENCE -> target self <- TYPE_REFERENCED_BY <- target
137 138 139 |
# File 'lib/docbook_files/file_data.rb', line 137 def add_references(incs) add_rels(FileRefTypes::TYPE_REFERENCE, FileRefTypes::TYPE_REFERENCED_BY, incs) end |
#add_rel(type, target) ⇒ Object
Add a one-way relationship, type and target, to self
97 98 99 |
# File 'lib/docbook_files/file_data.rb', line 97 def add_rel(type, target) @rels << {:type => type, :target => target} end |
#add_rels(type, invtype, targets) ⇒ Object
Add a two-way relationship between self and a number of targets.
101 102 103 104 105 106 |
# File 'lib/docbook_files/file_data.rb', line 101 def add_rels(type, invtype, targets) targets.each {|t| self.add_rel(type, t) t.add_rel(invtype, self) } end |
#exists? ⇒ Boolean
Does the really file exist?
92 93 94 |
# File 'lib/docbook_files/file_data.rb', line 92 def exists? @status != STATUS_NOT_FOUND end |
#get_rel_targets(type) ⇒ Object
Get all targets for a relation
108 109 110 |
# File 'lib/docbook_files/file_data.rb', line 108 def get_rel_targets(type) @rels.find_all{|rel| rel[:type] == type}.map{|r| r[:target]} end |
#included_by ⇒ Object
Retrieves all FileDatas that include self
127 128 129 |
# File 'lib/docbook_files/file_data.rb', line 127 def included_by get_rel_targets(FileRefTypes::TYPE_INCLUDED_BY) end |
#includes ⇒ Object
Retrieves all included FileDatas
123 124 125 |
# File 'lib/docbook_files/file_data.rb', line 123 def includes get_rel_targets(FileRefTypes::TYPE_INCLUDE) end |
#referenced_by ⇒ Object
Retrieves all FileDatas that reference self
145 146 147 |
# File 'lib/docbook_files/file_data.rb', line 145 def referenced_by get_rel_targets(FileRefTypes::TYPE_REFERENCED_BY) end |
#references ⇒ Object
Retrieves all referenced files
141 142 143 |
# File 'lib/docbook_files/file_data.rb', line 141 def references get_rel_targets(FileRefTypes::TYPE_REFERENCE) end |
#relative2main(file_name) ⇒ Object
Try to find the path of file_name that is relative to directory of the _main file_. If there is no common part return the file_name.
152 153 154 155 156 157 158 159 |
# File 'lib/docbook_files/file_data.rb', line 152 def relative2main(file_name) md = file_name.match("^#{@@MainDir}/") if md.nil? file_name else md.post_match end end |
#to_hash(props) ⇒ Object
Return a hash with the values for the passed symbols.
Example: to_hash([:name, :mime]) would return
{:name => "name", :mime => "application/xml"}.
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/docbook_files/file_data.rb', line 166 def to_hash(props) me_hash = {} props.each {|p| if ([:includes, :included_by, :references, :referenced_by].member?(p)) me_hash[p] = self.send(p).map{|p2| p2.path} else me_hash[p] = self.send(p) end } me_hash end |