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.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cl_wiki/file.rb', line 13

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 = {}
  @metadata_keys = ['mtime']
  if autocreate
    if file_exists?
      readFile
    else
      writeToFile(default_content, false)
    end
  end
end

Instance Attribute Details

#clientLastReadModTimeObject

Returns the value of attribute clientLastReadModTime.



11
12
13
# File 'lib/cl_wiki/file.rb', line 11

def clientLastReadModTime
  @clientLastReadModTime
end

#fileExtObject (readonly)

Returns the value of attribute fileExt.



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

def fileExt
  @fileExt
end

#metadataObject (readonly)

Returns the value of attribute metadata.



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

def 
  @metadata
end

#modTimeAtLastReadObject (readonly)

Returns the value of attribute modTimeAtLastRead.



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

def modTimeAtLastRead
  @modTimeAtLastRead
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pagePathObject (readonly)

Returns the value of attribute pagePath.



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

def pagePath
  @pagePath
end

#wikiRootPathObject (readonly)

Returns the value of attribute wikiRootPath.



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

def wikiRootPath
  @wikiRootPath
end

Instance Method Details

#all_lines_are_metadata_lines(lines) ⇒ Object



141
142
143
144
# File 'lib/cl_wiki/file.rb', line 141

def (lines)
  lines.map { |ln| ln.scan(/\A(\w+):?/) }.flatten.
    map { |k| @metadata_keys.include?(k) }.uniq == [true]
end

#apply_metadataObject



154
155
156
# File 'lib/cl_wiki/file.rb', line 154

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

#contentObject



63
64
65
# File 'lib/cl_wiki/file.rb', line 63

def content
  @contents
end

#content=(newContent) ⇒ Object



67
68
69
# File 'lib/cl_wiki/file.rb', line 67

def content=(newContent)
  writeToFile(newContent)
end

#content_is_default?Boolean

Returns:

  • (Boolean)


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

def content_is_default?
  @contents.to_s == default_content
end

#default_contentObject



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

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

#deleteObject



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

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

#ding_mtimeObject



90
91
92
# File 'lib/cl_wiki/file.rb', line 90

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

#file_exists?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/cl_wiki/file.rb', line 44

def file_exists?
  FileTest.exist?(fullPathAndName)
end

#fileNameObject

Raises:

  • (Exception)


58
59
60
61
# File 'lib/cl_wiki/file.rb', line 58

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

#fullPathObject



48
49
50
51
52
# File 'lib/cl_wiki/file.rb', line 48

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

#fullPathAndNameObject



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

def fullPathAndName
  ::File.expand_path(@name + @fileExt, fullPath)
end

#make_dirs(dir) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/cl_wiki/file.rb', line 98

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



94
95
96
# File 'lib/cl_wiki/file.rb', line 94

def 
  @metadata.collect { |k, v| "#{k}: #{v}" }.join("\n") + "\n\n\n"
end

#read_metadata(lines) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/cl_wiki/file.rb', line 146

def (lines)
  @metadata = {}
  lines.each do |ln|
    key, value = ln.split(': ')
    @metadata[key] = value if @metadata_keys.include?(key)
  end
end

#readFileObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/cl_wiki/file.rb', line 109

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cl_wiki/file.rb', line 120

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?
        if (raw_lines[0..index-1])
          start_index = index + 2
        end
        break
      end
    end
  end

  if start_index > 0
    [raw_lines[0..start_index-3], raw_lines[start_index..-1]]
  else
    [[], raw_lines]
  end
end

#writeToFile(content, checkModTime = true) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cl_wiki/file.rb', line 71

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