Class: Jekyll::Compose::FileMover
- Inherits:
-
Object
- Object
- Jekyll::Compose::FileMover
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
#force ⇒ Object
Returns the value of attribute force.
6
7
8
|
# File 'lib/jekyll-compose/file_mover.rb', line 6
def force
@force
end
|
#movement ⇒ Object
Returns the value of attribute movement.
6
7
8
|
# File 'lib/jekyll-compose/file_mover.rb', line 6
def movement
@movement
end
|
#root ⇒ Object
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_exists ⇒ Object
33
34
35
36
|
# 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 ⇒ Object
21
22
23
24
25
26
27
|
# 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 ⇒ Object
42
43
44
45
|
# 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 ⇒ Object
13
14
15
|
# File 'lib/jekyll-compose/file_mover.rb', line 13
def resource_type_from
"file"
end
|
#resource_type_to ⇒ Object
17
18
19
|
# File 'lib/jekyll-compose/file_mover.rb', line 17
def resource_type_to
"file"
end
|
#update_front_matter ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# 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! ⇒ Object
38
39
40
|
# 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 ⇒ Object
29
30
31
|
# 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
|