Module: Libis::Workflow::Base::WorkItem
- Included in:
- WorkItem
- Defined in:
- lib/libis/workflow/base/work_item.rb
Overview
Base module for all work items.
This module lacks the implementation for the data attributes. It functions as an interface that describes the common functionality regardless of the storage implementation. These attributes require some implementation:
-
parent: [Object|nil] a link to a parent work item. Work items can be organized in any hierarchy you think is
relevant for your workflow (e.g. directory[/directory...]/file/line or library/section/book/page). Of course hierarchies are not mandatory.
-
items: [Array] a list of child work items. see above.
-
options: [Hash] a set of options for the task chain on how to deal with this work item. This attribute can be
used to fine-tune the behaviour of tasks for a particular work item.
-
properties: [Hash] a set of properties, typically collected during the workflow processing and used to store
final or intermediate resulst of tasks. The ::Lias::Ingester::FileItem module uses this attribute to store the properties (e.g. size, checksum, ...) of the file it represents.
-
status_log: [Array] a list of all status changes the work item went through.
-
summary: [Hash] collected statistics about the ingest for the work item and its children. This structure will
be filled in by the included task ::Lias::Ingester::Tasks::Analyzer wich is appended to the workflow by default.
The module is created so that it is possible to implement an ActiveRecord/Datamapper/… implementation easily. A simple in-memory implementation would require:
attr_accessor :parent attr_accessor :items attr_accessor :options, :properties attr_accessor :status_log attr_accessor :summary
def initialize
self.parent = nil
self.items = []
self. = {}
self.properties = {}
self.status_log = []
self.summary = {}
end
protected
## Method below should be adapted to match the implementation of the status array
def add_status_log(info)
self.status_log << info
end
The implementation should also take care that the public methods #save and #save! are implemented. ActiveRecord and Mongoid are known to implement these, but others may not.
Instance Method Summary collapse
-
#add_item(item) ⇒ Object
(also: #<<)
Add a child work item.
-
#each(&block) ⇒ Object
Iterates over the work item clients and invokes code on each of them.
-
#get_item_list ⇒ Object
Get list of items.
-
#get_items ⇒ Object
Get list of items.
-
#get_parent ⇒ Libis::Workflow::Base::WorkItem
Return item’s parent.
-
#get_root ⇒ Libis::Workflow::Base::WorkItem
go up the hierarchy and return the topmost work item.
-
#get_run ⇒ Libis::Workflow::Base::Run
Get the top.
-
#label ⇒ Object
Label is a more descriptive name.
- #label=(value) ⇒ Object
- #labelpath ⇒ Object
- #labels ⇒ Object
-
#name ⇒ String
String representation of the identity of the work item.
- #name=(n) ⇒ Object
- #namepath ⇒ Object
- #names ⇒ Object
- #size ⇒ Object (also: #count)
-
#to_filename ⇒ String
File name safe version of the to_s output.
- #to_s ⇒ Object
Methods included from Logging
Methods included from Status
#check_status, #compare_status, #set_status, #status, #status_label, #status_progress, #status_text
Instance Method Details
#add_item(item) ⇒ Object Also known as: <<
Add a child work item
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/libis/workflow/base/work_item.rb', line 133 def add_item(item) return self unless item and item.is_a?(Libis::Workflow::Base::WorkItem) self.items << item item.parent = self # noinspection RubyResolve self.save! # noinspection RubyResolve item.save! self end |
#each(&block) ⇒ Object
Iterates over the work item clients and invokes code on each of them.
120 121 122 |
# File 'lib/libis/workflow/base/work_item.rb', line 120 def each(&block) self.items.each(&block) end |
#get_item_list ⇒ Object
Get list of items.
This method should return a list of items that is safe to iterate over while it is being altered.
156 157 158 |
# File 'lib/libis/workflow/base/work_item.rb', line 156 def get_item_list self.items.dup end |
#get_items ⇒ Object
Get list of items.
This method should return a list of items that can be accessed during long processing times.
149 150 151 |
# File 'lib/libis/workflow/base/work_item.rb', line 149 def get_items self.items end |
#get_parent ⇒ Libis::Workflow::Base::WorkItem
Return item’s parent
162 163 164 |
# File 'lib/libis/workflow/base/work_item.rb', line 162 def get_parent self.parent end |
#get_root ⇒ Libis::Workflow::Base::WorkItem
go up the hierarchy and return the topmost work item
169 170 171 |
# File 'lib/libis/workflow/base/work_item.rb', line 169 def get_root self.get_parent && self.get_parent.is_a?(Libis::Workflow::Base::WorkItem) && self.get_parent.get_root || self end |
#get_run ⇒ Libis::Workflow::Base::Run
Get the top
176 177 178 179 |
# File 'lib/libis/workflow/base/work_item.rb', line 176 def get_run return self if self.is_a?(Libis::Workflow::Base::Run) self.get_parent&.get_run end |
#label ⇒ Object
Label is a more descriptive name
92 93 94 |
# File 'lib/libis/workflow/base/work_item.rb', line 92 def label self.properties['label'] || self.name end |
#label=(value) ⇒ Object
96 97 98 |
# File 'lib/libis/workflow/base/work_item.rb', line 96 def label=(value) self.properties['label'] = value end |
#labelpath ⇒ Object
104 105 106 |
# File 'lib/libis/workflow/base/work_item.rb', line 104 def labelpath; self.labels.join('/'); end |
#labels ⇒ Object
100 101 102 |
# File 'lib/libis/workflow/base/work_item.rb', line 100 def labels (self.parent.labels rescue Array.new).push(label).compact end |
#name ⇒ String
String representation of the identity of the work item.
You may want to overwrite this method as it tries the :name property or whatever #inspect returns if that failes. Typically this should return the key value, file name or id number. If that’s what your :name property contains, you’re fine.
70 71 72 73 |
# File 'lib/libis/workflow/base/work_item.rb', line 70 def name # noinspection RubyResolve self.properties['name'] || self.inspect end |
#name=(n) ⇒ Object
75 76 77 |
# File 'lib/libis/workflow/base/work_item.rb', line 75 def name=(n) self.properties['name'] = n end |
#namepath ⇒ Object
87 88 89 |
# File 'lib/libis/workflow/base/work_item.rb', line 87 def namepath; self.names.join('/'); end |
#names ⇒ Object
83 84 85 |
# File 'lib/libis/workflow/base/work_item.rb', line 83 def names (self.parent.names rescue Array.new).push(name).compact end |
#size ⇒ Object Also known as: count
124 125 126 |
# File 'lib/libis/workflow/base/work_item.rb', line 124 def size self.items.size end |
#to_filename ⇒ String
File name safe version of the to_s output.
The output should be safe to use as a file name to store work item data. Typical use is when extra file items are created by a task and need to be stored on disk. The default implementation URL-encodes (%xx) all characters except alphanumeric, ‘.’ and ‘-’.
115 116 117 |
# File 'lib/libis/workflow/base/work_item.rb', line 115 def to_filename self.to_s.gsub(/[^\w.-]/) { |s| '%%%02x' % s.ord } end |
#to_s ⇒ Object
79 80 81 |
# File 'lib/libis/workflow/base/work_item.rb', line 79 def to_s; self.name; end |