Class: TaxGenerator::Application

Inherits:
Object
  • Object
show all
Includes:
ApplicationHelper
Defined in:
lib/tax_generator/application.rb

Overview

class used run the processsor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApplicationHelper

create_directories, elements_with_content, erb_template, execute_with_rescue, format_error, log_error, log_message, nokogiri_xml, rescue_interrupt, root

Constructor Details

#initializevoid

method used to initialize the application, by initializing the options for command line access



20
21
22
23
# File 'lib/tax_generator/application.rb', line 20

def initialize
  @opts = Slop::Options.new
  @opts.banner = 'usage: tax_generator [options] ...'
end

Instance Attribute Details

#optsSlop::Options

Returns the options that are used for command line access.

Returns:

  • (Slop::Options)

    the options that are used for command line access



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/tax_generator/application.rb', line 8

class Application
  include TaxGenerator::ApplicationHelper

  attr_reader :opts

  # method used to initialize the application, by initializing the options for command line
  # access
  #
  #
  # @return [void]
  #
  # @api public
  def initialize
    @opts = Slop::Options.new
    @opts.banner = 'usage: tax_generator [options] ...'
  end

  #  receives a list of options that are used to determine the input files and output and input folders
  #
  # @param  [Hash]  options the options that are used for command line access
  #
  # @see #execute_with_rescue
  # @see TaxGenerator::Processor#new
  # @see TaxGenerator::Processor#work
  #
  # @return [void]
  #
  # @api public
  def run(options = {})
    execute_with_rescue do
      options = options.present? ? options : parse_options
      processor = TaxGenerator::Processor.new(options)
      processor.work
    end
  end

  #  returns the logger used to log messages and errors
  #
  # @return [Logger]
  #
  # @api public
  def self.app_logger
    @logger ||= Logger.new($stdout)
  end

  #  returns the options list as a hash after parsing command line arguments
  #
  # @return [Hash]
  #
  # @api public
  def parse_options
    setup_command_line_options
    parser = Slop::Parser.new(@opts)
    result = parser.parse(ARGV.dup)
    result.to_hash
  end

  #  sets the directory options for command line access
  #
  # @return [void]
  #
  # @api public
  def directory_options
    @opts.separator ''
    @opts.separator 'Directory options:'
    @opts.string '-i', '--input_dir', 'The input directory ', default: "#{root}/data/input"
    @opts.string '-o', '--output_dir', 'The ouput directory', default: "#{root}/data/output"
  end

  #  sets the file options for command line access
  #
  # @return [void]
  #
  # @api public
  def file_options
    @opts.separator ''
    @opts.separator 'Extra options:'
    @opts.string '-t', '--taxonomy_filename', 'The taxonomy file name', default: 'taxonomy.xml'
    @opts.string '-d', '--destinations_filename', 'The destinations file name', default: 'destinations.xml'
  end

  #  sets the version of the gem available to be displayed from command line
  #
  # @return [void]
  #
  # @api public
  def setup_version
    @opts.on '--version', 'The destinations file name' do
      puts TaxGenerator.gem_version
      exit
    end
  end

  #  sets the help menu for command line access
  #
  # @return [void]
  #
  # @api public
  def setup_help
    @opts.on '--help', 'print the help' do
      puts @opts
      exit
    end
  end

  #  sets the command line options
  #
  # @return [void]
  #
  # @api public
  def setup_command_line_options
    directory_options
    file_options
    setup_version
    setup_help
  end
end

Class Method Details

.app_loggerLogger

returns the logger used to log messages and errors

Returns:

  • (Logger)


49
50
51
# File 'lib/tax_generator/application.rb', line 49

def self.app_logger
  @logger ||= Logger.new($stdout)
end

Instance Method Details

#directory_optionsvoid

This method returns an undefined value.

sets the directory options for command line access



70
71
72
73
74
75
# File 'lib/tax_generator/application.rb', line 70

def directory_options
  @opts.separator ''
  @opts.separator 'Directory options:'
  @opts.string '-i', '--input_dir', 'The input directory ', default: "#{root}/data/input"
  @opts.string '-o', '--output_dir', 'The ouput directory', default: "#{root}/data/output"
end

#file_optionsvoid

This method returns an undefined value.

sets the file options for command line access



82
83
84
85
86
87
# File 'lib/tax_generator/application.rb', line 82

def file_options
  @opts.separator ''
  @opts.separator 'Extra options:'
  @opts.string '-t', '--taxonomy_filename', 'The taxonomy file name', default: 'taxonomy.xml'
  @opts.string '-d', '--destinations_filename', 'The destinations file name', default: 'destinations.xml'
end

#parse_optionsHash

returns the options list as a hash after parsing command line arguments

Returns:

  • (Hash)


58
59
60
61
62
63
# File 'lib/tax_generator/application.rb', line 58

def parse_options
  setup_command_line_options
  parser = Slop::Parser.new(@opts)
  result = parser.parse(ARGV.dup)
  result.to_hash
end

#run(options = {}) ⇒ void

This method returns an undefined value.

receives a list of options that are used to determine the input files and output and input folders

Parameters:

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

    the options that are used for command line access

See Also:

  • TaxGenerator::ApplicationHelper#execute_with_rescue
  • Processor#new
  • Processor#work


36
37
38
39
40
41
42
# File 'lib/tax_generator/application.rb', line 36

def run(options = {})
  execute_with_rescue do
    options = options.present? ? options : parse_options
    processor = TaxGenerator::Processor.new(options)
    processor.work
  end
end

#setup_command_line_optionsvoid

This method returns an undefined value.

sets the command line options



118
119
120
121
122
123
# File 'lib/tax_generator/application.rb', line 118

def setup_command_line_options
  directory_options
  file_options
  setup_version
  setup_help
end

#setup_helpvoid

This method returns an undefined value.

sets the help menu for command line access



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

def setup_help
  @opts.on '--help', 'print the help' do
    puts @opts
    exit
  end
end

#setup_versionvoid

This method returns an undefined value.

sets the version of the gem available to be displayed from command line



94
95
96
97
98
99
# File 'lib/tax_generator/application.rb', line 94

def setup_version
  @opts.on '--version', 'The destinations file name' do
    puts TaxGenerator.gem_version
    exit
  end
end