class Jekyll::Compose::FileMover

Attributes

force[R]
movement[R]
root[R]

Public Class Methods

new(movement, force = false, root = nil) click to toggle source
# File lib/jekyll-compose/file_mover.rb, line 7
def initialize(movement, force = false, root = nil)
  @movement = movement
  @force = force
  @root = root
end

Public Instance Methods

ensure_directory_exists() click to toggle source
# File lib/jekyll-compose/file_mover.rb, line 33
def ensure_directory_exists
  dir = File.dirname to
  Dir.mkdir(dir) unless Dir.exist?(dir)
end
move() click to toggle source
# File lib/jekyll-compose/file_mover.rb, line 21
def move
  return unless valid_source? && valid_destination?

  ensure_directory_exists
  update_front_matter
  move_file
end
move_file() click to toggle source
# File lib/jekyll-compose/file_mover.rb, line 42
def move_file
  FileUtils.mv(from, to)
  Jekyll.logger.info "#{resource_type_from.capitalize} #{from} was moved to #{to}"
end
resource_type_from() click to toggle source
# File lib/jekyll-compose/file_mover.rb, line 13
def resource_type_from
  "file"
end
resource_type_to() click to toggle source
# File lib/jekyll-compose/file_mover.rb, line 17
def resource_type_to
  "file"
end
update_front_matter() click to toggle source
# File lib/jekyll-compose/file_mover.rb, line 47
def update_front_matter
  content = File.read(from)
  if content =~ Jekyll::Document::YAML_FRONT_MATTER_REGEXP
    content = $POSTMATCH
    match = Regexp.last_match[1] if Regexp.last_match
    data = movement.front_matter(Psych.safe_load(match))
    File.write(from, "#{Psych.dump(data)}---\n#{content}")
  end
rescue Psych::SyntaxError => e
  Jekyll.logger.warn e
rescue StandardError => e
  Jekyll.logger.warn e
end
validate_should_write!() click to toggle source
# File lib/jekyll-compose/file_mover.rb, line 38
def validate_should_write!
  raise ArgumentError, "A #{resource_type_to} already exists at #{to}" if File.exist?(to) && !force
end
validate_source() click to toggle source
# File lib/jekyll-compose/file_mover.rb, line 29
def validate_source
  raise ArgumentError, "There was no #{resource_type_from} found at '#{from}'." unless File.exist? from
end

Private Instance Methods

file_path(path) click to toggle source
# File lib/jekyll-compose/file_mover.rb, line 89
def file_path(path)
  return path if root.nil? || root.empty?

  File.join(root, path)
end
from() click to toggle source
# File lib/jekyll-compose/file_mover.rb, line 81
def from
  movement.from
end
invalidate_with(msg) click to toggle source
# File lib/jekyll-compose/file_mover.rb, line 76
def invalidate_with(msg)
  Jekyll.logger.warn msg
  false
end
to() click to toggle source
# File lib/jekyll-compose/file_mover.rb, line 85
def to
  file_path(movement.to)
end
valid_destination?() click to toggle source
# File lib/jekyll-compose/file_mover.rb, line 69
def valid_destination?
  return true if force
  return true unless File.exist?(to)

  invalidate_with "A #{resource_type_to} already exists at #{to}"
end
valid_source?() click to toggle source
# File lib/jekyll-compose/file_mover.rb, line 63
def valid_source?
  return true if File.exist?(from)

  invalidate_with "There was no #{resource_type_from} found at '#{from}'."
end