From de6d8c54ae50c4f9fc9f73c15df54411185523d5 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Wed, 20 Dec 2017 16:08:37 +0100 Subject: [PATCH] Introduced new simple provider structure to DRY up Units and enforce sanitized naming. --- .../unit/common/fallback_provider.rb | 29 --------------- .../unit/common/provider/attribute.rb | 36 +++++++++++++++++++ .../unit/common/provider/fallback.rb | 16 +++++++++ lib/sequencer/unit/exchange/connection.rb | 4 +-- .../exchange/folder_contacts/folder_ids.rb | 4 +-- lib/sequencer/unit/ldap/config.rb | 5 +-- lib/sequencer/unit/ldap/connection.rb | 5 +-- 7 files changed, 62 insertions(+), 37 deletions(-) delete mode 100644 lib/sequencer/unit/common/fallback_provider.rb create mode 100644 lib/sequencer/unit/common/provider/attribute.rb create mode 100644 lib/sequencer/unit/common/provider/fallback.rb diff --git a/lib/sequencer/unit/common/fallback_provider.rb b/lib/sequencer/unit/common/fallback_provider.rb deleted file mode 100644 index 5ea564ce2..000000000 --- a/lib/sequencer/unit/common/fallback_provider.rb +++ /dev/null @@ -1,29 +0,0 @@ -class Sequencer - class Unit - module Common - class FallbackProvider < Sequencer::Unit::Base - - def process - provides = self.class.provides - raise 'Only one provide attribute possible' if provides.size != 1 - - attribute = provides.first - return if state.provided?(attribute) - - result = fallback - - # don't store nil values which are default anyway - return if result.nil? - - state.provide(attribute, result) - end - - private - - def fallback - raise 'Missing implementation of fallback method' - end - end - end - end -end diff --git a/lib/sequencer/unit/common/provider/attribute.rb b/lib/sequencer/unit/common/provider/attribute.rb new file mode 100644 index 000000000..115af7466 --- /dev/null +++ b/lib/sequencer/unit/common/provider/attribute.rb @@ -0,0 +1,36 @@ +class Sequencer + class Unit + module Common + module Provider + class Attribute < Sequencer::Unit::Base + + def process + return if ignore? + state.provide(attribute, value) + end + + private + + def attribute + @attribute ||= provides + end + + def provides + provides_list = self.class.provides + raise "Only single provide attribute possible for class #{self.class.name}" if provides_list.size != 1 + provides_list.first + end + + def value + @value ||= send(attribute) + end + + def ignore? + # don't store nil values which are default anyway + value.nil? + end + end + end + end + end +end diff --git a/lib/sequencer/unit/common/provider/fallback.rb b/lib/sequencer/unit/common/provider/fallback.rb new file mode 100644 index 000000000..a5b6b6288 --- /dev/null +++ b/lib/sequencer/unit/common/provider/fallback.rb @@ -0,0 +1,16 @@ +class Sequencer + class Unit + module Common + module Provider + class Fallback < Sequencer::Unit::Common::Provider::Attribute + + private + + def ignore? + state.provided?(attribute) || super + end + end + end + end + end +end diff --git a/lib/sequencer/unit/exchange/connection.rb b/lib/sequencer/unit/exchange/connection.rb index 2c8f2234c..da71593b8 100644 --- a/lib/sequencer/unit/exchange/connection.rb +++ b/lib/sequencer/unit/exchange/connection.rb @@ -1,14 +1,14 @@ class Sequencer class Unit module Exchange - class Connection < Sequencer::Unit::Common::FallbackProvider + class Connection < Sequencer::Unit::Common::Provider::Fallback uses :ews_config provides :ews_connection private - def fallback + def ews_connection Viewpoint::EWSClient.new( config[:endpoint], config[:user], diff --git a/lib/sequencer/unit/import/exchange/folder_contacts/folder_ids.rb b/lib/sequencer/unit/import/exchange/folder_contacts/folder_ids.rb index 49ef21099..0618cbda7 100644 --- a/lib/sequencer/unit/import/exchange/folder_contacts/folder_ids.rb +++ b/lib/sequencer/unit/import/exchange/folder_contacts/folder_ids.rb @@ -3,13 +3,13 @@ class Sequencer module Import module Exchange module FolderContacts - class FolderIds < Sequencer::Unit::Common::FallbackProvider + class FolderIds < Sequencer::Unit::Common::Provider::Fallback provides :ews_folder_ids private - def fallback + def ews_folder_ids ::Import::Exchange.config[:folders] end end diff --git a/lib/sequencer/unit/ldap/config.rb b/lib/sequencer/unit/ldap/config.rb index 77d242753..3425c4f5c 100644 --- a/lib/sequencer/unit/ldap/config.rb +++ b/lib/sequencer/unit/ldap/config.rb @@ -3,12 +3,13 @@ require 'import/ldap' class Sequencer class Unit module Ldap - class Config < Sequencer::Unit::Common::FallbackProvider + class Config < Sequencer::Unit::Common::Provider::Fallback + provides :ldap_config private - def fallback + def ldap_config ::Import::Ldap.config end end diff --git a/lib/sequencer/unit/ldap/connection.rb b/lib/sequencer/unit/ldap/connection.rb index 7d03da751..4276d95dd 100644 --- a/lib/sequencer/unit/ldap/connection.rb +++ b/lib/sequencer/unit/ldap/connection.rb @@ -3,13 +3,14 @@ require 'ldap' class Sequencer class Unit module Ldap - class Connection < Sequencer::Unit::Common::FallbackProvider + class Connection < Sequencer::Unit::Common::Provider::Fallback + uses :ldap_config provides :ldap_connection private - def fallback + def ldap_connection ::Ldap.new(ldap_config) end end