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
-
.create_directories(*args) ⇒ void
creates directories from a list of arguments.
-
.elements_with_content(element) ⇒ String
returns the text from a nokogiri element by rejecting blank elements.
-
.erb_template(file_path) ⇒ String
Reads a file and interpretes it as ERB.
-
.execute_with_rescue ⇒ void
wrapper to execute a block and rescue from exception.
-
.format_error(exception) ⇒ String
formats a exception to be displayed on screen.
-
.log_error(exception) ⇒ String
Displays a error with fatal log level.
-
.log_message(message, options = {}) ⇒ String
formats a exception to be displayed on screen.
-
.nokogiri_xml(file_path) ⇒ void
returns a Nokogiri XML document from a file.
-
.rescue_interrupt ⇒ void
rescues from a interrupt error and shows a message.
-
.root ⇒ void
returns the root path of the gem.
-
.set_celluloid_exception_handling ⇒ void
sets the exception handler for celluloid actors.
Class Method Details
.create_directories(*args) ⇒ void
This method returns an undefined value.
creates directories from a list of arguments
51 52 53 54 55 |
# File 'lib/tax_generator/helpers/application_helper.rb', line 51 def create_directories(*args) args.pmap 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
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
80 81 82 83 84 |
# File 'lib/tax_generator/helpers/application_helper.rb', line 80 def erb_template(file_path) template = ERB.new(File.read(file_path)) template.filename = file_path template end |
.execute_with_rescue ⇒ void
This method returns an undefined value.
wrapper to execute a block and rescue from exception
135 136 137 138 139 140 141 142 |
# File 'lib/tax_generator/helpers/application_helper.rb', line 135 def execute_with_rescue set_celluloid_exception_handling 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
120 121 122 123 124 125 |
# File 'lib/tax_generator/helpers/application_helper.rb', line 120 def format_error(exception) = "#{exception.class} (#{exception.respond_to?(:message) ? exception. : exception.inspect}):\n" << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code) << ' ' << exception.backtrace.join("\n ") if exception.respond_to?(:backtrace) end |
.log_error(exception) ⇒ String
Displays a error with fatal log level
95 96 97 98 |
# File 'lib/tax_generator/helpers/application_helper.rb', line 95 def log_error(exception) = format_error(exception) (, log_method: 'fatal') end |
.log_message(message, options = {}) ⇒ String
formats a exception to be displayed on screen
109 110 111 |
# File 'lib/tax_generator/helpers/application_helper.rb', line 109 def (, = {}) app_logger.send(.fetch(:log_method, 'debug'), ) end |
.nokogiri_xml(file_path) ⇒ void
This method returns an undefined value.
returns a Nokogiri XML document from a file
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_interrupt ⇒ void
This method returns an undefined value.
rescues from a interrupt error and shows a message
149 150 151 152 |
# File 'lib/tax_generator/helpers/application_helper.rb', line 149 def rescue_interrupt `stty icanon echo` puts "\n Command was cancelled due to an Interrupt error." end |
.root ⇒ void
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.(File.dirname(File.dirname(File.dirname(__dir__)))) end |
.set_celluloid_exception_handling ⇒ void
This method returns an undefined value.
sets the exception handler for celluloid actors
63 64 65 66 67 68 69 70 71 |
# File 'lib/tax_generator/helpers/application_helper.rb', line 63 def set_celluloid_exception_handling Celluloid.logger = app_logger Celluloid.task_class = Celluloid::TaskThread Celluloid.exception_handler do |ex| unless ex.is_a?(Interrupt) log_error(ex) end end end |