class DNote::Format

Notes Formatter

Constants

EXTENSIONS

Attributes

dryrun[R]
format[R]
notes[R]
output[R]
template[R]
title[R]

Public Class Methods

new(notes, format: "text", title: "Developer's Notes", template: nil, output: nil, dryrun: false) click to toggle source
# File lib/dnote/format.rb, line 21
def initialize(notes,
               format: "text",
               title: "Developer's Notes",
               template: nil,
               output: nil,
               dryrun: false)
  @notes = notes
  @format = format
  @title = title
  @dryrun = dryrun
  @template = template
  @output = output
end

Public Instance Methods

render() click to toggle source
# File lib/dnote/format.rb, line 35
def render
  if notes.empty?
    $stderr << "No #{notes.labels.join(", ")} notes.\n"
  else
    case format
    when "custom"
      render_custom
    else
      render_template
    end
  end
end

Private Instance Methods

debug?() click to toggle source
# File lib/dnote/format.rb, line 104
def debug?
  $DEBUG
end
dryrun?() click to toggle source
# File lib/dnote/format.rb, line 100
def dryrun?
  @dryrun
end
erb(file) click to toggle source
# File lib/dnote/format.rb, line 67
def erb(file)
  scope = ErbScope.new(notes: notes, title: title)
  scope.render(file)
end
fu() click to toggle source
# File lib/dnote/format.rb, line 108
def fu
  @fu ||=
    if dryrun? && debug?
      FileUtils::DryRun
    elsif dryrun?
      FileUtils::Noop
    elsif debug?
      FileUtils::Verbose
    else
      FileUtils
    end
end
publish(result, fname = nil) click to toggle source
# File lib/dnote/format.rb, line 72
def publish(result, fname = nil)
  if output
    write(result, fname)
  else
    puts(result)
  end
  $stderr << "(#{notes.counts.map { |l, n| "#{n} #{l}s" }.join(", ")})\n"
end
render_custom() click to toggle source

C U S T O M

# File lib/dnote/format.rb, line 52
def render_custom
  result = erb(template)
  publish(result)
end
render_template() click to toggle source

T E M P L A T E

# File lib/dnote/format.rb, line 59
def render_template
  template = File.join(File.dirname(__FILE__), "templates", "#{format}.erb")
  raise "No such format - #{format}" unless File.exist?(template)

  result = erb(template)
  publish(result)
end
write(result, fname = nil) click to toggle source
# File lib/dnote/format.rb, line 81
def write(result, fname = nil)
  if output.to_s[-1, 1] == "/" || File.directory?(output)
    fmt = format.split("/").first
    ext = EXTENSIONS[fmt] || fmt
    file = File.join(output, fname || "notes.#{ext}")
  else
    file = output
  end
  if dryrun?
    puts "mkdir: #{File.dirname(file)}"
    puts "write: #{file}"
  else
    dir = File.dirname(file)
    fu.mkdir(dir) unless File.exist?(dir)
    File.open(file, "w") { |f| f << result }
  end
  file
end