Skip to content

Add support for keywords in transition parameters and callback arguments#79

Open
rolftimmermans wants to merge 2 commits intopiotrmurach:masterfrom
rolftimmermans:master
Open

Add support for keywords in transition parameters and callback arguments#79
rolftimmermans wants to merge 2 commits intopiotrmurach:masterfrom
rolftimmermans:master

Conversation

@rolftimmermans
Copy link

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:

fm = FiniteMachine.new do
  event :ready, :red => :yellow

  on_before_ready do |event, params|
    puts "lights switching in #{params[:time]} seconds"
  end
end

fm.ready(time: 3)

After:

fm = FiniteMachine.new do
  event :ready, :red => :yellow

  on_before_ready do |event, time:|
    puts "lights switching in #{time} seconds"
  end
end

fm.ready(time: 3)

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

  • Tests written & passing locally?
  • Code style checked?
  • Rebased with master branch?
  • Documentation updated?
  • Changelog updated?

Note that though I verified the code style, lots of issues are flagged by Rubocop because of newer defaults.

gem "ruby-prof", "~> 0.17.0", platforms: :mri
gem "pry", "~> 0.10.1"
gem "ruby-prof", platforms: :mri
gem "pry"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}) }"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceBeforeComma: Space found before comma.

on_before :go , &callback

on_after :slow , &callback
on_after :stop , &callback

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceBeforeComma: Space found before comma.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants