Module: TaxGenerator::ApplicationHelper

Included in:
Application, Destination, FileCreator, Processor, TaxonomyTree
Defined in:
lib/tax_generator/helpers/application_helper.rb

Overview

class that holds the helper methods used in the classes

Class Method Summary collapse

Class Method Details

.create_directories(*args) ⇒ void

This method returns an undefined value.

creates directories from a list of arguments

Parameters:

  • args (Array)

    the arguments that will be used as directory names and will create them



51
52
53
54
55
# File 'lib/tax_generator/helpers/application_helper.rb', line 51

def create_directories(*args)
  args.each do |argument|
    FileUtils.mkdir_p(argument) unless File.directory?(argument)
  end
end

.elements_with_content(element) ⇒ String

returns the text from a nokogiri element by rejecting blank elements

Parameters:

  • element (Nokogiri::Element)

    the nokogiri element that will select only children with content and returns their text

Returns:

  • (String)


16
17
18
19
20
21
22
# File 'lib/tax_generator/helpers/application_helper.rb', line 16

def elements_with_content(element)
  if element.present?
    element.select { |elem| elem.content.present? }.join(&:text)
  else
    element
  end
end

.erb_template(file_path) ⇒ String

Reads a file and interpretes it as ERB

Parameters:

  • file_path (String)

    the file that will be read and interpreted as ERB

Returns:

  • (String)


66
67
68
69
70
# File 'lib/tax_generator/helpers/application_helper.rb', line 66

def erb_template(file_path)
  template = ERB.new(File.read(file_path))
  template.filename = file_path
  template
end

.execute_with_rescuevoid

This method returns an undefined value.

wrapper to execute a block and rescue from exception

See Also:

  • #set_celluloid_exception_handling
  • #rescue_interrupt
  • #log_error


121
122
123
124
125
126
127
# File 'lib/tax_generator/helpers/application_helper.rb', line 121

def execute_with_rescue
  yield if block_given?
rescue Interrupt
  rescue_interrupt
rescue => error
  log_error(error)
end

.format_error(exception) ⇒ String

formats a exception to be displayed on screen

Parameters:

  • exception (Exception)

    the exception that will be formatted and printed on screen

Returns:

  • (String)


106
107
108
109
110
111
# File 'lib/tax_generator/helpers/application_helper.rb', line 106

def format_error(exception)
  message = "#{exception.class} (#{exception.respond_to?(:message) ? exception.message : exception.inspect}):\n"
  message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code)
  message << '  ' << exception.backtrace.join("\n  ") if exception.respond_to?(:backtrace)
  message
end

.log_error(exception) ⇒ String

Displays a error with fatal log level

Parameters:

  • exception (Exception)

    the exception that will be formatted and printed on screen

Returns:

  • (String)

See Also:

  • #format_error
  • #log_message


81
82
83
84
# File 'lib/tax_generator/helpers/application_helper.rb', line 81

def log_error(exception)
  message = format_error(exception)
  log_message(message, log_method: 'fatal')
end

.log_message(message, options = {}) ⇒ String

formats a exception to be displayed on screen

Parameters:

  • message (String)

    the message that will be printed to the log file

  • options (Hash) (defaults to: {})

    the options used to determine how to log the message

Options Hash (options):

  • :log_method (String)

    The log method , by default debug

Returns:

  • (String)


95
96
97
# File 'lib/tax_generator/helpers/application_helper.rb', line 95

def log_message(message, options = {})
  app_logger.send(options.fetch(:log_method, 'debug'), message)
end

.nokogiri_xml(file_path) ⇒ void

This method returns an undefined value.

returns a Nokogiri XML document from a file

Parameters:

  • file_path (String)

    the path to the xml file that will be parsed



40
41
42
# File 'lib/tax_generator/helpers/application_helper.rb', line 40

def nokogiri_xml(file_path)
  Nokogiri::XML(File.open(file_path), nil, 'UTF-8')
end

.rescue_interruptvoid

This method returns an undefined value.

rescues from a interrupt error and shows a message



134
135
136
137
# File 'lib/tax_generator/helpers/application_helper.rb', line 134

def rescue_interrupt
  `stty icanon echo`
  puts "\n Command was cancelled due to an Interrupt error."
end

.rootvoid

This method returns an undefined value.

returns the root path of the gem



29
30
31
# File 'lib/tax_generator/helpers/application_helper.rb', line 29

def root
  File.expand_path(File.dirname(File.dirname(File.dirname(__dir__))))
end