Class: CMSScanner::Formatter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cms_scanner/formatter.rb

Overview

Base Formatter

Direct Known Subclasses

Cli, Json

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#controller_name ⇒ Object (readonly)

Returns the value of attribute controller_name.



42
43
44
# File 'lib/cms_scanner/formatter.rb', line 42

def controller_name
  @controller_name
end

Instance Method Details

#base_format ⇒ String

Returns The underscored format to use as a base.

Returns:

  • (String) —

    The underscored format to use as a base



50
# File 'lib/cms_scanner/formatter.rb', line 50

def base_format; end

#beautify ⇒ Object

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



60
# File 'lib/cms_scanner/formatter.rb', line 60

def beautify; end

#format ⇒ String

Returns The underscored name of the class.

Returns:

  • (String) —

    The underscored name of the class



45
46
47
# File 'lib/cms_scanner/formatter.rb', line 45

def format
  self.class.name.demodulize.underscore
end

#formats ⇒ Array<String>

Returns:

  • (Array<String>)


53
54
55
# File 'lib/cms_scanner/formatter.rb', line 53

def formats
  [format, base_format].compact
end

#output(tpl, vars = {}, controller_name = nil) ⇒ Object

See Also:



63
64
65
# File 'lib/cms_scanner/formatter.rb', line 63

def output(tpl, vars = {}, controller_name = nil)
  puts render(tpl, vars, controller_name)
end

#render(tpl, vars = {}, controller_name = nil) ⇒ Object

Parameters:

  • tpl (String)
  • vars (Hash) (defaults to: {})
  • controller_name (String) (defaults to: nil)


70
71
72
73
74
75
76
77
# File 'lib/cms_scanner/formatter.rb', line 70

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
  ERB.new(File.read(view_path(tpl)), nil, '-').result(binding)
end

#template_vars(vars) ⇒ Void

Parameters:

  • vars (Hash)

Returns:

  • (Void)


82
83
84
85
86
# File 'lib/cms_scanner/formatter.rb', line 82

def template_vars(vars)
  vars.each do |key, value|
    instance_variable_set("@#{key}", value) unless key == :views_directories
  end
end

#view_path(tpl) ⇒ String

Returns The path of the view.

Parameters:

  • tpl (String)

Returns:

  • (String) —

    The path of the view



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cms_scanner/formatter.rb', line 91

def view_path(tpl)
  if tpl[0, 1] == '@' # Global Template
    tpl = tpl.delete('@')
  else
    fail 'The controller_name can not be nil' unless controller_name
    tpl = "#{controller_name}/#{tpl}"
  end

  fail "Wrong tpl format: '#{tpl}'" unless tpl =~ /\A[\w\/_]+\z/

  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

  fail "View not found for #{format}/#{tpl}"
end

#views_directories ⇒ Array<String>

Returns The directories to look into for views.

Returns:

  • (Array<String>) —

    The directories to look into for views



113
114
115
# File 'lib/cms_scanner/formatter.rb', line 113

def views_directories
  @views_directories ||= [Pathname.new(APP_DIR).join('views').to_s]
end