Class: MaintenanceTasks::TaskData Private
- Inherits:
-
Object
- Object
- MaintenanceTasks::TaskData
- Defined in:
- app/models/maintenance_tasks/task_data.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Class that represents the data related to a Task. Such information can be sourced from a Task or from existing Run records for a Task that was since deleted.
Instances of this class replace a Task class instance in cases where we don’t need the actual Task subclass.
Instance Attribute Summary collapse
-
#name ⇒ String
(also: #to_s)
readonly
private
The name of the Task.
Class Method Summary collapse
-
.available_tasks ⇒ Array<TaskData>
private
Returns a list of sorted Task Data objects that represent the available Tasks.
-
.find(name) ⇒ TaskData
private
Initializes a Task Data by name, raising if the Task does not exist.
Instance Method Summary collapse
-
#category ⇒ Symbol
private
Retrieves the Task’s category, which is one of active, new, or completed.
-
#code ⇒ String?
private
The Task’s source code.
-
#csv_task? ⇒ Boolean
private
Whether the Task inherits from CsvTask.
-
#deleted? ⇒ Boolean
private
Whether the Task has been deleted.
-
#initialize(name, last_run = :none_passed) ⇒ TaskData
constructor
private
Initializes a Task Data with a name and optionally a last_run.
-
#last_run ⇒ MaintenanceTasks::Run?
private
Retrieves the latest Run associated with the Task.
- #new ⇒ MaintenanceTasks::Task? private
-
#parameter_names ⇒ Array<String>
private
The names of parameters the Task accepts.
-
#previous_runs ⇒ ActiveRecord::Relation<MaintenanceTasks::Run>
private
Returns the set of Run records associated with the Task previous to the last Run.
-
#status ⇒ String
private
The Task status.
Constructor Details
#initialize(name, last_run = :none_passed) ⇒ TaskData
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes a Task Data with a name and optionally a last_run.
60 61 62 63 |
# File 'app/models/maintenance_tasks/task_data.rb', line 60 def initialize(name, last_run = :none_passed) @name = name @last_run = last_run unless last_run == :none_passed end |
Instance Attribute Details
#name ⇒ String (readonly) Also known as: to_s
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the name of the Task.
66 67 68 |
# File 'app/models/maintenance_tasks/task_data.rb', line 66 def name @name end |
Class Method Details
.available_tasks ⇒ Array<TaskData>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a list of sorted Task Data objects that represent the available Tasks.
Tasks are sorted by category, and within a category, by Task name. Determining a Task’s category require its latest Run record. To optimize calls to the database, a single query is done to get the last Run for each Task, and Task Data instances are initialized with these last_run values.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/models/maintenance_tasks/task_data.rb', line 41 def available_tasks task_names = Task.available_tasks.map(&:name) available_task_runs = Run.where(task_name: task_names) last_runs = Run.with_attached_csv.where( id: available_task_runs.select("MAX(id) as id").group(:task_name) ) task_names.map do |task_name| last_run = last_runs.find { |run| run.task_name == task_name } TaskData.new(task_name, last_run) end.sort_by!(&:name) end |
.find(name) ⇒ TaskData
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes a Task Data by name, raising if the Task does not exist.
For the purpose of this method, a Task does not exist if it’s deleted and doesn’t have a Run. While technically, it could have existed and been deleted since, if it never had a Run we may as well consider it non-existent since we don’t have interesting data to show.
25 26 27 28 29 |
# File 'app/models/maintenance_tasks/task_data.rb', line 25 def find(name) task_data = new(name) task_data.last_run || Task.named(name) task_data end |
Instance Method Details
#category ⇒ Symbol
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieves the Task’s category, which is one of active, new, or completed.
128 129 130 131 132 133 134 135 136 |
# File 'app/models/maintenance_tasks/task_data.rb', line 128 def category if last_run.present? && last_run.active? :active elsif last_run.nil? :new else :completed end end |
#code ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The Task’s source code.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/models/maintenance_tasks/task_data.rb', line 74 def code return if deleted? task = Task.named(name) file = if Object.respond_to?(:const_source_location) Object.const_source_location(task.name).first else task.instance_method(:process).source_location.first end File.read(file) end |
#csv_task? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns whether the Task inherits from CsvTask.
139 140 141 |
# File 'app/models/maintenance_tasks/task_data.rb', line 139 def csv_task? !deleted? && Task.named(name).has_csv_content? end |
#deleted? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns whether the Task has been deleted.
110 111 112 113 114 115 |
# File 'app/models/maintenance_tasks/task_data.rb', line 110 def deleted? Task.named(name) false rescue Task::NotFoundError true end |
#last_run ⇒ MaintenanceTasks::Run?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieves the latest Run associated with the Task.
90 91 92 93 94 |
# File 'app/models/maintenance_tasks/task_data.rb', line 90 def last_run return @last_run if defined?(@last_run) @last_run = runs.first end |
#new ⇒ MaintenanceTasks::Task?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
154 155 156 157 158 |
# File 'app/models/maintenance_tasks/task_data.rb', line 154 def new return if deleted? MaintenanceTasks::Task.named(name).new end |
#parameter_names ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the names of parameters the Task accepts.
144 145 146 147 148 149 150 |
# File 'app/models/maintenance_tasks/task_data.rb', line 144 def parameter_names if deleted? [] else Task.named(name).attribute_names end end |
#previous_runs ⇒ ActiveRecord::Relation<MaintenanceTasks::Run>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the set of Run records associated with the Task previous to the last Run. This collection represents a historic of past Runs for information purposes, since the base for Task Data information comes primarily from the last Run.
103 104 105 106 107 |
# File 'app/models/maintenance_tasks/task_data.rb', line 103 def previous_runs return Run.none unless last_run runs.where.not(id: last_run.id) end |
#status ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The Task status. It returns the status of the last Run, if present. If the Task does not have any Runs, the Task status is new.
121 122 123 |
# File 'app/models/maintenance_tasks/task_data.rb', line 121 def status last_run&.status || "new" end |