Class: TaxGenerator::FileCreator

Inherits:
Concurrent::Actor::RestartingContext
  • Object
show all
Includes:
Concurrent::Async, MethodicActor, ApplicationHelper
Defined in:
lib/tax_generator/classes/file_creator.rb

Overview

class used to create the files

Instance Attribute 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

Methods included from MethodicActor

#on_message

Constructor Details

#initialize(*args) ⇒ void

processes the job received and registers itself inside the manager

Parameters:

  • job (Hash)

    the job that is passed to the current actor

  • manager (TaxGenerator::Processor)

    the manager that manages the actor

See Also:



44
45
46
47
48
49
50
51
# File 'lib/tax_generator/classes/file_creator.rb', line 44

def initialize(*args)
  job = args[0]
  @processor = args[1]
  job = job.stringify_keys
  @job = job
  process_job(job)
  processor.register_worker_for_job(job, self)
end

Instance Attribute Details

#destinationNokogiri::Element

Returns the destination node from the xml document.

Returns:

  • (Nokogiri::Element)

    the destination node from the xml document



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
# File 'lib/tax_generator/classes/file_creator.rb', line 18

class FileCreator < Concurrent::Actor::RestartingContext
  include Concurrent::Async
  include MethodicActor
  include TaxGenerator::ApplicationHelper

  attr_reader :processor, :job, :job_id, :destination, :taxonomy, :output_folder

  #  returns the template file path used for generating the files
  #
  # @return [void]
  #
  # @api public
  def template_name
    File.join(root, 'templates', 'template.html.erb')
  end

  #  processes the job received and registers itself inside the manager
  # @see TaxGenerator::Processor#register_worker_for_job
  # @see #process_job
  #
  # @param  [Hash] job the job that is passed to the current actor
  # @param  [TaxGenerator::Processor] manager the manager that manages the actor
  #
  # @return [void]
  #
  # @api public
  def initialize(*args)
    job = args[0]
    @processor = args[1]
    job = job.stringify_keys
    @job = job
    process_job(job)
    processor.register_worker_for_job(job, self)
  end

  #  processes the job information by retrieving keys from the hash
  #
  # @param  [Hash] job the job that is passed to the current actor
  #
  # @return [void]
  #
  # @api public
  def process_job(job)
    job = job.stringify_keys
    @destination = job['destination']
    @job_id = job['atlas_id']
    @taxonomy = job['taxonomy']
    @output_folder = job['output_folder']
  end

  #  finds all the nodes in the tree with the given name
  #
  # @return [Array<Tree::TreeNode>]
  #
  # @api public
  def atlas_node
    @taxonomy.find_by_name(@job_id).first
  end

  #  renders the template and creates new file with the template html
  #
  # @return [void]
  #
  # @api public
  def start_work
    log_message "Generating html for destination #{@job_id}"
    output = Tilt.new(template_name).render(nil, fetch_atlas_details)
    File.open(File.join(@output_folder, "#{@job_id}.html"), 'w') { |file| file << output }
    mark_job_completed
  end

  #  marks the job as completed after file is generated
  #
  # @return [void]
  #
  # @api public
  def mark_job_completed
    @processor.jobs[@job_id]['status'] = 'finished'
  end

  # fetches the details needed to be passed to the erb template
  # @see TaxGenerator::Destination#new
  #
  # @return [void]
  #
  # @api public
  def fetch_atlas_details
    content = @destination.present? ? TaxGenerator::Destination.new(@destination).to_hash : {}
    content.merge(details: atlas_node)
  end
end

#jobHash

Returns the job that this actor received.

Returns:

  • (Hash)

    the job that this actor received



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
# File 'lib/tax_generator/classes/file_creator.rb', line 18

class FileCreator < Concurrent::Actor::RestartingContext
  include Concurrent::Async
  include MethodicActor
  include TaxGenerator::ApplicationHelper

  attr_reader :processor, :job, :job_id, :destination, :taxonomy, :output_folder

  #  returns the template file path used for generating the files
  #
  # @return [void]
  #
  # @api public
  def template_name
    File.join(root, 'templates', 'template.html.erb')
  end

  #  processes the job received and registers itself inside the manager
  # @see TaxGenerator::Processor#register_worker_for_job
  # @see #process_job
  #
  # @param  [Hash] job the job that is passed to the current actor
  # @param  [TaxGenerator::Processor] manager the manager that manages the actor
  #
  # @return [void]
  #
  # @api public
  def initialize(*args)
    job = args[0]
    @processor = args[1]
    job = job.stringify_keys
    @job = job
    process_job(job)
    processor.register_worker_for_job(job, self)
  end

  #  processes the job information by retrieving keys from the hash
  #
  # @param  [Hash] job the job that is passed to the current actor
  #
  # @return [void]
  #
  # @api public
  def process_job(job)
    job = job.stringify_keys
    @destination = job['destination']
    @job_id = job['atlas_id']
    @taxonomy = job['taxonomy']
    @output_folder = job['output_folder']
  end

  #  finds all the nodes in the tree with the given name
  #
  # @return [Array<Tree::TreeNode>]
  #
  # @api public
  def atlas_node
    @taxonomy.find_by_name(@job_id).first
  end

  #  renders the template and creates new file with the template html
  #
  # @return [void]
  #
  # @api public
  def start_work
    log_message "Generating html for destination #{@job_id}"
    output = Tilt.new(template_name).render(nil, fetch_atlas_details)
    File.open(File.join(@output_folder, "#{@job_id}.html"), 'w') { |file| file << output }
    mark_job_completed
  end

  #  marks the job as completed after file is generated
  #
  # @return [void]
  #
  # @api public
  def mark_job_completed
    @processor.jobs[@job_id]['status'] = 'finished'
  end

  # fetches the details needed to be passed to the erb template
  # @see TaxGenerator::Destination#new
  #
  # @return [void]
  #
  # @api public
  def fetch_atlas_details
    content = @destination.present? ? TaxGenerator::Destination.new(@destination).to_hash : {}
    content.merge(details: atlas_node)
  end
end

#job_idString

Returns the id of the node from the taxonomy tree.

Returns:

  • (String)

    the id of the node from the taxonomy tree



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
# File 'lib/tax_generator/classes/file_creator.rb', line 18

class FileCreator < Concurrent::Actor::RestartingContext
  include Concurrent::Async
  include MethodicActor
  include TaxGenerator::ApplicationHelper

  attr_reader :processor, :job, :job_id, :destination, :taxonomy, :output_folder

  #  returns the template file path used for generating the files
  #
  # @return [void]
  #
  # @api public
  def template_name
    File.join(root, 'templates', 'template.html.erb')
  end

  #  processes the job received and registers itself inside the manager
  # @see TaxGenerator::Processor#register_worker_for_job
  # @see #process_job
  #
  # @param  [Hash] job the job that is passed to the current actor
  # @param  [TaxGenerator::Processor] manager the manager that manages the actor
  #
  # @return [void]
  #
  # @api public
  def initialize(*args)
    job = args[0]
    @processor = args[1]
    job = job.stringify_keys
    @job = job
    process_job(job)
    processor.register_worker_for_job(job, self)
  end

  #  processes the job information by retrieving keys from the hash
  #
  # @param  [Hash] job the job that is passed to the current actor
  #
  # @return [void]
  #
  # @api public
  def process_job(job)
    job = job.stringify_keys
    @destination = job['destination']
    @job_id = job['atlas_id']
    @taxonomy = job['taxonomy']
    @output_folder = job['output_folder']
  end

  #  finds all the nodes in the tree with the given name
  #
  # @return [Array<Tree::TreeNode>]
  #
  # @api public
  def atlas_node
    @taxonomy.find_by_name(@job_id).first
  end

  #  renders the template and creates new file with the template html
  #
  # @return [void]
  #
  # @api public
  def start_work
    log_message "Generating html for destination #{@job_id}"
    output = Tilt.new(template_name).render(nil, fetch_atlas_details)
    File.open(File.join(@output_folder, "#{@job_id}.html"), 'w') { |file| file << output }
    mark_job_completed
  end

  #  marks the job as completed after file is generated
  #
  # @return [void]
  #
  # @api public
  def mark_job_completed
    @processor.jobs[@job_id]['status'] = 'finished'
  end

  # fetches the details needed to be passed to the erb template
  # @see TaxGenerator::Destination#new
  #
  # @return [void]
  #
  # @api public
  def fetch_atlas_details
    content = @destination.present? ? TaxGenerator::Destination.new(@destination).to_hash : {}
    content.merge(details: atlas_node)
  end
end

#output_folderString

Returns the output folder where the new files will be created.

Returns:

  • (String)

    the output folder where the new files will be created



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
# File 'lib/tax_generator/classes/file_creator.rb', line 18

class FileCreator < Concurrent::Actor::RestartingContext
  include Concurrent::Async
  include MethodicActor
  include TaxGenerator::ApplicationHelper

  attr_reader :processor, :job, :job_id, :destination, :taxonomy, :output_folder

  #  returns the template file path used for generating the files
  #
  # @return [void]
  #
  # @api public
  def template_name
    File.join(root, 'templates', 'template.html.erb')
  end

  #  processes the job received and registers itself inside the manager
  # @see TaxGenerator::Processor#register_worker_for_job
  # @see #process_job
  #
  # @param  [Hash] job the job that is passed to the current actor
  # @param  [TaxGenerator::Processor] manager the manager that manages the actor
  #
  # @return [void]
  #
  # @api public
  def initialize(*args)
    job = args[0]
    @processor = args[1]
    job = job.stringify_keys
    @job = job
    process_job(job)
    processor.register_worker_for_job(job, self)
  end

  #  processes the job information by retrieving keys from the hash
  #
  # @param  [Hash] job the job that is passed to the current actor
  #
  # @return [void]
  #
  # @api public
  def process_job(job)
    job = job.stringify_keys
    @destination = job['destination']
    @job_id = job['atlas_id']
    @taxonomy = job['taxonomy']
    @output_folder = job['output_folder']
  end

  #  finds all the nodes in the tree with the given name
  #
  # @return [Array<Tree::TreeNode>]
  #
  # @api public
  def atlas_node
    @taxonomy.find_by_name(@job_id).first
  end

  #  renders the template and creates new file with the template html
  #
  # @return [void]
  #
  # @api public
  def start_work
    log_message "Generating html for destination #{@job_id}"
    output = Tilt.new(template_name).render(nil, fetch_atlas_details)
    File.open(File.join(@output_folder, "#{@job_id}.html"), 'w') { |file| file << output }
    mark_job_completed
  end

  #  marks the job as completed after file is generated
  #
  # @return [void]
  #
  # @api public
  def mark_job_completed
    @processor.jobs[@job_id]['status'] = 'finished'
  end

  # fetches the details needed to be passed to the erb template
  # @see TaxGenerator::Destination#new
  #
  # @return [void]
  #
  # @api public
  def fetch_atlas_details
    content = @destination.present? ? TaxGenerator::Destination.new(@destination).to_hash : {}
    content.merge(details: atlas_node)
  end
end

#processorTaxGenerator::Processor

Returns the manager that manages the current actor.

Returns:



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
# File 'lib/tax_generator/classes/file_creator.rb', line 18

class FileCreator < Concurrent::Actor::RestartingContext
  include Concurrent::Async
  include MethodicActor
  include TaxGenerator::ApplicationHelper

  attr_reader :processor, :job, :job_id, :destination, :taxonomy, :output_folder

  #  returns the template file path used for generating the files
  #
  # @return [void]
  #
  # @api public
  def template_name
    File.join(root, 'templates', 'template.html.erb')
  end

  #  processes the job received and registers itself inside the manager
  # @see TaxGenerator::Processor#register_worker_for_job
  # @see #process_job
  #
  # @param  [Hash] job the job that is passed to the current actor
  # @param  [TaxGenerator::Processor] manager the manager that manages the actor
  #
  # @return [void]
  #
  # @api public
  def initialize(*args)
    job = args[0]
    @processor = args[1]
    job = job.stringify_keys
    @job = job
    process_job(job)
    processor.register_worker_for_job(job, self)
  end

  #  processes the job information by retrieving keys from the hash
  #
  # @param  [Hash] job the job that is passed to the current actor
  #
  # @return [void]
  #
  # @api public
  def process_job(job)
    job = job.stringify_keys
    @destination = job['destination']
    @job_id = job['atlas_id']
    @taxonomy = job['taxonomy']
    @output_folder = job['output_folder']
  end

  #  finds all the nodes in the tree with the given name
  #
  # @return [Array<Tree::TreeNode>]
  #
  # @api public
  def atlas_node
    @taxonomy.find_by_name(@job_id).first
  end

  #  renders the template and creates new file with the template html
  #
  # @return [void]
  #
  # @api public
  def start_work
    log_message "Generating html for destination #{@job_id}"
    output = Tilt.new(template_name).render(nil, fetch_atlas_details)
    File.open(File.join(@output_folder, "#{@job_id}.html"), 'w') { |file| file << output }
    mark_job_completed
  end

  #  marks the job as completed after file is generated
  #
  # @return [void]
  #
  # @api public
  def mark_job_completed
    @processor.jobs[@job_id]['status'] = 'finished'
  end

  # fetches the details needed to be passed to the erb template
  # @see TaxGenerator::Destination#new
  #
  # @return [void]
  #
  # @api public
  def fetch_atlas_details
    content = @destination.present? ? TaxGenerator::Destination.new(@destination).to_hash : {}
    content.merge(details: atlas_node)
  end
end

#taxonomyTaxGenerator::TaxonomyTree

Returns the taxonomy tree holding all the nodes from the taxonomy xml document.

Returns:



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
# File 'lib/tax_generator/classes/file_creator.rb', line 18

class FileCreator < Concurrent::Actor::RestartingContext
  include Concurrent::Async
  include MethodicActor
  include TaxGenerator::ApplicationHelper

  attr_reader :processor, :job, :job_id, :destination, :taxonomy, :output_folder

  #  returns the template file path used for generating the files
  #
  # @return [void]
  #
  # @api public
  def template_name
    File.join(root, 'templates', 'template.html.erb')
  end

  #  processes the job received and registers itself inside the manager
  # @see TaxGenerator::Processor#register_worker_for_job
  # @see #process_job
  #
  # @param  [Hash] job the job that is passed to the current actor
  # @param  [TaxGenerator::Processor] manager the manager that manages the actor
  #
  # @return [void]
  #
  # @api public
  def initialize(*args)
    job = args[0]
    @processor = args[1]
    job = job.stringify_keys
    @job = job
    process_job(job)
    processor.register_worker_for_job(job, self)
  end

  #  processes the job information by retrieving keys from the hash
  #
  # @param  [Hash] job the job that is passed to the current actor
  #
  # @return [void]
  #
  # @api public
  def process_job(job)
    job = job.stringify_keys
    @destination = job['destination']
    @job_id = job['atlas_id']
    @taxonomy = job['taxonomy']
    @output_folder = job['output_folder']
  end

  #  finds all the nodes in the tree with the given name
  #
  # @return [Array<Tree::TreeNode>]
  #
  # @api public
  def atlas_node
    @taxonomy.find_by_name(@job_id).first
  end

  #  renders the template and creates new file with the template html
  #
  # @return [void]
  #
  # @api public
  def start_work
    log_message "Generating html for destination #{@job_id}"
    output = Tilt.new(template_name).render(nil, fetch_atlas_details)
    File.open(File.join(@output_folder, "#{@job_id}.html"), 'w') { |file| file << output }
    mark_job_completed
  end

  #  marks the job as completed after file is generated
  #
  # @return [void]
  #
  # @api public
  def mark_job_completed
    @processor.jobs[@job_id]['status'] = 'finished'
  end

  # fetches the details needed to be passed to the erb template
  # @see TaxGenerator::Destination#new
  #
  # @return [void]
  #
  # @api public
  def fetch_atlas_details
    content = @destination.present? ? TaxGenerator::Destination.new(@destination).to_hash : {}
    content.merge(details: atlas_node)
  end
end

Instance Method Details

#atlas_nodeArray<Tree::TreeNode>

finds all the nodes in the tree with the given name

Returns:

  • (Array<Tree::TreeNode>)


73
74
75
# File 'lib/tax_generator/classes/file_creator.rb', line 73

def atlas_node
  @taxonomy.find_by_name(@job_id).first
end

#fetch_atlas_detailsvoid

This method returns an undefined value.

fetches the details needed to be passed to the erb template

See Also:

  • Destination#new


104
105
106
107
# File 'lib/tax_generator/classes/file_creator.rb', line 104

def fetch_atlas_details
  content = @destination.present? ? TaxGenerator::Destination.new(@destination).to_hash : {}
  content.merge(details: atlas_node)
end

#mark_job_completedvoid

This method returns an undefined value.

marks the job as completed after file is generated



94
95
96
# File 'lib/tax_generator/classes/file_creator.rb', line 94

def mark_job_completed
  @processor.jobs[@job_id]['status'] = 'finished'
end

#process_job(job) ⇒ void

This method returns an undefined value.

processes the job information by retrieving keys from the hash

Parameters:

  • job (Hash)

    the job that is passed to the current actor



60
61
62
63
64
65
66
# File 'lib/tax_generator/classes/file_creator.rb', line 60

def process_job(job)
  job = job.stringify_keys
  @destination = job['destination']
  @job_id = job['atlas_id']
  @taxonomy = job['taxonomy']
  @output_folder = job['output_folder']
end

#start_workvoid

This method returns an undefined value.

renders the template and creates new file with the template html



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

def start_work
  log_message "Generating html for destination #{@job_id}"
  output = Tilt.new(template_name).render(nil, fetch_atlas_details)
  File.open(File.join(@output_folder, "#{@job_id}.html"), 'w') { |file| file << output }
  mark_job_completed
end

#template_namevoid

This method returns an undefined value.

returns the template file path used for generating the files



30
31
32
# File 'lib/tax_generator/classes/file_creator.rb', line 30

def template_name
  File.join(root, 'templates', 'template.html.erb')
end