class Rwiki::Utils::FileHelper

Attributes

path[R]

Public Class Methods

create_home_page!() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
new(path) click to toggle source
# File lib/rwiki/utils/file_helper.rb, line 6
def initialize(path)
  @path = self.class.sanitize_path(path)
end
sanitize_path(path) click to toggle source
# 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

Public Instance Methods

add_page(name) click to toggle source
# 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
basename() click to toggle source
# File lib/rwiki/utils/file_helper.rb, line 26
def basename
  File.basename(path)
end
delete() click to toggle source
# File lib/rwiki/utils/file_helper.rb, line 51
def delete
  FileUtils.rm_rf(full_path)
  FileUtils.rm_rf("#{full_path}.txt")
end
exists?() click to toggle source
# File lib/rwiki/utils/file_helper.rb, line 30
def exists?
  File.exists?(full_file_path)
end
file_path() click to toggle source
# File lib/rwiki/utils/file_helper.rb, line 10
def file_path
  [path, Rwiki.configuration.page_file_extension].join('.')
end
full_file_path() click to toggle source
# File lib/rwiki/utils/file_helper.rb, line 18
def full_file_path
  [full_path, Rwiki.configuration.page_file_extension].join('.')
end
full_parent_path() click to toggle source
# File lib/rwiki/utils/file_helper.rb, line 22
def full_parent_path
  File.dirname(full_path)
end
full_path() click to toggle source
# File lib/rwiki/utils/file_helper.rb, line 14
def full_path
  self.class.expand_node_path(path)
end
move_to(new_parent_path) click to toggle source
# 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_content() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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