module Bashly::Renderable
Attributes
Public Instance Methods
Source
# 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.enabled?(:view_markers) ? "#{view_marker path}\n#{content}" : content end
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.
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
Source
# File lib/bashly/concerns/renderable.rb, line 12 def strings @strings ||= MessageStrings.new end
Source
# File lib/bashly/concerns/renderable.rb, line 50 def user_file_exist?(file) File.exist? user_file_path(file) end
Returns true if the user’s source file exists
Source
# 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
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.
Source
# File lib/bashly/concerns/renderable.rb, line 56 def user_string(text, indent: 0) wrap = Settings.word_wrap - indent text.wrap(wrap).indent(indent).sanitize_for_print end
Returns a wrapped, indented and sanitized string Designed to place help and example messages inside bash’s ‘printf’
Source
# File lib/bashly/concerns/renderable.rb, line 17 def view_marker(id = nil) id ||= ":#{caller_locations(1..1).first.path}" "# #{id}" if Settings.enabled? :view_markers end
Outputs a comment that describes the view unless in production mode
Private Instance Methods
Source
# File lib/bashly/concerns/renderable.rb, line 71 def base_views_path @base_views_path ||= File.expand_path '../views/', __dir__ end
Source
# File lib/bashly/concerns/renderable.rb, line 67 def self_views_path @self_views_path ||= "#{base_views_path}/#{views_subfolder}" end
Source
# File lib/bashly/concerns/renderable.rb, line 63 def view_path(view) "#{self_views_path}/#{view}.gtx" end
Source
# File lib/bashly/concerns/renderable.rb, line 75 def views_subfolder @views_subfolder ||= self.class.name.split('::').last.to_underscore end