Class: Jekyll::Compose::FileMover

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-compose/file_mover.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(movement, force = false, root = nil) ⇒ FileMover

Returns a new instance of FileMover.



7
8
9
10
11
# File 'lib/jekyll-compose/file_mover.rb', line 7

def initialize(movement, force = false, root = nil)
  @movement = movement
  @force = force
  @root = root
end

Instance Attribute Details

#forceObject (readonly)

Returns the value of attribute force.



6
7
8
# File 'lib/jekyll-compose/file_mover.rb', line 6

def force
  @force
end

#movementObject (readonly)

Returns the value of attribute movement.



6
7
8
# File 'lib/jekyll-compose/file_mover.rb', line 6

def movement
  @movement
end

#rootObject (readonly)

Returns the value of attribute root.



6
7
8
# File 'lib/jekyll-compose/file_mover.rb', line 6

def root
  @root
end

Instance Method Details

#ensure_directory_existsObject



32
33
34
35
# File 'lib/jekyll-compose/file_mover.rb', line 32

def ensure_directory_exists
  dir = File.dirname to
  Dir.mkdir(dir) unless Dir.exist?(dir)
end

#moveObject



21
22
23
24
25
26
# File 'lib/jekyll-compose/file_mover.rb', line 21

def move
  validate_source
  validate_should_write!
  ensure_directory_exists
  move_file
end

#move_fileObject



41
42
43
44
# File 'lib/jekyll-compose/file_mover.rb', line 41

def move_file
  FileUtils.mv(from, to)
  puts "#{resource_type_from.capitalize} #{from} was moved to #{to}"
end

#resource_type_fromObject



13
14
15
# File 'lib/jekyll-compose/file_mover.rb', line 13

def resource_type_from
  "file"
end

#resource_type_toObject



17
18
19
# File 'lib/jekyll-compose/file_mover.rb', line 17

def resource_type_to
  "file"
end

#validate_should_write!Object

Raises:

  • (ArgumentError)


37
38
39
# File 'lib/jekyll-compose/file_mover.rb', line 37

def validate_should_write!
  raise ArgumentError, "A #{resource_type_to} already exists at #{to}" if File.exist?(to) && !force
end

#validate_sourceObject

Raises:

  • (ArgumentError)


28
29
30
# File 'lib/jekyll-compose/file_mover.rb', line 28

def validate_source
  raise ArgumentError, "There was no #{resource_type_from} found at '#{from}'." unless File.exist? from
end