lib/simple_service/organizer.rb in simple_service-1.3.8 vs lib/simple_service/organizer.rb in simple_service-1.3.9
- old
+ new
@@ -4,12 +4,12 @@
include ServiceBase::InstanceMethods
extend ServiceBase::ClassMethods
attr_accessor :context
- def initialize(context = {})
- @context = context
+ def initialize(_context = {})
+ @context = validate_context(_context)
symbolize_context_keys
setup_call_chain
define_getters_and_setters
end
@@ -39,15 +39,20 @@
end
else
context
end
+ # also merge any optional keys
+ command.get_optional.each do |key|
+ _context[key] = context[key]
+ end
+
# instantiate and call the command
- new_context = command.new(_context).call
+ resulting_context = command.new(_context).call
# update the master context with the results of the command
- @context.merge!(new_context)
+ @context.merge!(resulting_context)
end
end
end
# allow execution of the service from the class level for those
@@ -55,9 +60,18 @@
def self.call(context = {})
self.new(context).call
end
private
+
+ def validate_context(_context)
+ unless _context.class == Hash
+ raise InvalidArgumentError,
+ "Hash required as argument, but was given a #{_context.class}"
+ end
+
+ _context
+ end
def with_validation
# don't mess with the context if we are doing internal validation
add_validation_keys_to_context unless skip_validation