Skip to content

Commit

Permalink
Settings are sent to configure
Browse files Browse the repository at this point in the history
  • Loading branch information
sbellware committed Apr 29, 2021
1 parent 21129a5 commit a997391
Show file tree
Hide file tree
Showing 16 changed files with 333 additions and 252 deletions.
16 changes: 0 additions & 16 deletions lib/messaging/controls/handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,6 @@ class Example
end
end
end

module Settings
Error = Class.new(RuntimeError)

class Example
include Messaging::Handle

setting :some_setting

attr_accessor :some_other_setting

def handle(message_data)
raise Settings::Error if some_setting.nil?
end
end
end
end
end
end
29 changes: 17 additions & 12 deletions lib/messaging/handle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ def self.included(cls)
cls.class_exec do
include Dependency
include Virtual
include Settings::Setting

def handler_logger
@handler_logger ||= Log.get(self)
Expand All @@ -32,35 +31,41 @@ def build(strict: nil, session: nil, settings: nil)
instance = new
instance.strict = strict

if not settings.nil?
settings.set(instance)
end
arguments = {}

if Build.send_session?(session, instance)
instance.configure(session: session)
else
instance.configure
arguments[:session] = session
end

if Build.send_settings?(settings, instance)
arguments[:settings] = settings
end

instance.configure(**arguments)

instance
end

def self.send_session?(session, instance)
configure_session?(instance) && !session.nil?
configure?(instance, :session) && !session.nil?
end

def self.send_settings?(settings, instance)
configure?(instance, :settings) && !settings.nil?
end

def self.configure_session?(instance)
def self.configure?(instance, parameter_name)
configure_method = instance.method(:configure)

parameter_type, _name = configure_method.parameters.find do |type, name|
name == :session
parameter_type, _ = configure_method.parameters.find do |type, name|
name == parameter_name
end

return false if parameter_type.nil?

return true if [:key, :keyreq].include?(parameter_type)

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})"
error_message = "Incorrect definition of handler's configure method. The #{parameter_name} parameter must be a keyword argument (Handler: #{instance.class}, Parameter Type: #{parameter_type.inspect})"
handler_logger.error(tag: :handle) { error_message }
raise Error, error_message
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require_relative '../../../automated_init'

context "Handle" do
context "Configure" do
context "Session" do
context "Configure Method Receives Session" do
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

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 not raised" do
refute_raises(ArgumentError) do
handler_class.build
end
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
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative '../../../automated_init'

context "Handle" do
context "Configure" do
context "Session" 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
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative '../../../automated_init'

context "Handle" do
context "Configure" do
context "Session" do
context "Parameterless Configure Method" do
handler_class = Controls::Handler::Example

context "Given" do
session = Object.new

test "Argument error not raised" do
refute_raises(ArgumentError) do
handler_class.build(session: session)
end
end
end

context "Not Given" do
test "Argument error not raised" do
refute_raises(ArgumentError) do
handler_class.build
end
end
end
end
end
end
end
34 changes: 34 additions & 0 deletions test/automated/handle/configure/session_and_settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative '../../automated_init'

context "Handle" do
context "Configure" do
context "Session and Settings" do
handler_class = Class.new do
include Messaging::Handle

attr_accessor :session
attr_accessor :settings

def configure(session: nil, settings: nil)
self.session = session
self.settings = settings
end
end

session = Object.new
settings = Object.new

context "Passed to configure method" do
handler = handler_class.build(session: session, settings: settings)

test "Session" do
assert(handler.session.equal?(session))
end

test "Settings" do
assert(handler.settings.equal?(settings))
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require_relative '../../../automated_init'

context "Handle" do
context "Configure" do
context "Settings" do
context "Configure Method Receives Settings" do
context "Settings Is an Optional Keyword Argument" do
handler_class = Class.new do
include Messaging::Handle

attr_accessor :settings

def configure(settings: nil)
self.settings = settings
end
end

context "Given" do
settings = Object.new

context "Settings is passed to configure method" do
handler = handler_class.build(settings: settings)

test do
assert(handler.settings.equal?(settings))
end
end
end

context "Not Given" do
test "Argument error not raised" do
refute_raises(ArgumentError) do
handler_class.build
end
end
end
end

context "Settings is a required keyword argument" do
handler_class = Class.new do
include Messaging::Handle

attr_accessor :settings

def configure(settings:)
self.settings = settings
end
end

context "Given" do
settings = Object.new

context "Settings is passed to configure method" do
handler = handler_class.build(settings: settings)

test do
assert(handler.settings.equal?(settings))
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
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative '../../../automated_init'

context "Handle" do
context "Configure" do
context "Settings" do
context "Invalid Settings Parameter Definition" do
context "Settings is a Positional Argument" do
handler_class = Class.new do
include Messaging::Handle

def configure(settings)
end
end

test "Is an error" do
assert_raises(Messaging::Handle::Build::Error) do
handler_class.build
end
end
end

context "Settings Is an Optional Positional Argument" do
handler_class = Class.new do
include Messaging::Handle

def configure(settings=nil)
end
end

test "Is an error" do
assert_raises(Messaging::Handle::Build::Error) do
handler_class.build
end
end
end
end
end
end
end
Loading

0 comments on commit a997391

Please sign in to comment.