Class: ClWiki::File

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#clientLastReadModTimeObject

Returns the value of attribute clientLastReadModTime.



9
10
11
# File 'lib/cl_wiki/file.rb', line 9

def clientLastReadModTime
  @clientLastReadModTime
end

#fileExtObject (readonly)

Returns the value of attribute fileExt.



8
9
10
# File 'lib/cl_wiki/file.rb', line 8

def fileExt
  @fileExt
end

#modTimeAtLastReadObject (readonly)

Returns the value of attribute modTimeAtLastRead.



8
9
10
# File 'lib/cl_wiki/file.rb', line 8

def modTimeAtLastRead
  @modTimeAtLastRead
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/cl_wiki/file.rb', line 8

def name
  @name
end

#pagePathObject (readonly)

Returns the value of attribute pagePath.



8
9
10
# File 'lib/cl_wiki/file.rb', line 8

def pagePath
  @pagePath
end

#wikiRootPathObject (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_metadataObject



139
140
141
# File 'lib/cl_wiki/file.rb', line 139

def 
  @modTimeAtLastRead = Time.parse(@metadata['mtime']) if @metadata.keys.include? 'mtime'
end

#contentObject



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

Returns:

  • (Boolean)


29
30
31
# File 'lib/cl_wiki/file.rb', line 29

def content_is_default?
  @contents.to_s == default_content
end

#default_contentObject



37
38
39
# File 'lib/cl_wiki/file.rb', line 37

def default_content
  "Describe " + @name + " here."
end

#deleteObject



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

def delete
  File.delete(fullPathAndName) if File.exists?(fullPathAndName)
end

#ding_mtimeObject



87
88
89
# File 'lib/cl_wiki/file.rb', line 87

def ding_mtime
  @metadata['mtime'] = Time.now
end

#file_exists?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/cl_wiki/file.rb', line 41

def file_exists?
  FileTest.exists?(fullPathAndName)
end

#fileNameObject

Raises:

  • (Exception)


55
56
57
58
# File 'lib/cl_wiki/file.rb', line 55

def fileName
  raise Exception, 'ClWikiFile.fileName is deprecated, use fullPathAndName'
  # fullPathAndName
end

#fullPathObject



45
46
47
48
49
# File 'lib/cl_wiki/file.rb', line 45

def fullPath
  res = ::File.expand_path(::File.join(@wikiRootPath, @pagePath))
  raise 'no dirs in fullPath' if res.split('/').empty?
  res
end

#fullPathAndNameObject



51
52
53
# File 'lib/cl_wiki/file.rb', line 51

def fullPathAndName
  ::File.expand_path(@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_writeObject



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

#readFileObject



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