Skip to content
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

Introduce names for FiniteMachine instances #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## [v0.14.1] - unreleased

### Added
* Add ability to name a FiniteMachine instance

## [v0.14.0] - 2020-09-12

### Added
Expand Down Expand Up @@ -312,6 +317,7 @@

* Initial release

[v0.14.1]: https://github.com/piotrmurach/finite_machine/compare/v0.14.0...v0.14.1
[v0.14.0]: https://github.com/piotrmurach/finite_machine/compare/v0.13.0...v0.14.0
[v0.13.0]: https://github.com/piotrmurach/finite_machine/compare/v0.12.1...v0.13.0
[v0.12.1]: https://github.com/piotrmurach/finite_machine/compare/v0.12.0...v0.12.1
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -710,14 +710,26 @@ fm.stop # callbacks are fired

### 3.7 Logging transitions

To help debug your state machine, **FiniteMachine** provides `:log_transitions` option.
To help debug your state machine, **FiniteMachine** provides a `:log_transitions` option.

```ruby
FiniteMachine.new(log_transitions: true) do
...
end
```

A FiniteMachine instance can optionally be given a name:

```ruby
fm = FiniteMachine.new(name: 'TrafficLights', log_transitions: true) do
...
end
```

The name will then appear in transition logs which helps disambiguate events relevant to different state machines.

Note that if no name is specified then the first 8 characters from a randomly generated UUID string are used as a default name.

### 3.8 Conditional transitions

Each event takes an optional `:if` and `:unless` options which act as a predicate for the transition. The `:if` and `:unless` can take a symbol, a string, a Proc or an array. Use `:if` option when you want to specify when the transition **should** happen. If you want to specify when the transition **should not** happen then use `:unless` option.
Expand Down
4 changes: 2 additions & 2 deletions lib/finite_machine/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def format_error(error)
message.join
end

def report_transition(name, from, to, *args)
message = ["Transition: @event=#{name} "]
def report_transition(machine_name, event_name, from, to, *args)
message = ["Transition: @machine=#{machine_name} @event=#{event_name} "]
unless args.empty?
message << "@with=[#{args.join(',')}] "
end
Expand Down
4 changes: 3 additions & 1 deletion lib/finite_machine/state_machine.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "forwardable"
require "securerandom"

require_relative "catchable"
require_relative "dsl"
Expand Down Expand Up @@ -95,6 +96,7 @@ def initialize(*args, &block)
@events_map = EventsMap.new
@env = Env.new(self, [])
@dsl = DSL.new(self, options)
@name = options.fetch(:name) { SecureRandom.uuid.split("-")[0] }

env.target = args.pop unless args.empty?
env.aliases << options[:alias_target] if options[:alias_target]
Expand Down Expand Up @@ -356,7 +358,7 @@ def transition!(event_name, *data, &block)
block.call(from_state, to_state) if block

if log_transitions
Logger.report_transition(event_name, from_state, to_state, *data)
Logger.report_transition(@name, event_name, from_state, to_state, *data)
end

try_trigger(event_name) { transition_to!(to_state) }
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/log_transitions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
after { FiniteMachine.logger = ::Logger.new($stderr) }

it "logs transitions" do
fsm = FiniteMachine.new log_transitions: true do
fsm = FiniteMachine.new name: "TrafficLights", log_transitions: true do
Copy link
Owner

Choose a reason for hiding this comment

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

We need a test case when the name is not specified. I'd probably keep this test as it was without the name and test the short UUID generation. Let me know if you need any pointers on how to test this.

Copy link
Author

Choose a reason for hiding this comment

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

Understood. I'll give it a shot and reach out if I run into trouble. Thanks.

Copy link
Author

Choose a reason for hiding this comment

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

Hi @piotrmurach: Just wanted to chime in and let you know that I got distracted but will get back to this in the next couple of days. Thanks.

initial :green

event :slow, :green => :yellow
Expand All @@ -15,10 +15,10 @@

fsm.slow
output.rewind
expect(output.read).to match(/Transition: @event=slow green -> yellow/)
expect(output.read).to match(/Transition: @machine=TrafficLights @event=slow green -> yellow/)

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. [98/80]


fsm.stop(1, 2)
output.rewind
expect(output.read).to match(/Transition: @event=stop @with=\[1,2\] yellow -> red/)
expect(output.read).to match(/Transition: @machine=TrafficLights @event=stop @with=\[1,2\] yellow -> red/)

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. [110/80]

end
end
4 changes: 2 additions & 2 deletions spec/unit/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
end

it "reports transition" do
logger.report_transition(:go, :red, :green)
logger.report_transition('TrafficLights', :go, :red, :green)

expect(log).to have_received(:info).with("Transition: @event=go red -> green")
expect(log).to have_received(:info).with("Transition: @machine=TrafficLights @event=go red -> green")

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. [105/80]

end
end