Refactoring: Changed attribute name instance_action to more re-useable and generic name action.

This commit is contained in:
Thorsten Eckel 2017-12-21 12:19:30 +01:00
parent a42531ce46
commit fc1b66d646
24 changed files with 167 additions and 132 deletions

View file

@ -9,8 +9,8 @@ class Sequencer
class Assign < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure
uses :instance, :associations, :instance_action, :dry_run
provides :instance_action
uses :instance, :associations, :action, :dry_run
provides :action
def process
return if dry_run
@ -19,9 +19,9 @@ class Sequencer
instance.assign_attributes(associations)
# execute associations check only if needed for performance reasons
return if instance_action != :unchanged
return if action != :unchanged
return if !changed?
state.provide(:instance_action, :changed)
state.provide(:action, :changed)
rescue => e
handle_failure(e)
end

View file

@ -6,9 +6,9 @@ class Sequencer
module Attributes
class AddByIds < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Mapping::Mixin::ProvideMapped
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
skip_any_instance_action
skip_any_action
def process
provide_mapped do

View file

@ -5,17 +5,17 @@ class Sequencer
module Model
module Attributes
class CheckMandatory < Sequencer::Unit::Base
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
skip_any_instance_action
skip_any_action
uses :mapped
provides :instance_action
provides :action
def process
mandatory.each do |mapped_attribute|
next if mapped[mapped_attribute].present?
state.provide(:instance_action, :skipped)
state.provide(:action, :skipped)
break
end
end

View file

@ -5,17 +5,17 @@ class Sequencer
module Model
class Create < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
skip_any_instance_action
skip_any_action
uses :mapped, :model_class
provides :instance, :instance_action
provides :instance, :action
def process
instance = model_class.new(mapped)
state.provide(:instance, instance)
state.provide(:instance_action, :created)
state.provide(:action, :created)
rescue => e
handle_failure(e)
end

View file

@ -5,7 +5,7 @@ class Sequencer
module Model
class HttpLog < Sequencer::Unit::Base
uses :dry_run, :instance_action, :remote_id, :mapped, :exception
uses :dry_run, :action, :remote_id, :mapped, :exception
def process
return if dry_run
@ -13,7 +13,7 @@ class Sequencer
direction: 'out',
facility: facility,
method: 'tcp',
url: "#{instance_action} -> #{remote_id}",
url: "#{action} -> #{remote_id}",
status: status,
ip: nil,
request: {
@ -31,7 +31,7 @@ class Sequencer
def status
@status ||= begin
instance_action == :failed ? :failed : :success
action == :failed ? :failed : :success
end
end

View file

@ -6,9 +6,9 @@ class Sequencer
module Lookup
class Attributes < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
skip_instance_action :skipped
skip_action :skipped
uses :mapped, :model_class
provides :instance

View file

@ -6,9 +6,9 @@ class Sequencer
module Lookup
class ExternalSync < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
skip_instance_action :skipped
skip_action :skipped
uses :remote_id, :model_class, :external_sync_source
provides :instance

View file

@ -7,13 +7,13 @@ class Sequencer
module HandleFailure
def self.included(base)
base.provides :exception, :instance_action
base.provides :exception, :action
end
def handle_failure(e)
logger.error(e)
state.provide(:exception, e)
state.provide(:instance_action, :failed)
state.provide(:action, :failed)
end
end
end

View file

@ -0,0 +1,55 @@
class Sequencer
class Unit
module Import
module Common
module Model
module Mixin
module Skip
module Action
module ClassMethods
def skip_action(*actions)
declaration_accessor(
key: __method__,
attributes: actions
)
end
alias skip_actions skip_action
def skip_any_action
skip_actions(:any)
end
def skip_action?(action)
logger.debug("Checking if skip is necessary for action #{action.inspect}.")
return false if action.blank?
logger.debug("Checking if skip is necessary for skip_actions #{skip_actions.inspect}.")
return false if skip_actions.blank?
return true if skip_actions.include?(action)
return true if skip_actions.include?(:any)
false
end
end
def self.prepended(base)
base.extend(ClassMethods)
end
def process
action = state.optional(:action)
if self.class.skip_action?(action)
logger.debug("Skipping due to provided action #{action.inspect}.")
else
logger.debug("Nope. Won't skip action #{action.inspect}.")
super
end
end
end
end
end
end
end
end
end
end

View file

@ -1,55 +0,0 @@
class Sequencer
class Unit
module Import
module Common
module Model
module Mixin
module Skip
module InstanceAction
module ClassMethods
def skip_instance_action(*instance_actions)
declaration_accessor(
key: __method__,
attributes: instance_actions
)
end
alias skip_instance_actions skip_instance_action
def skip_any_instance_action
skip_instance_actions(:any)
end
def skip_instance_action?(instance_action)
logger.debug("Checking if skip is necessary for instance_action #{instance_action.inspect}.")
return false if instance_action.blank?
logger.debug("Checking if skip is necessary for skip_instance_actions #{skip_instance_actions.inspect}.")
return false if skip_instance_actions.blank?
return true if skip_instance_actions.include?(instance_action)
return true if skip_instance_actions.include?(:any)
false
end
end
def self.prepended(base)
base.extend(ClassMethods)
end
def process
instance_action = state.optional(:instance_action)
if self.class.skip_instance_action?(instance_action)
logger.debug("Skipping due to provided instance_action #{instance_action.inspect}.")
else
logger.debug("Nope. Won't skip instance_action #{instance_action.inspect}.")
super
end
end
end
end
end
end
end
end
end
end

View file

@ -9,16 +9,16 @@ class Sequencer
module Blank
class Base < Sequencer::Unit::Base
include ::Sequencer::Unit::Common::Mixin::DynamicAttribute
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
skip_any_instance_action
skip_any_action
provides :instance_action
provides :action
def process
return if !skip?
logger.debug("Skipping. Blank #{attribute} found: #{attribute_value.inspect}")
state.provide(:instance_action, :skipped)
state.provide(:action, :skipped)
end
private

View file

@ -9,16 +9,16 @@ class Sequencer
module MissingMandatory
class Base < Sequencer::Unit::Base
include ::Sequencer::Unit::Common::Mixin::DynamicAttribute
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
skip_any_instance_action
skip_any_action
provides :instance_action
provides :action
def process
return if !skip?
logger.debug("Skipping. Missing mandatory attributes for #{attribute}: #{attribute_value.inspect}")
state.provide(:instance_action, :skipped)
state.provide(:action, :skipped)
end
private

View file

@ -1,21 +0,0 @@
require 'sequencer/unit/import/common/model/statistics/mixin/instance_action_diff'
class Sequencer
class Unit
module Import
module Common
module Model
module Statistics
class Diff < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Model::Statistics::Mixin::InstanceActionDiff
def process
state.provide(:statistics_diff, diff)
end
end
end
end
end
end
end
end

View file

@ -0,0 +1,33 @@
require 'sequencer/unit/import/common/model/statistics/mixin/action_diff'
class Sequencer
class Unit
module Import
module Common
module Model
module Statistics
module Diff
class ModelKey < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Model::Statistics::Mixin::ActionDiff
uses :model_class
def process
state.provide(:statistics_diff) do
{
model_key => diff,
}
end
end
def model_key
model_class.name.pluralize.to_sym
end
end
end
end
end
end
end
end
end

View file

@ -0,0 +1,23 @@
require 'sequencer/unit/import/common/model/statistics/mixin/action_diff'
class Sequencer
class Unit
module Import
module Common
module Model
module Statistics
module Diff
class TopLevel < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Model::Statistics::Mixin::ActionDiff
def process
state.provide(:statistics_diff, diff)
end
end
end
end
end
end
end
end
end

View file

@ -7,26 +7,26 @@ class Sequencer
module Model
module Statistics
module Mixin
module InstanceActionDiff
module ActionDiff
include Sequencer::Unit::Import::Common::Model::Statistics::Mixin::Common
def self.included(base)
base.uses :instance_action
base.uses :action
base.provides :statistics_diff
end
private
def diff
raise "Unknown action '#{instance_action}'" if !possible?
raise "Unknown action '#{action}'" if !possible?
empty_diff.merge(
instance_action => 1,
action => 1,
sum: 1,
)
end
def possible?
possible_actions.include?(instance_action)
possible_actions.include?(action)
end
end
end

View file

@ -5,12 +5,12 @@ class Sequencer
module Model
class Update < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
skip_any_instance_action
skip_any_action
uses :instance, :mapped
provides :instance_action
provides :action
def process
# check if no instance is given - so we can't update it
@ -26,7 +26,7 @@ class Sequencer
instance.assign_attributes(mapped)
action = changed? ? :updated : :unchanged
state.provide(:instance_action, action)
state.provide(:action, action)
end
rescue => e
handle_failure(e)

View file

@ -5,9 +5,9 @@ class Sequencer
module User
module Attributes
class Downcase < Sequencer::Unit::Base
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
skip_instance_action :skipped
skip_action :skipped
uses :mapped

View file

@ -5,9 +5,9 @@ class Sequencer
module User
module Email
class CheckValidity < Sequencer::Unit::Base
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
skip_instance_action :skipped
skip_action :skipped
uses :mapped

View file

@ -5,7 +5,7 @@ class Sequencer
module FolderContact
module Statistics
class Diff < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Model::Statistics::Mixin::InstanceActionDiff
include ::Sequencer::Unit::Import::Common::Model::Statistics::Mixin::ActionDiff
uses :ews_folder_name

View file

@ -7,9 +7,9 @@ class Sequencer
module RoleIds
class Dn < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Mapping::Mixin::ProvideMapped
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
skip_any_instance_action
skip_any_action
uses :resource, :remote_id, :dn_roles

View file

@ -6,12 +6,12 @@ class Sequencer
module Attributes
module RoleIds
class Unassigned < Sequencer::Unit::Base
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
skip_any_instance_action
skip_any_action
uses :resource, :dn_roles, :ldap_config, :mapped
provides :instance_action
provides :action
def process
# use signup/Zammad default roles
@ -30,11 +30,11 @@ class Sequencer
if instance.present?
# deactivate instance if role assignment is lost
instance.update!(active: false)
state.provide(:instance_action, :deactivated)
state.provide(:action, :deactivated)
else
# skip instance creation if no existing
# instance was found yet
state.provide(:instance_action, :skipped)
state.provide(:action, :skipped)
end
end
end

View file

@ -6,9 +6,9 @@ class Sequencer
module Attributes
class Static < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Mapping::Mixin::ProvideMapped
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::InstanceAction
prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
skip_any_instance_action
skip_any_action
def process
provide_mapped do

View file

@ -1,4 +1,4 @@
require 'sequencer/unit/import/common/model/statistics/mixin/instance_action_diff'
require 'sequencer/unit/import/common/model/statistics/mixin/action_diff'
class Sequencer
class Unit
@ -7,7 +7,7 @@ class Sequencer
module User
module Statistics
class Diff < Sequencer::Unit::Base
include ::Sequencer::Unit::Import::Common::Model::Statistics::Mixin::InstanceActionDiff
include ::Sequencer::Unit::Import::Common::Model::Statistics::Mixin::ActionDiff
uses :instance, :associations, :signup_role_ids