Module: Dimples::Writeable

Included in:
Page, Post
Defined in:
lib/dimples/writeable.rb

Overview

A mixin class that neatly handles writing out a file.

Instance Method Summary collapse

Instance Method Details

#write(path, context = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/dimples/writeable.rb', line 6

def write(path, context = {})
  output = context ? render(context) : contents
  parent_path = File.dirname(path)

  FileUtils.mkdir_p(parent_path) unless Dir.exist?(parent_path)

  File.open(path, 'w+') do |file|
    file.write(output)
  end
rescue SystemCallError => e
  error_message = "Failed to write #{path} (#{e.message})"
  raise Errors::PublishingError, error_message
end