Module: LucaSupport::View

Included in:
LucaRecord::Base
Defined in:
lib/luca_support/view.rb

Overview

File rendering functionality like HTML, PDF.

Class Method Summary collapse

Class Method Details

.erb2pdf(path) ⇒ Object



19
20
21
# File 'lib/luca_support/view.rb', line 19

def erb2pdf(path)
  html2pdf(render_erb(path))
end

.html2pdf(html_dat) ⇒ Object

Requires wkhtmltopdf command



32
33
34
35
36
# File 'lib/luca_support/view.rb', line 32

def html2pdf(html_dat)
  out, err, stat = Open3.capture3('wkhtmltopdf --enable-local-file-access - -', stdin_data: html_dat)
  STDERR.puts err
  out
end

.nushell(records, mode = :expand, columns = []) ⇒ Object

render mode swithes nushell Viewer www.nushell.sh/commands/categories/viewers.html



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/luca_support/view.rb', line 54

def nushell(records, mode=:expand, columns=[])
  return nil if records.is_a?(String)

  require 'open3'
  select = if columns.empty?
             ''
           else
             '| select --ignore-errors ' + columns.map { |col| col.gsub(/[^a-zA-Z0-9_-]/, '') }.join(' ')
           end
  render = case mode
           when :explore
             'explore'
           when :collapse
             'table -c'
           else
             'table -e'
           end
  Open3.pipeline_w(%(nu -c 'cat - | from json #{select} | #{render}')) { |stdin| stdin.puts JSON.dump(records) }
end

.render_erb(path) ⇒ Object



23
24
25
26
27
# File 'lib/luca_support/view.rb', line 23

def render_erb(path)
  @template_dir = File.dirname(path)
  erb = ERB.new(File.read(path.to_s), trim_mode: '-')
  erb.result(binding)
end

.save_pdf(html_dat, path) ⇒ Object



15
16
17
# File 'lib/luca_support/view.rb', line 15

def save_pdf(html_dat, path)
  File.write(path, html2pdf(html_dat))
end

.search_template(file, dir = 'templates') ⇒ Object

Search existing file and return path under:

  1. ‘templates/’ in Project directory that data resides

  2. ‘templates/’ in Library directory that calls LucaSupport::View#search_template



42
43
44
45
46
47
48
49
# File 'lib/luca_support/view.rb', line 42

def search_template(file, dir = 'templates')
  # TODO: load config
  [CONST.pjdir, lib_path].each do |base|
    path = (Pathname(base) / dir / file)
    return path.to_path if path.file?
  end
  nil
end