class Consult::Template

Constants

LOCATIONS

Attributes

config[R]
name[R]

Public Class Methods

new(name, config) click to toggle source
# File lib/consult/template.rb, line 14
def initialize(name, config)
  @name = name
  @config = config
end

Public Instance Methods

dest() click to toggle source
# File lib/consult/template.rb, line 45
def dest
  resolve @config.fetch(:dest)
end
expired?() click to toggle source
# File lib/consult/template.rb, line 53
def expired?
  # Treat renders as expired if a TTL isn't set, or it has never been rendered before
  return true if !config.key?(:ttl) || !dest.exist?
  dest.mtime < (Time.now - @config[:ttl].to_i)
end
ordered_locations() click to toggle source
# File lib/consult/template.rb, line 63
def ordered_locations
  @config.keys & LOCATIONS
end
path() click to toggle source
# File lib/consult/template.rb, line 33
def path
  resolve @config[:path]
end
paths() click to toggle source
# File lib/consult/template.rb, line 37
def paths
  @config.fetch(:paths, []).map { |path| resolve(path) }
end
render(save: true) click to toggle source
# File lib/consult/template.rb, line 19
def render(save: true)
  # Attempt to render
  renderer = ERB.new(contents, nil, '-')
  result = renderer.result(binding)

  File.open(dest, 'wb') { |f| f << result } if save
  puts "Consult: Rendered #{name}" if verbose?
  result
rescue StandardError => e
  STDERR.puts "Error rendering template: #{name}"
  STDERR.puts e
  nil
end
should_render?() click to toggle source
# File lib/consult/template.rb, line 49
def should_render?
  expired?
end
vars() click to toggle source
# File lib/consult/template.rb, line 41
def vars
  @config[:vars]
end
verbose?() click to toggle source
# File lib/consult/template.rb, line 59
def verbose?
  @config[:verbose]
end

Private Instance Methods

consul_contents(location) click to toggle source
# File lib/consult/template.rb, line 76
def consul_contents(location)
  [@config[location]].compact.flatten.map do |key|
    Diplomat::Kv.get(key, options: nil, not_found: :return, found: :return).force_encoding 'utf-8'
  end.join
end
contents() click to toggle source

Concatenate all the source templates together, in the order provided

# File lib/consult/template.rb, line 70
def contents
  ordered_locations.map do |location|
    location.to_s.start_with?('consul') ? consul_contents(location) : disk_contents(location)
  end.join
end
disk_contents(location) click to toggle source
# File lib/consult/template.rb, line 82
def disk_contents(location)
  [public_send(location)].compact.flatten.map do |file_path|
    File.read file_path, encoding: 'utf-8'
  end.join
end