Add support for keywords in transition parameters and callback arguments#79
Add support for keywords in transition parameters and callback arguments#79rolftimmermans wants to merge 2 commits intopiotrmurach:masterfrom
Conversation
| gem "ruby-prof", "~> 0.17.0", platforms: :mri | ||
| gem "pry", "~> 0.10.1" | ||
| gem "ruby-prof", platforms: :mri | ||
| gem "pry" |
There was a problem hiding this comment.
Bundler/OrderedGems: Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem pry should appear before ruby-prof.
| target.public_send(object.to_sym, *args, **kwargs, &block) | ||
| when String | ||
| string = args.empty? ? "-> { #{object} }" : "-> { #{object}(*#{args}) }" | ||
| string = args.empty? ? "-> { #{object} }" : "-> { #{object}(*#{args}, **#{kwargs}) }" |
There was a problem hiding this comment.
Metrics/LineLength: Line is too long. [93/80]
| # @api private | ||
| def define_event_bang(event_name, silent) | ||
| machine.send(:define_singleton_method, "#{event_name}!") do |*data, &block| | ||
| machine.send(:define_singleton_method, "#{event_name}!") do |*args, **kwargs, &block| |
There was a problem hiding this comment.
Metrics/LineLength: Line is too long. [91/80]
| # @api private | ||
| def define_event_transition(event_name, silent) | ||
| machine.send(:define_singleton_method, event_name) do |*data, &block| | ||
| machine.send(:define_singleton_method, event_name) do |*args, **kwargs, &block| |
There was a problem hiding this comment.
Metrics/LineLength: Line is too long. [85/80]
| def call(*args) | ||
| @on_delivery.call(*args) if @on_delivery | ||
| def call(*args, **kwargs) | ||
| @on_delivery.call(*args, **kwargs) if @on_delivery |
There was a problem hiding this comment.
Style/SafeNavigation: Use safe navigation (&.) instead of checking if an object exists before calling the method.
| expected = {name: :stop, from: :yellow, to: :red, a: "foo", b: "bar"} | ||
| fsm.stop(a: "foo", b: "bar") | ||
|
|
||
| expected = {name: :ready, from: :red, to: :yellow, a: :foo, b: :bar} |
There was a problem hiding this comment.
Layout/SpaceInsideHashLiteralBraces: Space inside { missing.
Layout/SpaceInsideHashLiteralBraces: Space inside } missing.
| expected = {name: :slow, from: :green, to: :yellow, a: 1, b: 2, c: 3} | ||
| fsm.slow(a: 1, b: 2, c: 3) | ||
|
|
||
| expected = {name: :stop, from: :yellow, to: :red, a: "foo", b: "bar"} |
There was a problem hiding this comment.
Layout/SpaceInsideHashLiteralBraces: Space inside { missing.
Layout/SpaceInsideHashLiteralBraces: Space inside } missing.
| on_after :go , &callback | ||
| end | ||
|
|
||
| expected = {name: :slow, from: :green, to: :yellow, a: 1, b: 2, c: 3} |
There was a problem hiding this comment.
Layout/SpaceInsideHashLiteralBraces: Space inside { missing.
Layout/SpaceInsideHashLiteralBraces: Space inside } missing.
| on_after :slow , &callback | ||
| on_after :stop , &callback | ||
| on_after :ready, &callback | ||
| on_after :go , &callback |
There was a problem hiding this comment.
Layout/SpaceBeforeComma: Space found before comma.
| on_before :go , &callback | ||
|
|
||
| on_after :slow , &callback | ||
| on_after :stop , &callback |
There was a problem hiding this comment.
Layout/SpaceBeforeComma: Space found before comma.
Describe the change
Callback parameters currently cannot accept keyword arguments; any keyword parameters to a transition are converted to a hash. While it is workable, having mandatory keyword parameters and the ability to destructure keywords is very helpful.
Why are we doing this?
Before:
After:
Benefits
Greater usability for state machines that rely on multiple arguments, in particular if some are optional. Positional arguments are very hard to read and use in certain cases. Existing code should continue to work as is.
Drawbacks
I honestly can't think of any, except for merging this PR and releasing a new version since a long time?
Requirements
masterbranch?Note that though I verified the code style, lots of issues are flagged by Rubocop because of newer defaults.