Method: Simple::Service.invoke

Defined in:
lib/simple/service.rb

.invoke(service, name, args: {}, flags: {}) ⇒ Object

invokes an action with a given name.

This is the general form of invoking a service. It accepts the following arguments:

  • args: an Array of positional arguments OR a Hash of named arguments.

  • flags: a Hash of flags.

Note that the keys in both the flags and the args Hash must be strings.

The service is being called with a parameters built out of those like this:

  • The service’s positional arguments are being built from the args array parameter or from the named_args hash parameter.

  • The service’s keyword arguments are being built from the named_args and flags arguments.

In other words:

  1. You cannot set both args and named_args at the same time.

  2. The flags arguments are only being used to determine the service’s keyword parameters.

So, if the service X implements an action “def foo(bar, baz:)”, the following would all invoke that service:

  • Service.invoke3(X, :foo, “bar-value”, baz: “baz-value”), or

  • Service.invoke3(X, :foo, bar: “bar-value”, baz: “baz-value”), or

  • Service.invoke(X, :foo, args: [“bar-value”], flags: { “baz” => “baz-value” }), or

  • Service.invoke(X, :foo, args: { “bar” => “bar-value”, “baz” => “baz-value” }).

(see spec/service_spec.rb)

When there are not enough positional arguments to match the number of required positional arguments of the method we raise an ArgumentError.

When there are more positional arguments provided than the number accepted by the method we raise an ArgumentError.

Entries in the named_args Hash that are not defined in the action itself are ignored.



153
154
155
156
157
158
159
# File 'lib/simple/service.rb', line 153

def self.invoke(service, name, args: {}, flags: {})
  expect! args => [Hash, Array], flags: Hash
  args.each_key { |key| expect! key => String } if args.is_a?(Hash)
  flags.each_key { |key| expect! key => String }

  action(service, name).invoke(args: args, flags: flags)
end