module Bashly::Renderable

Attributes

render_options[R]

Public Instance Methods

load_user_file(file, placeholder: true) click to toggle source

Reads a file from the userspace (Settings.source_dir) and returns its contents. If the file is not found, returns a string with a hint.

# File lib/bashly/concerns/renderable.rb, line 24
def load_user_file(file, placeholder: true)
  path = user_file_path file

  content = if File.exist? path
    File.read(path).remove_front_matter
  elsif placeholder
    'echo "error: cannot load file"'
  else
    ''
  end

  Settings.production? ? content : "#{view_marker path}\n#{content}"
end
render(view, render_options = {}) click to toggle source
# File lib/bashly/concerns/renderable.rb, line 7
def render(view, render_options = {})
  @render_options = render_options
  GTX.render_file view_path(view), context: binding, filename: "#{views_subfolder}.#{view}"
end
strings() click to toggle source
# File lib/bashly/concerns/renderable.rb, line 12
def strings
  @strings ||= MessageStrings.new
end
user_file_exist?(file) click to toggle source

Returns true if the user’s source file exists

# File lib/bashly/concerns/renderable.rb, line 50
def user_file_exist?(file)
  File.exist? user_file_path(file)
end
user_file_path(file) click to toggle source

Returns a path to a file in the user’s source_dir. The file argument should either be without exteneion, or with the user’s configured partials_extension.

# File lib/bashly/concerns/renderable.rb, line 41
def user_file_path(file)
  path = "#{Settings.source_dir}/#{file}"
  ext = ".#{Settings.partials_extension}"
  return path if path.end_with? ext

  "#{path}#{ext}"
end
view_marker(id = nil) click to toggle source

Outputs a comment that describes the view unless in production mode

# File lib/bashly/concerns/renderable.rb, line 17
def view_marker(id = nil)
  id ||= ":#{caller_locations(1..1).first.path}"
  "# #{id}" unless Settings.production?
end

Private Instance Methods

base_views_path() click to toggle source
# File lib/bashly/concerns/renderable.rb, line 64
def base_views_path
  @base_views_path ||= File.expand_path '../views/', __dir__
end
self_views_path() click to toggle source
# File lib/bashly/concerns/renderable.rb, line 60
def self_views_path
  @self_views_path ||= "#{base_views_path}/#{views_subfolder}"
end
view_path(view) click to toggle source
# File lib/bashly/concerns/renderable.rb, line 56
def view_path(view)
  "#{self_views_path}/#{view}.gtx"
end
views_subfolder() click to toggle source
# File lib/bashly/concerns/renderable.rb, line 68
def views_subfolder
  @views_subfolder ||= self.class.name.split('::').last.to_underscore
end