2024-04-04 21:57:41 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-10-04 00:05:45 +08:00
|
|
|
# = Service::StepsInspector
|
2024-04-04 21:57:41 +08:00
|
|
|
#
|
|
|
|
# This class takes a {Service::Base::Context} object and inspects it.
|
|
|
|
# It will output a list of steps and what is their known state.
|
2024-10-04 00:05:45 +08:00
|
|
|
class Service::StepsInspector
|
2024-04-04 21:57:41 +08:00
|
|
|
# @!visibility private
|
|
|
|
class Step
|
|
|
|
attr_reader :step, :result, :nesting_level
|
|
|
|
|
|
|
|
delegate :name, to: :step
|
2024-11-09 00:24:40 +08:00
|
|
|
delegate :failure?, :success?, :error, :raised_exception?, to: :step_result, allow_nil: true
|
2024-04-04 21:57:41 +08:00
|
|
|
|
|
|
|
def self.for(step, result, nesting_level: 0)
|
|
|
|
class_name =
|
|
|
|
"#{module_parent_name}::#{step.class.name.split("::").last.sub(/^(\w+)Step$/, "\\1")}"
|
|
|
|
class_name.constantize.new(step, result, nesting_level: nesting_level)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(step, result, nesting_level: 0)
|
|
|
|
@step = step
|
|
|
|
@result = result
|
|
|
|
@nesting_level = nesting_level
|
|
|
|
end
|
|
|
|
|
|
|
|
def type
|
|
|
|
self.class.name.split("::").last.downcase
|
|
|
|
end
|
DEV: Replace `params` by the contract object in services
This patch replaces the parameters provided to a service through
`params` by the contract object.
That way, it allows better consistency when accessing input params. For
example, if you have a service without a contract, to access a
parameter, you need to use `params[:my_parameter]`. But with a contract,
you do this through `contract.my_parameter`. Now, with this patch,
you’ll be able to access it through `params.my_parameter` or
`params[:my_parameter]`.
Some methods have been added to the contract object to better mimic a
Hash. That way, when accessing/using `params`, you don’t have to think
too much about it:
- `params.my_key` is also accessible through `params[:my_key]`.
- `params.my_key = value` can also be done through `params[:my_key] =
value`.
- `#slice` and `#merge` are available.
- `#to_hash` has been implemented, so the contract object will be
automatically cast as a hash by Ruby depending on the context. For
example, with an AR model, you can do this: `user.update(**params)`.
2024-10-23 23:57:48 +08:00
|
|
|
alias inspect_type type
|
2024-04-04 21:57:41 +08:00
|
|
|
|
|
|
|
def emoji
|
|
|
|
"#{result_emoji}#{unexpected_result_emoji}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def steps
|
|
|
|
[self]
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
DEV: Replace `params` by the contract object in services
This patch replaces the parameters provided to a service through
`params` by the contract object.
That way, it allows better consistency when accessing input params. For
example, if you have a service without a contract, to access a
parameter, you need to use `params[:my_parameter]`. But with a contract,
you do this through `contract.my_parameter`. Now, with this patch,
you’ll be able to access it through `params.my_parameter` or
`params[:my_parameter]`.
Some methods have been added to the contract object to better mimic a
Hash. That way, when accessing/using `params`, you don’t have to think
too much about it:
- `params.my_key` is also accessible through `params[:my_key]`.
- `params.my_key = value` can also be done through `params[:my_key] =
value`.
- `#slice` and `#merge` are available.
- `#to_hash` has been implemented, so the contract object will be
automatically cast as a hash by Ruby depending on the context. For
example, with an AR model, you can do this: `user.update(**params)`.
2024-10-23 23:57:48 +08:00
|
|
|
"#{" " * nesting_level}[#{inspect_type}] '#{name}' #{emoji}".rstrip
|
2024-04-04 21:57:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def step_result
|
|
|
|
result["result.#{type}.#{name}"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def result_emoji
|
2024-11-09 00:24:40 +08:00
|
|
|
return "💥" if raised_exception?
|
2024-04-04 21:57:41 +08:00
|
|
|
return "❌" if failure?
|
|
|
|
return "✅" if success?
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
|
|
|
def unexpected_result_emoji
|
|
|
|
" ⚠️#{unexpected_result_text}" if step_result.try(:[], "spec.unexpected_result")
|
|
|
|
end
|
|
|
|
|
|
|
|
def unexpected_result_text
|
|
|
|
return " <= expected to return true but got false instead" if failure?
|
|
|
|
" <= expected to return false but got true instead"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# @!visibility private
|
|
|
|
class Model < Step
|
|
|
|
def error
|
|
|
|
return result[name].errors.inspect if step_result.invalid
|
|
|
|
step_result.exception.full_message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# @!visibility private
|
|
|
|
class Contract < Step
|
|
|
|
def error
|
2024-06-13 17:55:57 +08:00
|
|
|
"#{step_result.errors.inspect}\n\nProvided parameters: #{step_result.parameters.pretty_inspect}"
|
2024-04-04 21:57:41 +08:00
|
|
|
end
|
DEV: Replace `params` by the contract object in services
This patch replaces the parameters provided to a service through
`params` by the contract object.
That way, it allows better consistency when accessing input params. For
example, if you have a service without a contract, to access a
parameter, you need to use `params[:my_parameter]`. But with a contract,
you do this through `contract.my_parameter`. Now, with this patch,
you’ll be able to access it through `params.my_parameter` or
`params[:my_parameter]`.
Some methods have been added to the contract object to better mimic a
Hash. That way, when accessing/using `params`, you don’t have to think
too much about it:
- `params.my_key` is also accessible through `params[:my_key]`.
- `params.my_key = value` can also be done through `params[:my_key] =
value`.
- `#slice` and `#merge` are available.
- `#to_hash` has been implemented, so the contract object will be
automatically cast as a hash by Ruby depending on the context. For
example, with an AR model, you can do this: `user.update(**params)`.
2024-10-23 23:57:48 +08:00
|
|
|
|
|
|
|
def inspect_type
|
|
|
|
"params"
|
|
|
|
end
|
2024-04-04 21:57:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# @!visibility private
|
|
|
|
class Policy < Step
|
|
|
|
def error
|
|
|
|
step_result.reason
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# @!visibility private
|
|
|
|
class Transaction < Step
|
|
|
|
def steps
|
|
|
|
[self, *step.steps.map { Step.for(_1, result, nesting_level: nesting_level + 1).steps }]
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
2024-11-09 00:24:40 +08:00
|
|
|
"#{" " * nesting_level}[#{inspect_type}]"
|
2024-04-04 21:57:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def step_result
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2024-11-09 00:24:40 +08:00
|
|
|
|
DEV: Provide user input to services using `params` key
Currently in services, we don’t make a distinction between input
parameters, options and dependencies.
This can lead to user input modifying the service behavior, whereas it
was not the developer intention.
This patch addresses the issue by changing how data is provided to
services:
- `params` is now used to hold all data coming from outside (typically
user input from a controller) and a contract will take its values from
`params`.
- `options` is a new key to provide options to a service. This typically
allows changing a service behavior at runtime. It is, of course,
totally optional.
- `dependencies` is actually anything else provided to the service (like
`guardian`) and available directly from the context object.
The `service_params` helper in controllers has been updated to reflect
those changes, so most of the existing services didn’t need specific
changes.
The options block has the same DSL as contracts, as it’s also based on
`ActiveModel`. There aren’t any validations, though. Here’s an example:
```ruby
options do
attribute :allow_changing_hidden, :boolean, default: false
end
```
And here’s an example of how to call a service with the new keys:
```ruby
MyService.call(params: { key1: value1, … }, options: { my_option: true }, guardian:, …)
```
2024-10-18 23:45:47 +08:00
|
|
|
# @!visibility private
|
|
|
|
class Options < Step
|
|
|
|
end
|
2024-04-04 21:57:41 +08:00
|
|
|
|
2024-11-09 00:24:40 +08:00
|
|
|
# @!visibility private
|
|
|
|
class Try < Transaction
|
|
|
|
def error
|
|
|
|
step_result.exception.inspect
|
|
|
|
end
|
|
|
|
|
|
|
|
def step_result
|
|
|
|
result["result.#{type}.#{name}"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-04-04 21:57:41 +08:00
|
|
|
attr_reader :steps, :result
|
|
|
|
|
|
|
|
def initialize(result)
|
|
|
|
@steps = result.__steps__.map { Step.for(_1, result).steps }.flatten
|
|
|
|
@result = result
|
|
|
|
end
|
|
|
|
|
|
|
|
# Inspect the provided result object.
|
|
|
|
# Example output:
|
|
|
|
# [1/4] [model] 'channel' ✅
|
DEV: Replace `params` by the contract object in services
This patch replaces the parameters provided to a service through
`params` by the contract object.
That way, it allows better consistency when accessing input params. For
example, if you have a service without a contract, to access a
parameter, you need to use `params[:my_parameter]`. But with a contract,
you do this through `contract.my_parameter`. Now, with this patch,
you’ll be able to access it through `params.my_parameter` or
`params[:my_parameter]`.
Some methods have been added to the contract object to better mimic a
Hash. That way, when accessing/using `params`, you don’t have to think
too much about it:
- `params.my_key` is also accessible through `params[:my_key]`.
- `params.my_key = value` can also be done through `params[:my_key] =
value`.
- `#slice` and `#merge` are available.
- `#to_hash` has been implemented, so the contract object will be
automatically cast as a hash by Ruby depending on the context. For
example, with an AR model, you can do this: `user.update(**params)`.
2024-10-23 23:57:48 +08:00
|
|
|
# [2/4] [params] 'default' ✅
|
2024-04-04 21:57:41 +08:00
|
|
|
# [3/4] [policy] 'check_channel_permission' ❌
|
|
|
|
# [4/4] [step] 'change_status'
|
|
|
|
# @return [String] the steps of the result object with their state
|
|
|
|
def inspect
|
2024-11-09 00:24:40 +08:00
|
|
|
steps
|
|
|
|
.map
|
|
|
|
.with_index do |step, index|
|
|
|
|
"[#{format("%#{steps.size.to_s.size}s", index + 1)}/#{steps.size}] #{step.inspect}"
|
|
|
|
end
|
|
|
|
.join("\n")
|
2024-04-04 21:57:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# @return [String, nil] the first available error, if any.
|
|
|
|
def error
|
|
|
|
steps.detect(&:failure?)&.error
|
|
|
|
end
|
|
|
|
end
|