Refactoring: Unified and improved SubSequence handling.

This commit is contained in:
Thorsten Eckel 2017-11-30 14:55:13 +01:00
parent 92930a0e8f
commit b1f1dc2e12
8 changed files with 117 additions and 133 deletions

View file

@ -1,37 +0,0 @@
require 'sequencer/mixin/sub_sequence'
class Sequencer
module Mixin
module ImportJob
module ResourceLoop
include ::Sequencer::Mixin::SubSequence
private
def resource_sequence(sequence_name, items)
default_params = {
dry_run: import_job.dry_run,
import_job: import_job,
}
items.each do |item|
resource_params = {}
if block_given?
resource_params = yield item
else
resource_params[:resource] = item
end
resource_params[:resource] = resource_params[:resource].with_indifferent_access
sub_sequence(sequence_name,
parameters: default_params.merge(resource_params))
end
# store possible unsaved values in result e.g. statistics
import_job.save!
end
end
end
end
end

View file

@ -1,9 +0,0 @@
class Sequencer
module Mixin
module SubSequence
def sub_sequence(sequence, args = {})
Sequencer.process(sequence, args)
end
end
end
end

View file

@ -1,65 +0,0 @@
require 'sequencer/mixin/sub_sequence'
class Sequencer
class Unit
module Import
module Common
module ImportJob
module SubSequence
class General < Sequencer::Unit::Base
include ::Sequencer::Mixin::SubSequence
uses :import_job
def process
resource_sequence
end
private
# INFO: Cache results via `@sequence ||= ...`, if needed
def sequence
raise "Missing implementation of '#{__method__}' method for '#{self.class.name}'"
end
# INFO: Cache results via `@resources ||= ...`, if needed
def resources
raise "Missing implementation of '#{__method__}' method for '#{self.class.name}'"
end
def default_parameters
{
dry_run: import_job.dry_run,
import_job: import_job,
}
end
def resource_sequence
return if resources.blank?
defaults = default_parameters
resources.each do |resource|
arguments = {
parameters: defaults.merge(resource: resource)
}
yield resource, arguments if block_given?
arguments[:parameters][:resource] = arguments[:parameters][:resource].with_indifferent_access
result = sub_sequence(sequence, arguments)
processed(result)
end
end
def processed(_result); end
end
end
end
end
end
end
end

View file

@ -1,15 +0,0 @@
class Sequencer
class Unit
module Import
module Common
module ImportJob
module SubSequence
class Resource < Sequencer::Unit::Import::Common::ImportJob::SubSequence::General
uses :resource
end
end
end
end
end
end
end

View file

@ -0,0 +1,49 @@
class Sequencer
class Unit
module Import
module Common
module SubSequence
module Mixin
module Base
private
def sequence
raise "Missing implementation of '#{__method__}' method for '#{self.class.name}'"
end
def sequence_name
@sequence_name ||= sequence
end
def default_params
{}
end
def default_parameter
@default_parameter ||= default_params
end
def sequence_resource(resource)
sequence_parameter = default_params.dup
sequence_parameter[:resource] = resource
sequence_parameter = yield(sequence_parameter) if block_given?
sequence_parameter[:resource] = sequence_parameter[:resource].to_h.with_indifferent_access
::Sequencer.process(sequence_name,
parameters: default_params.merge(sequence_parameter))
end
def sequence_resources(resources, &block)
resources.each do |resource|
sequence_resource(resource, &block)
end
end
end
end
end
end
end
end
end

View file

@ -0,0 +1,28 @@
class Sequencer
class Unit
module Import
module Common
module SubSequence
module Mixin
module ImportJob
include ::Sequencer::Unit::Import::Common::SubSequence::Mixin::Base
def self.included(base)
base.uses :import_job
end
private
def default_params
{
dry_run: import_job.dry_run,
import_job: import_job,
}
end
end
end
end
end
end
end
end

View file

@ -0,0 +1,26 @@
class Sequencer
class Unit
module Import
module Common
module SubSequence
module Mixin
module Resources
include ::Sequencer::Unit::Import::Common::SubSequence::Mixin::Base
def process
return if resources.blank?
sequence_resources(resources)
end
private
def resources
raise "Missing implementation of '#{__method__}' method for '#{self.class.name}'"
end
end
end
end
end
end
end
end

View file

@ -1,5 +1,3 @@
require 'sequencer/mixin/import_job/resource_loop'
class Sequencer
class Unit
module Import
@ -7,27 +5,36 @@ class Sequencer
module FolderContacts
class SubSequence < Sequencer::Unit::Base
include ::Sequencer::Unit::Exchange::Folders::Mixin::Folder
include ::Sequencer::Mixin::ImportJob::ResourceLoop
include ::Sequencer::Unit::Import::Common::SubSequence::Mixin::ImportJob
uses :ews_folder_ids, :import_job
uses :ews_folder_ids
def process
return if ews_folder_ids.blank?
ews_folder_ids.each do |folder_id|
folder = ews_folder.find(folder_id)
display_path = ews_folder.display_path(folder)
resource_sequence('Import::Exchange::FolderContact', folder.items) do |item|
sequence_resources(folder.items) do |parameters|
item = parameters[:resource]
logger.debug("Extracting attributes from Exchange item: #{item.get_all_properties!.inspect}")
{
parameters.merge(
resource: ::Import::Exchange::ItemAttributes.extract(item),
ews_folder_name: display_path,
}
)
end
end
end
private
def sequence
'Import::Exchange::FolderContact'
end
end
end
end