module TaxGenerator::ApplicationHelper
class that holds the helper methods used in the classes
Public Instance Methods
creates directories from a list of arguments
@param [Array] args the arguments that will be used as directory names and will create them
@return [void]
@api public
# 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
returns the text from a nokogiri element by rejecting blank elements
@param [Nokogiri::Element] element the nokogiri element that will select only children with content and returns their text
@return [String]
@api public
# 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
Reads a file and interpretes it as ERB
@param [String] file_path the file that will be read and interpreted as ERB
@return [String]
@api public
# 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
wrapper to execute a block and rescue from exception
@see set_celluloid_exception_handling @see rescue_interrupt
@see log_error
@return [void]
@api public
# 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
formats a exception to be displayed on screen
@param [Exception] exception the exception that will be formatted and printed on screen
@return [String]
@api public
# 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
Displays a error with fatal log level @see format_error
@see log_message
@param [Exception] exception the exception that will be formatted and printed on screen
@return [String]
@api public
# 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
formats a exception to be displayed on screen
@param [String] message the message that will be printed to the log file @param [Hash] options the options used to determine how to log the message @option options [String] :log_method The log method , by default debug
@return [String]
@api public
# 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
returns a Nokogiri XML document from a file
@param [String] file_path the path to the xml file that will be parsed
@return [void]
@api public
# 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
rescues from a interrupt error and shows a message
@return [void]
@api public
# 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
returns the root path of the gem
@return [void]
@api public
# File lib/tax_generator/helpers/application_helper.rb, line 29 def root File.expand_path(File.dirname(File.dirname(File.dirname(__dir__)))) end