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

Add graph label option to transitions #345

Open
wants to merge 3 commits 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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,24 @@ class Vehicle
end
```

#### Transition labels

If you would prefer a custom label on a graphviz generated graph, specify
the `:graph_label` option on the transition statement.

For example:

```ruby

class Vehicle
state_machine :initial => :parked do

state :parked do
transition :to => :idling, :on => [:ignite, :shift_up], :if => :seatbelt_on?, :graph_label => "Seatbelt is ON"
end
end
```

#### Transition context

Some flexibility is provided around the context in which transitions can be
Expand Down
6 changes: 5 additions & 1 deletion lib/state_machine/branch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ class Branch
# * +from+ / +except_from+
# * +to+ / +except_to+
attr_reader :known_states


# An optional, custom, label to add to the graph when drawing the branch.
attr_reader :graph_label

# Creates a new branch
def initialize(options = {}) #:nodoc:
# Build conditionals
@if_condition = options.delete(:if)
@unless_condition = options.delete(:unless)
@graph_label = options.delete(:graph_label)

# Build event requirement
@event_requirement = build_matcher(options, :on, :except_on)
Expand Down
10 changes: 8 additions & 2 deletions lib/state_machine/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def reset
@branches = []
@known_states = []
end

# Draws a representation of this event on the given graph. This will
# create 1 or more edges on the graph for each branch (i.e. transition)
# configured.
Expand All @@ -205,12 +205,18 @@ def reset
def draw(graph, options = {})
valid_states = machine.states.by_priority.map {|state| state.name}
branches.each do |branch|
branch.draw(graph, options[:human_name] ? human_name : name, valid_states)
branch.draw(graph, graph_label(branch, options), valid_states)
end

true
end

def graph_label(branch, options = {})
graph_label_name = (options[:human_name] ? human_name : name).to_s

branch.graph_label ? branch.graph_label : graph_label_name
end

# Generates a nicely formatted description of this event's contents.
#
# For example,
Expand Down
2 changes: 1 addition & 1 deletion lib/state_machine/graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def v0_api?

# The ruby-graphviz version data
def version
Constants::RGV_VERSION.split('.')
GraphViz::Constants::RGV_VERSION.split('.')
end
end
end
2 changes: 2 additions & 0 deletions state_machine.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Gem::Specification.new do |s|
s.extra_rdoc_files = %w(README.md CHANGELOG.md LICENSE)

s.add_development_dependency("rake")
s.add_development_dependency("test-unit")
s.add_development_dependency("simplecov")
s.add_development_dependency("ruby-graphviz")
s.add_development_dependency("appraisal", "~> 0.5.0")
end
22 changes: 22 additions & 0 deletions test/unit/event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,28 @@ def test_should_use_event_name_for_edge_label
end
end

class EventDrawingWithCustomBranchLabelTest < Test::Unit::TestCase
def setup
states = [:parked, :idling]

@machine = StateMachine::Machine.new(Class.new, :initial => :parked)
@machine.other_states(*states)

graph = StateMachine::Graph.new('test')
states.each {|state| graph.add_nodes(state.to_s)}

@machine.events << @event = StateMachine::Event.new(@machine , :park, :human_name => 'Park')
@event.transition :parked => :idling, :graph_label => "extra graph label"

@event.draw(graph, :human_name => true)
@edge = graph.get_edge_at_index(0)
end

def test_should_use_event_human_name_for_edge_label
assert_equal 'extra graph label', @edge['label'].to_s.gsub('"', '')
end
end

class EventDrawingWithHumanNameTest < Test::Unit::TestCase
def setup
states = [:parked, :idling]
Expand Down
2 changes: 1 addition & 1 deletion test/unit/machine_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3261,7 +3261,7 @@ def test_should_allow_orientation_to_be_portrait
assert_equal 'TB', graph['rankdir'].to_s.gsub('"', '')
end

if Constants::RGV_VERSION != '0.9.0'
if GraphViz::Constants::RGV_VERSION != '0.9.0'
def test_should_allow_human_names_to_be_displayed
@machine.event :ignite, :human_name => 'Ignite'
@machine.state :parked, :human_name => 'Parked'
Expand Down