Class: Rwiki::Utils::FileHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/rwiki/utils/file_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FileHelper

Returns a new instance of FileHelper.



6
7
8
# File 'lib/rwiki/utils/file_helper.rb', line 6

def initialize(path)
  @path = self.class.sanitize_path(path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/rwiki/utils/file_helper.rb', line 4

def path
  @path
end

Class Method Details

.create_home_page!Object



109
110
111
112
113
114
# File 'lib/rwiki/utils/file_helper.rb', line 109

def self.create_home_page!
  return if File.exists?(Rwiki.configuration.root_page_full_file_path)

  FileUtils.mkdir_p(Rwiki.configuration.rwiki_path)
  FileUtils.touch(Rwiki.configuration.root_page_full_file_path)
end

.expand_node_file_path(path) ⇒ Object



105
106
107
# File 'lib/rwiki/utils/file_helper.rb', line 105

def self.expand_node_file_path(path)
  "#{expand_node_path(path)}.#{Rwiki.configuration.page_file_extension}"
end

.expand_node_path(path) ⇒ Object



101
102
103
# File 'lib/rwiki/utils/file_helper.rb', line 101

def self.expand_node_path(path)
  File.join(Rwiki.configuration.rwiki_path, sanitize_path(path))
end

.sanitize_path(path) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rwiki/utils/file_helper.rb', line 89

def self.sanitize_path(path)
  sanitized_path = path.clone

  sanitized_path = '/' + sanitized_path unless sanitized_path.start_with?('/')
  # remove the page file extension
  sanitized_path.gsub!(/\.#{Rwiki.configuration.page_file_extension}$/, '')
  # remove the rwiki path
  sanitized_path.sub!(Rwiki.configuration.rwiki_path, '') if path.start_with?(Rwiki.configuration.rwiki_path)

  sanitized_path
end

Instance Method Details

#add_page(name) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/rwiki/utils/file_helper.rb', line 42

def add_page(name)
  new_file_full_path = File.join(full_path, name)
  new_file_full_path += '.txt' unless name.end_with?(".#{Rwiki.configuration.page_file_extension}")

  FileUtils.mkdir_p(full_path) unless Dir.exists?(full_path)
  File.open(new_file_full_path, 'w') { |f| f.write("h1. #{name}\n\n") }
  new_file_full_path
end

#basenameObject



26
27
28
# File 'lib/rwiki/utils/file_helper.rb', line 26

def basename
  File.basename(path)
end

#deleteObject



51
52
53
54
# File 'lib/rwiki/utils/file_helper.rb', line 51

def delete
  FileUtils.rm_rf(full_path)
  FileUtils.rm_rf("#{full_path}.txt")
end

#exists?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/rwiki/utils/file_helper.rb', line 30

def exists?
  File.exists?(full_file_path)
end

#file_pathObject



10
11
12
# File 'lib/rwiki/utils/file_helper.rb', line 10

def file_path
  [path, Rwiki.configuration.page_file_extension].join('.')
end

#full_file_pathObject



18
19
20
# File 'lib/rwiki/utils/file_helper.rb', line 18

def full_file_path
  [full_path, Rwiki.configuration.page_file_extension].join('.')
end

#full_parent_pathObject



22
23
24
# File 'lib/rwiki/utils/file_helper.rb', line 22

def full_parent_path
  File.dirname(full_path)
end

#full_pathObject



14
15
16
# File 'lib/rwiki/utils/file_helper.rb', line 14

def full_path
  self.class.expand_node_path(path)
end

#move_to(new_parent_path) ⇒ Object



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

def move_to(new_parent_path)
  new_parent_full_path = self.class.expand_node_path(new_parent_path)
  return false if new_parent_full_path == full_path

  new_parent_file_full_path = self.class.expand_node_file_path(new_parent_path)
  if File.exists?(new_parent_file_full_path)
    FileUtils.mkdir(new_parent_full_path) unless Dir.exists?(new_parent_full_path)

    FileUtils.mv(full_path, new_parent_full_path) if Dir.exists?(full_path)
    FileUtils.mv(full_file_path, new_parent_full_path)

    @path = File.join(new_parent_path, basename)
    true
  else
    false
  end
end

#read_file_contentObject



34
35
36
# File 'lib/rwiki/utils/file_helper.rb', line 34

def read_file_content
  File.open(full_file_path, 'r:UTF-8') { |f| f.read }
end

#rename_to(new_name) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rwiki/utils/file_helper.rb', line 56

def rename_to(new_name)
  new_name.gsub!(/\.#{Rwiki.configuration.page_file_extension}$/, '')
  new_full_path = File.join(full_parent_path, new_name)

  unless File.exists?(new_full_path)
    FileUtils.mv(full_path, new_full_path) if Dir.exists?(full_path)
    FileUtils.mv(full_file_path, "#{new_full_path}.#{Rwiki.configuration.page_file_extension}")

    @path = self.class.sanitize_path(new_full_path)
    true
  else
    false
  end
end

#update_file_content(content) ⇒ Object



38
39
40
# File 'lib/rwiki/utils/file_helper.rb', line 38

def update_file_content(content)
  File.open(full_file_path, 'w:UTF-8') { |f| f.write(content) }
end