Class: Yarrow::Tools::OutputFile

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/yarrow/tools/output_file.rb

Overview

TODO: consider renaming this to OutputDocument.

Constant Summary collapse

WRITE_MODE =
'w+:UTF-8'.freeze

Instance Method Summary collapse

Methods included from Configurable

#config

Instance Method Details

#docrootString

Returns Docroot of the output target.

Returns:

  • (String)

    Docroot of the output target



15
16
17
# File 'lib/yarrow/tools/output_file.rb', line 15

def docroot
  @docroot ||= config.output_dir || 'public'
end

#index_nameString

Returns Basename reflecting the server convention (usually: index.html).

Returns:

  • (String)

    Basename reflecting the server convention (usually: index.html)



10
11
12
# File 'lib/yarrow/tools/output_file.rb', line 10

def index_name
  @index_name ||= config.index_name || 'index.html'
end

#write(path, content) ⇒ Object

Write an output file to the specified path under the docroot.

Parameters:

  • path (String)
  • content (String)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yarrow/tools/output_file.rb', line 23

def write(path, content)
  # If the target path is a directory,
  # generate a default index filename.
  if path[path.length-1] == '/'
    path = "#{path}#{index_name}"
  end

  target_path = Pathname.new("#{docroot}#{path}")

  FileUtils.mkdir_p(target_path.dirname)

  File.open(target_path.to_s, WRITE_MODE) do |file|
    file.puts(content)
  end
end