Skip to content

Commit

Permalink
Handler's configure session keyword parameter can be optional or requ…
Browse files Browse the repository at this point in the history
…ired
  • Loading branch information
sbellware committed Apr 29, 2021
1 parent 2bdd063 commit 21129a5
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 125 deletions.
41 changes: 0 additions & 41 deletions lib/messaging/controls/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 11 additions & 5 deletions lib/messaging/handle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 21129a5

Please sign in to comment.