Module: Bashly::Renderable

Included in:
Script::Base, Script::Wrapper
Defined in:
lib/bashly/concerns/renderable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#render_optionsObject (readonly)

Returns the value of attribute render_options.



5
6
7
# File 'lib/bashly/concerns/renderable.rb', line 5

def render_options
  @render_options
end

Instance Method Details

#load_user_file(file, placeholder: true) ⇒ Object

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.



24
25
26
27
28
29
30
31
32
33
34
35
36
# 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 = {}) ⇒ Object



7
8
9
10
# 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

#stringsObject



12
13
14
# File 'lib/bashly/concerns/renderable.rb', line 12

def strings
  @strings ||= MessageStrings.new
end

#user_file_exist?(file) ⇒ Boolean

Returns true if the user’s source file exists

Returns:

  • (Boolean)


50
51
52
# File 'lib/bashly/concerns/renderable.rb', line 50

def user_file_exist?(file)
  File.exist? user_file_path(file)
end

#user_file_path(file) ⇒ Object

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.



41
42
43
44
45
46
47
# 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) ⇒ Object

Outputs a comment that describes the view unless in production mode



17
18
19
20
# 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