From 21129a5cd72fadc08932717b0bdee9e04041e672 Mon Sep 17 00:00:00 2001 From: Scott Bellware Date: Thu, 29 Apr 2021 15:29:43 -0500 Subject: [PATCH] Handler's configure session keyword parameter can be optional or required --- lib/messaging/controls/handler.rb | 41 ----------- lib/messaging/handle.rb | 16 ++-- .../configure_method_accepts_session.rb | 52 +++++++++++-- .../invalid_session_parameter.rb | 73 ------------------- .../invalid_session_parameter_definition.rb | 37 ++++++++++ ...d.rb => parameterless_configure_method.rb} | 2 +- 6 files changed, 96 insertions(+), 125 deletions(-) delete mode 100644 test/automated/handle/session_configuration/invalid_session_parameter.rb create mode 100644 test/automated/handle/session_configuration/invalid_session_parameter_definition.rb rename test/automated/handle/session_configuration/{bare_configure_method.rb => parameterless_configure_method.rb} (92%) diff --git a/lib/messaging/controls/handler.rb b/lib/messaging/controls/handler.rb index e26c28f..967cc6e 100644 --- a/lib/messaging/controls/handler.rb +++ b/lib/messaging/controls/handler.rb @@ -61,47 +61,6 @@ class Example end end - module SessionArgument - class Example - include Messaging::Handle - - attr_accessor :session - - def configure(session: nil) - self.session = session - end - end - - module Anomaly - module Required - class Example - include Messaging::Handle - - def configure(session:) - end - end - end - - module Positional - class Example - include Messaging::Handle - - def configure(session) - end - end - - module Optional - class Example - include Messaging::Handle - - def configure(session=nil) - end - end - end - end - end - end - module Settings Error = Class.new(RuntimeError) diff --git a/lib/messaging/handle.rb b/lib/messaging/handle.rb index f459ec3..fc592f4 100644 --- a/lib/messaging/handle.rb +++ b/lib/messaging/handle.rb @@ -26,6 +26,8 @@ def handler_logger end module Build + Error = Class.new(RuntimeError) + def build(strict: nil, session: nil, settings: nil) instance = new instance.strict = strict @@ -34,7 +36,7 @@ def build(strict: nil, session: nil, settings: nil) settings.set(instance) end - if Build.configure_session?(instance) + if Build.send_session?(session, instance) instance.configure(session: session) else instance.configure @@ -43,20 +45,24 @@ def build(strict: nil, session: nil, settings: nil) instance end + def self.send_session?(session, instance) + configure_session?(instance) && !session.nil? + end + def self.configure_session?(instance) configure_method = instance.method(:configure) - parameter_type, _ = configure_method.parameters.find do |type, name| + parameter_type, _name = configure_method.parameters.find do |type, name| name == :session end return false if parameter_type.nil? - return true if parameter_type == :key + return true if [:key, :keyreq].include?(parameter_type) - error_message = "Optional session parameter of configure is not a keyword argument (Type: #{parameter_type.inspect})" + error_message = "Incorrect definition of handler's configure method. Session parameter must be a keyword argument (Handler: #{instance.class}, Session Parameter Type: #{parameter_type.inspect})" handler_logger.error(tag: :handle) { error_message } - raise ArgumentError, error_message + raise Error, error_message end def self.handler_logger diff --git a/test/automated/handle/session_configuration/configure_method_accepts_session.rb b/test/automated/handle/session_configuration/configure_method_accepts_session.rb index 902f938..2e8f9c8 100644 --- a/test/automated/handle/session_configuration/configure_method_accepts_session.rb +++ b/test/automated/handle/session_configuration/configure_method_accepts_session.rb @@ -2,16 +2,26 @@ context "Handle" do context "Session Configuration" do - context "Configure Method Accepts Session Argument" do - handler_class = Controls::Handler::SessionArgument::Example + context "Session is an optional keyword argument" do + handler_class = Class.new do + include Messaging::Handle + + attr_accessor :session + + def configure(session: nil) + self.session = session + end + end context "Given" do session = Object.new - handler = handler_class.build(session: session) + context "Session is passed to configure method" do + handler = handler_class.build(session: session) - test "Session is passed to configure method" do - assert(handler.session.equal?(session)) + test do + assert(handler.session.equal?(session)) + end end end @@ -23,5 +33,37 @@ end end end + + context "Session is a required keyword argument" do + handler_class = Class.new do + include Messaging::Handle + + attr_accessor :session + + def configure(session:) + self.session = session + end + end + + context "Given" do + session = Object.new + + context "Session is passed to configure method" do + handler = handler_class.build(session: session) + + test do + assert(handler.session.equal?(session)) + end + end + end + + context "Not Given" do + test "Argument error is raised" do + assert_raises(ArgumentError) do + handler_class.build + end + end + end + end end end diff --git a/test/automated/handle/session_configuration/invalid_session_parameter.rb b/test/automated/handle/session_configuration/invalid_session_parameter.rb deleted file mode 100644 index e2a3a91..0000000 --- a/test/automated/handle/session_configuration/invalid_session_parameter.rb +++ /dev/null @@ -1,73 +0,0 @@ -require_relative '../../automated_init' - -context "Handle" do - context "Session Configuration" do - context "Configure Method Includes Incorrect Session Parameter" do - context "Session is positional argument" do - handler_class = Controls::Handler::SessionArgument::Anomaly::Positional::Example - - context "Given" do - session = Object.new - - test "Argument error is raised" do - assert_raises(ArgumentError) do - handler_class.build(session: session) - end - end - end - - context "Not Given" do - test "Argument error is raised" do - assert_raises(ArgumentError) do - handler_class.build - end - end - end - end - - context "Session is optional positional argument" do - handler_class = Controls::Handler::SessionArgument::Anomaly::Positional::Optional::Example - - context "Given" do - session = Object.new - - test "Argument error is raised" do - assert_raises(ArgumentError) do - handler_class.build(session: session) - end - end - end - - context "Not Given" do - test "Argument error is raised" do - assert_raises(ArgumentError) do - handler_class.build - end - end - end - end - - context "Session is required keyword argument" do - handler_class = Controls::Handler::SessionArgument::Anomaly::Required::Example - - context "Given" do - session = Object.new - - test "Argument error is raised" do - assert_raises(ArgumentError) do - handler_class.build(session: session) - end - end - end - - context "Not Given" do - test "Argument error is raised" do - assert_raises(ArgumentError) do - handler_class.build - end - end - end - end - end - end -end diff --git a/test/automated/handle/session_configuration/invalid_session_parameter_definition.rb b/test/automated/handle/session_configuration/invalid_session_parameter_definition.rb new file mode 100644 index 0000000..9717932 --- /dev/null +++ b/test/automated/handle/session_configuration/invalid_session_parameter_definition.rb @@ -0,0 +1,37 @@ +require_relative '../../automated_init' + +context "Handle" do + context "Session Configuration" do + context "Invalid Session Parameter Definition" do + context "Session is a Positional Argument" do + handler_class = Class.new do + include Messaging::Handle + + def configure(session) + end + end + + test "Is an error" do + assert_raises(Messaging::Handle::Build::Error) do + handler_class.build + end + end + end + + context "Session Is an Optional Positional Argument" do + handler_class = Class.new do + include Messaging::Handle + + def configure(session=nil) + end + end + + test "Is an error" do + assert_raises(Messaging::Handle::Build::Error) do + handler_class.build + end + end + end + end + end +end diff --git a/test/automated/handle/session_configuration/bare_configure_method.rb b/test/automated/handle/session_configuration/parameterless_configure_method.rb similarity index 92% rename from test/automated/handle/session_configuration/bare_configure_method.rb rename to test/automated/handle/session_configuration/parameterless_configure_method.rb index aba7820..faa11e6 100644 --- a/test/automated/handle/session_configuration/bare_configure_method.rb +++ b/test/automated/handle/session_configuration/parameterless_configure_method.rb @@ -2,7 +2,7 @@ context "Handle" do context "Session Configuration" do - context "Bare Configure Method" do + context "Parameterless Configure Method" do handler_class = Controls::Handler::Example context "Given" do