-
-
Notifications
You must be signed in to change notification settings - Fork 38
Add support for keywords in transition parameters and callback arguments #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,24 +20,24 @@ def initialize(object) | |
| # | ||
| # @api public | ||
| def invert | ||
| ->(*args, &block) { !call(*args, &block) } | ||
| ->(*args, **kwargs, &block) { !call(*args, **kwargs, &block) } | ||
| end | ||
|
|
||
| # Execute action | ||
| # | ||
| # @param [Object] target | ||
| # | ||
| # @api public | ||
| def call(target, *args, &block) | ||
| def call(target, *args, **kwargs, &block) | ||
| case object | ||
| when Symbol | ||
| target.public_send(object.to_sym, *args, &block) | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [93/80] |
||
| value = eval(string) | ||
| target.instance_exec(&value) | ||
| when ::Proc | ||
| object.arity.zero? ? object.call : object.call(target, *args) | ||
| object.arity.zero? ? object.call : object.call(target, *args, **kwargs) | ||
| else | ||
| raise ArgumentError, "Unknown callable #{object}" | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,9 +47,9 @@ def apply(event_name, silent = false) | |
| # | ||
| # @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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [85/80] |
||
| method = silent ? :transition : :trigger | ||
| machine.public_send(method, event_name, *data, &block) | ||
| machine.public_send(method, event_name, *args, **kwargs, &block) | ||
| end | ||
| end | ||
|
|
||
|
|
@@ -65,9 +65,9 @@ def define_event_transition(event_name, silent) | |
| # | ||
| # @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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [91/80] |
||
| method = silent ? :transition! : :trigger! | ||
| machine.public_send(method, event_name, *data, &block) | ||
| machine.public_send(method, event_name, *args, **kwargs, &block) | ||
| end | ||
| end | ||
| end # EventBuilder | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,8 +21,8 @@ def on_delivery(&block) | |
| # Invoke event handler | ||
| # | ||
| # @api private | ||
| 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. Choose a reason for hiding this commentThe 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. |
||
| end | ||
| alias handle_delivery call | ||
| end # Listener | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,44 +76,44 @@ module Once; end | |
|
|
||
| module Async; end | ||
|
|
||
| def on_enter(*args, &callback) | ||
| on HookEvent::Enter, *args, &callback | ||
| def on_enter(*args, **kwargs, &callback) | ||
| on HookEvent::Enter, *args, **kwargs, &callback | ||
| end | ||
|
|
||
| def on_transition(*args, &callback) | ||
| on HookEvent::Transition, *args, &callback | ||
| def on_transition(*args, **kwargs, &callback) | ||
| on HookEvent::Transition, *args, **kwargs, &callback | ||
| end | ||
|
|
||
| def on_exit(*args, &callback) | ||
| on HookEvent::Exit, *args, &callback | ||
| def on_exit(*args, **kwargs, &callback) | ||
| on HookEvent::Exit, *args, **kwargs, &callback | ||
| end | ||
|
|
||
| def once_on_enter(*args, &callback) | ||
| on HookEvent::Enter, *args, &callback.extend(Once) | ||
| def once_on_enter(*args, **kwargs, &callback) | ||
| on HookEvent::Enter, *args, **kwargs, &callback.extend(Once) | ||
| end | ||
|
|
||
| def once_on_transition(*args, &callback) | ||
| on HookEvent::Transition, *args, &callback.extend(Once) | ||
| def once_on_transition(*args, **kwargs, &callback) | ||
| on HookEvent::Transition, *args, **kwargs, &callback.extend(Once) | ||
| end | ||
|
|
||
| def once_on_exit(*args, &callback) | ||
| on HookEvent::Exit, *args, &callback.extend(Once) | ||
| def once_on_exit(*args, **kwargs, &callback) | ||
| on HookEvent::Exit, *args, **kwargs, &callback.extend(Once) | ||
| end | ||
|
|
||
| def on_before(*args, &callback) | ||
| on HookEvent::Before, *args, &callback | ||
| def on_before(*args, **kwargs, &callback) | ||
| on HookEvent::Before, *args, **kwargs, &callback | ||
| end | ||
|
|
||
| def on_after(*args, &callback) | ||
| on HookEvent::After, *args, &callback | ||
| def on_after(*args, **kwargs, &callback) | ||
| on HookEvent::After, *args, **kwargs, &callback | ||
| end | ||
|
|
||
| def once_on_before(*args, &callback) | ||
| on HookEvent::Before, *args, &callback.extend(Once) | ||
| def once_on_before(*args, **kwargs, &callback) | ||
| on HookEvent::Before, *args, **kwargs, &callback.extend(Once) | ||
| end | ||
|
|
||
| def once_on_after(*args, &callback) | ||
| on HookEvent::After, *args, &callback.extend(Once) | ||
| def once_on_after(*args, **kwargs, &callback) | ||
| on HookEvent::After, *args, **kwargs, &callback.extend(Once) | ||
| end | ||
|
|
||
| # Execute each of the hooks in order with supplied data | ||
|
|
@@ -126,13 +126,13 @@ def once_on_after(*args, &callback) | |
| # @return [nil] | ||
| # | ||
| # @api public | ||
| def emit(event, *data) | ||
| def emit(event, *args, **kwargs) | ||
| sync_exclusive do | ||
| [event.type].each do |hook_type| | ||
| any_state_or_event = HookEvent.any_state_or_event(hook_type) | ||
| [any_state_or_event, event.name].each do |event_name| | ||
| hooks[hook_type][event_name].each do |hook| | ||
| handle_callback(hook, event, *data) | ||
| handle_callback(hook, event, *args, **kwargs) | ||
| off(hook_type, event_name, &hook) if hook.is_a?(Once) | ||
| end | ||
| end | ||
|
|
@@ -166,23 +166,23 @@ def cancel_event(msg = nil) | |
| # @param [Array[Object]] :data | ||
| # | ||
| # @api private | ||
| def handle_callback(hook, event, *data) | ||
| to = machine.events_map.move_to(event.event_name, event.from, *data) | ||
| def handle_callback(hook, event, *args, **kwargs) | ||
| to = machine.events_map.move_to(event.event_name, event.from, *args, **kwargs) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [84/80] |
||
| trans_event = TransitionEvent.new(event.event_name, event.from, to) | ||
| callable = create_callable(hook) | ||
|
|
||
| if hook.is_a?(Async) | ||
| defer(callable, trans_event, *data) | ||
| defer(callable, trans_event, *args, **kwargs) | ||
| else | ||
| callable.(trans_event, *data) | ||
| callable.(trans_event, *args, **kwargs) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/LambdaCall: Prefer the use of lambda.call(...) over lambda.(...). |
||
| end | ||
| end | ||
|
|
||
| # Defer callback execution | ||
| # | ||
| # @api private | ||
| def defer(callable, trans_event, *data) | ||
| async_call = AsyncCall.new(machine, callable, trans_event, *data) | ||
| def defer(callable, trans_event, *args, **kwargs) | ||
| async_call = AsyncCall.new(machine, callable, trans_event, *args, **kwargs) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Metrics/LineLength: Line is too long. [81/80] |
||
| callback_queue.start unless callback_queue.running? | ||
| callback_queue << async_call | ||
| end | ||
|
|
@@ -220,8 +220,8 @@ def cleanup_callback_queue | |
| # | ||
| # @api private | ||
| def create_callable(hook) | ||
| callback = proc do |trans_event, *data| | ||
| machine.instance_exec(trans_event, *data, &hook) | ||
| callback = proc do |trans_event, *args, **kwargs| | ||
| machine.instance_exec(trans_event, *args, **kwargs, &hook) | ||
| end | ||
| Callable.new(callback) | ||
| end | ||
|
|
@@ -245,10 +245,10 @@ def callback_names | |
| # @return [self] | ||
| # | ||
| # @api private | ||
| def method_missing(method_name, *args, &block) | ||
| def method_missing(method_name, *args, **kwargs, &block) | ||
| _, event_name, callback_name = *method_name.to_s.match(/^(\w*?on_\w+?)_(\w+)$/) | ||
| if callback_name && callback_names.include?(callback_name.to_sym) | ||
| public_send(event_name, :"#{callback_name}", *args, &block) | ||
| public_send(event_name, :"#{callback_name}", *args, **kwargs, &block) | ||
| else | ||
| super | ||
| end | ||
|
|
||
There was a problem hiding this comment.
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.