Class: ClWiki::File
- Inherits:
-
Object
- Object
- ClWiki::File
- Defined in:
- lib/cl_wiki/file.rb
Instance Attribute Summary collapse
-
#clientLastReadModTime ⇒ Object
Returns the value of attribute clientLastReadModTime.
-
#fileExt ⇒ Object
readonly
Returns the value of attribute fileExt.
-
#modTimeAtLastRead ⇒ Object
readonly
Returns the value of attribute modTimeAtLastRead.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#pagePath ⇒ Object
readonly
Returns the value of attribute pagePath.
-
#wikiRootPath ⇒ Object
readonly
Returns the value of attribute wikiRootPath.
Instance Method Summary collapse
- #apply_metadata ⇒ Object
- #content ⇒ Object
- #content=(newContent) ⇒ Object
- #content_is_default? ⇒ Boolean
- #default_content ⇒ Object
- #delete ⇒ Object
- #ding_mtime ⇒ Object
- #file_exists? ⇒ Boolean
- #fileName ⇒ Object
- #fullPath ⇒ Object
- #fullPathAndName ⇒ Object
-
#initialize(fullPageName, wikiRootPath, fileExt = $wikiPageExt, autocreate = true) ⇒ File
constructor
A new instance of File.
- #make_dirs(dir) ⇒ Object
- #metadata_to_write ⇒ Object
- #read_metadata(lines) ⇒ Object
- #readFile ⇒ Object
- #split_metadata(raw_lines) ⇒ Object
- #writeToFile(content, checkModTime = true) ⇒ Object
Constructor Details
#initialize(fullPageName, wikiRootPath, fileExt = $wikiPageExt, autocreate = true) ⇒ File
Returns a new instance of File.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/cl_wiki/file.rb', line 11 def initialize(fullPageName, wikiRootPath, fileExt=$wikiPageExt, autocreate=true) # fullPageName must start with / ? @wikiRootPath = wikiRootPath fullPageName = ClWiki::Util.convertToNativePath(fullPageName) fullPageName.ensure_slash_prefix @pagePath, @name = ::File.split(fullPageName) @pagePath = '/' if @pagePath == '.' @fileExt = fileExt @metadata = {} if autocreate if file_exists? readFile else writeToFile(default_content, false) end end end |
Instance Attribute Details
#clientLastReadModTime ⇒ Object
Returns the value of attribute clientLastReadModTime.
9 10 11 |
# File 'lib/cl_wiki/file.rb', line 9 def clientLastReadModTime @clientLastReadModTime end |
#fileExt ⇒ Object (readonly)
Returns the value of attribute fileExt.
8 9 10 |
# File 'lib/cl_wiki/file.rb', line 8 def fileExt @fileExt end |
#modTimeAtLastRead ⇒ Object (readonly)
Returns the value of attribute modTimeAtLastRead.
8 9 10 |
# File 'lib/cl_wiki/file.rb', line 8 def modTimeAtLastRead @modTimeAtLastRead end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/cl_wiki/file.rb', line 8 def name @name end |
#pagePath ⇒ Object (readonly)
Returns the value of attribute pagePath.
8 9 10 |
# File 'lib/cl_wiki/file.rb', line 8 def pagePath @pagePath end |
#wikiRootPath ⇒ Object (readonly)
Returns the value of attribute wikiRootPath.
8 9 10 |
# File 'lib/cl_wiki/file.rb', line 8 def wikiRootPath @wikiRootPath end |
Instance Method Details
#apply_metadata ⇒ Object
139 140 141 |
# File 'lib/cl_wiki/file.rb', line 139 def @modTimeAtLastRead = Time.parse(@metadata['mtime']) if @metadata.keys.include? 'mtime' end |
#content ⇒ Object
60 61 62 |
# File 'lib/cl_wiki/file.rb', line 60 def content @contents end |
#content=(newContent) ⇒ Object
64 65 66 |
# File 'lib/cl_wiki/file.rb', line 64 def content=(newContent) writeToFile(newContent) end |
#content_is_default? ⇒ Boolean
29 30 31 |
# File 'lib/cl_wiki/file.rb', line 29 def content_is_default? @contents.to_s == default_content end |
#default_content ⇒ Object
37 38 39 |
# File 'lib/cl_wiki/file.rb', line 37 def default_content "Describe " + @name + " here." end |
#delete ⇒ Object
33 34 35 |
# File 'lib/cl_wiki/file.rb', line 33 def delete File.delete(fullPathAndName) if File.exists?(fullPathAndName) end |
#ding_mtime ⇒ Object
87 88 89 |
# File 'lib/cl_wiki/file.rb', line 87 def ding_mtime @metadata['mtime'] = Time.now end |
#file_exists? ⇒ Boolean
41 42 43 |
# File 'lib/cl_wiki/file.rb', line 41 def file_exists? FileTest.exists?(fullPathAndName) end |
#fileName ⇒ Object
55 56 57 58 |
# File 'lib/cl_wiki/file.rb', line 55 def fileName raise Exception, 'ClWikiFile.fileName is deprecated, use fullPathAndName' # fullPathAndName end |
#fullPath ⇒ Object
45 46 47 48 49 |
# File 'lib/cl_wiki/file.rb', line 45 def fullPath res = ::File.(::File.join(@wikiRootPath, @pagePath)) raise 'no dirs in fullPath' if res.split('/').empty? res end |
#fullPathAndName ⇒ Object
51 52 53 |
# File 'lib/cl_wiki/file.rb', line 51 def fullPathAndName ::File.(@name + @fileExt, fullPath) end |
#make_dirs(dir) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/cl_wiki/file.rb', line 95 def make_dirs(dir) # need to commit each dir as we make it, which is why we just don't # call File::makedirs. Core code copied from ftools.rb parent = ::File.dirname(dir) return if parent == dir or FileTest.directory? dir make_dirs parent unless FileTest.directory? parent if ::File.basename(dir) != "" Dir.mkdir dir, 0755 end end |
#metadata_to_write ⇒ Object
91 92 93 |
# File 'lib/cl_wiki/file.rb', line 91 def @metadata.collect { |k, v| "#{k}: #{v}" }.join("\n") + "\n\n\n" end |
#read_metadata(lines) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/cl_wiki/file.rb', line 131 def (lines) @metadata = {} lines.each do |ln| key, value = ln.split(': ') @metadata[key] = value end end |
#readFile ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/cl_wiki/file.rb', line 106 def readFile ::File.open(fullPathAndName, 'r') do |f| @modTimeAtLastRead = f.mtime raw_lines = f.readlines , content = (raw_lines) () @contents = content end end |
#split_metadata(raw_lines) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/cl_wiki/file.rb', line 117 def (raw_lines) start_index = 0 raw_lines.each_with_index do |ln, index| if ln.chomp.empty? next_line = raw_lines[index+1] if next_line.nil? || next_line.chomp.empty? start_index = index + 2 break end end end [raw_lines[0..start_index-3], raw_lines[start_index..-1]] end |
#writeToFile(content, checkModTime = true) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cl_wiki/file.rb', line 68 def writeToFile(content, checkModTime=true) if checkModTime # refactor, bring raiseIfMTimeNotEqual back into this class ClWiki::Util.raiseIfMTimeNotEqual(@modTimeAtLastRead, fullPathAndName) unless @clientLastReadModTime.nil? ClWiki::Util.raiseIfMTimeNotEqual(@clientLastReadModTime, fullPathAndName) end end make_dirs(fullPath) ding_mtime ::File.open(fullPathAndName, 'w+') do |f| f.print() f.print(content) end ::File.utime(@metadata['mtime'], @metadata['mtime'], fullPathAndName) readFile end |