From b1f1dc2e124e9165b149b43e9ecde4fa308841d8 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Thu, 30 Nov 2017 14:55:13 +0100 Subject: [PATCH] Refactoring: Unified and improved SubSequence handling. --- .../mixin/import_job/resource_loop.rb | 37 ----------- lib/sequencer/mixin/sub_sequence.rb | 9 --- .../common/import_job/sub_sequence/general.rb | 65 ------------------- .../import_job/sub_sequence/resource.rb | 15 ----- .../import/common/sub_sequence/mixin/base.rb | 49 ++++++++++++++ .../common/sub_sequence/mixin/import_job.rb | 28 ++++++++ .../common/sub_sequence/mixin/resources.rb | 26 ++++++++ .../exchange/folder_contacts/sub_sequence.rb | 21 ++++-- 8 files changed, 117 insertions(+), 133 deletions(-) delete mode 100644 lib/sequencer/mixin/import_job/resource_loop.rb delete mode 100644 lib/sequencer/mixin/sub_sequence.rb delete mode 100644 lib/sequencer/unit/import/common/import_job/sub_sequence/general.rb delete mode 100644 lib/sequencer/unit/import/common/import_job/sub_sequence/resource.rb create mode 100644 lib/sequencer/unit/import/common/sub_sequence/mixin/base.rb create mode 100644 lib/sequencer/unit/import/common/sub_sequence/mixin/import_job.rb create mode 100644 lib/sequencer/unit/import/common/sub_sequence/mixin/resources.rb diff --git a/lib/sequencer/mixin/import_job/resource_loop.rb b/lib/sequencer/mixin/import_job/resource_loop.rb deleted file mode 100644 index a27e7e670..000000000 --- a/lib/sequencer/mixin/import_job/resource_loop.rb +++ /dev/null @@ -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 diff --git a/lib/sequencer/mixin/sub_sequence.rb b/lib/sequencer/mixin/sub_sequence.rb deleted file mode 100644 index 75348641c..000000000 --- a/lib/sequencer/mixin/sub_sequence.rb +++ /dev/null @@ -1,9 +0,0 @@ -class Sequencer - module Mixin - module SubSequence - def sub_sequence(sequence, args = {}) - Sequencer.process(sequence, args) - end - end - end -end diff --git a/lib/sequencer/unit/import/common/import_job/sub_sequence/general.rb b/lib/sequencer/unit/import/common/import_job/sub_sequence/general.rb deleted file mode 100644 index b0ffeafa1..000000000 --- a/lib/sequencer/unit/import/common/import_job/sub_sequence/general.rb +++ /dev/null @@ -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 diff --git a/lib/sequencer/unit/import/common/import_job/sub_sequence/resource.rb b/lib/sequencer/unit/import/common/import_job/sub_sequence/resource.rb deleted file mode 100644 index 7037d6e07..000000000 --- a/lib/sequencer/unit/import/common/import_job/sub_sequence/resource.rb +++ /dev/null @@ -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 diff --git a/lib/sequencer/unit/import/common/sub_sequence/mixin/base.rb b/lib/sequencer/unit/import/common/sub_sequence/mixin/base.rb new file mode 100644 index 000000000..33baa5250 --- /dev/null +++ b/lib/sequencer/unit/import/common/sub_sequence/mixin/base.rb @@ -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 diff --git a/lib/sequencer/unit/import/common/sub_sequence/mixin/import_job.rb b/lib/sequencer/unit/import/common/sub_sequence/mixin/import_job.rb new file mode 100644 index 000000000..175cdca27 --- /dev/null +++ b/lib/sequencer/unit/import/common/sub_sequence/mixin/import_job.rb @@ -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 diff --git a/lib/sequencer/unit/import/common/sub_sequence/mixin/resources.rb b/lib/sequencer/unit/import/common/sub_sequence/mixin/resources.rb new file mode 100644 index 000000000..b617b7549 --- /dev/null +++ b/lib/sequencer/unit/import/common/sub_sequence/mixin/resources.rb @@ -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 diff --git a/lib/sequencer/unit/import/exchange/folder_contacts/sub_sequence.rb b/lib/sequencer/unit/import/exchange/folder_contacts/sub_sequence.rb index 1fd6d7436..9b53da919 100644 --- a/lib/sequencer/unit/import/exchange/folder_contacts/sub_sequence.rb +++ b/lib/sequencer/unit/import/exchange/folder_contacts/sub_sequence.rb @@ -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