class CMSScanner::Formatter::Base

Base Formatter

Constants

ERB_SUPPORTS_KVARGS

Attributes

controller_name[R]

Public Class Methods

new() click to toggle source
# File lib/cms_scanner/formatter.rb, line 54
def initialize
  # Can't put this at the top level of the class, due to the NS::
  extend NS::Formatter::InstanceMethods
end

Public Instance Methods

base_format() click to toggle source

@return [ String ] The underscored format to use as a base

# File lib/cms_scanner/formatter.rb, line 70
def base_format; end
beautify() click to toggle source

This is called after the scan and used in some formatters (e.g JSON) to indent results

# File lib/cms_scanner/formatter.rb, line 80
def beautify; end
format() click to toggle source

@return [ String ] The underscored name of the class

# File lib/cms_scanner/formatter.rb, line 60
def format
  self.class.name.demodulize.underscore
end
formats() click to toggle source

@return [ Array<String> ]

# File lib/cms_scanner/formatter.rb, line 73
def formats
  [format, base_format].compact
end
output(tpl, vars = {}, controller_name = nil) click to toggle source

@see render

# File lib/cms_scanner/formatter.rb, line 83
def output(tpl, vars = {}, controller_name = nil)
  puts render(tpl, vars, controller_name)
end
render(tpl, vars = {}, controller_name = nil) click to toggle source

@param [ String ] tpl @param [ Hash ] vars @param [ String ] controller_name

# File lib/cms_scanner/formatter.rb, line 92
def render(tpl, vars = {}, controller_name = nil)
  template_vars(vars)
  @controller_name = controller_name if controller_name

  # '-' is used to disable new lines when -%> is used
  # See http://www.ruby-doc.org/stdlib-2.1.1/libdoc/erb/rdoc/ERB.html
  # Since ruby 2.6, KVARGS are supported and passing argument is deprecated in ruby 3+
  if ERB_SUPPORTS_KVARGS
    ERB.new(File.read(view_path(tpl)), trim_mode: '-').result(binding)
  else
    ERB.new(File.read(view_path(tpl)), nil, '-').result(binding)
  end
end
template_vars(vars) click to toggle source

@param [ Hash ] vars

@return [ Void ]

# File lib/cms_scanner/formatter.rb, line 109
def template_vars(vars)
  vars.each do |key, value|
    instance_variable_set("@#{key}", value) unless key == :views_directories
  end
end
user_interaction?() click to toggle source

@return [ Boolean ]

# File lib/cms_scanner/formatter.rb, line 65
def user_interaction?
  format == 'cli'
end
view_path(tpl) click to toggle source

@param [ String ] tpl

@return [ String ] The path of the view

# File lib/cms_scanner/formatter.rb, line 118
def view_path(tpl)
  if tpl[0, 1] == '@' # Global Template
    tpl = tpl.delete('@')
  else
    raise 'The controller_name can not be nil' unless controller_name

    tpl = "#{controller_name}/#{tpl}"
  end

  raise "Wrong tpl format: '#{tpl}'" unless %r{\A[\w/_]+\z}.match?(tpl)

  views_directories.reverse_each do |dir|
    formats.each do |format|
      potential_file = File.join(dir, format, "#{tpl}.erb")

      return potential_file if File.exist?(potential_file)
    end
  end

  raise "View not found for #{format}/#{tpl}"
end
views_directories() click to toggle source

@return [ Array<String> ] The directories to look into for views

# File lib/cms_scanner/formatter.rb, line 141
def views_directories
  @views_directories ||= [
    APP_DIR, NS::APP_DIR,
    File.join(Dir.home, ".#{NS.app_name}"), File.join(Dir.pwd, ".#{NS.app_name}")
  ].uniq.reduce([]) { |acc, elem| acc << Pathname.new(elem).join('views').to_s }
end