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

allow multiple states to be defined on one line #288

Merged
merged 3 commits into from
Mar 19, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class Job

aasm do
state :sleeping, :initial => true
state :running
state :cleaning
state :running, :cleaning

event :run do
transitions :from => :sleeping, :to => :running
Expand Down
34 changes: 26 additions & 8 deletions lib/aasm/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,39 @@ def initial_state(new_initial_state=nil)
end

# define a state
def state(name, options={})
@state_machine.add_state(name, @klass, options)

if @klass.instance_methods.include?("#{name}?")
warn "#{@klass.name}: The aasm state name #{name} is already used!"
# args
# [0] state
# [1] options (or nil)
# or
# [0] state
# [1..] state
def state(*args)
if args.last.is_a?(Hash) && args.size == 2
names = [args.first]
options = args.last
elsif
Copy link

Choose a reason for hiding this comment

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

is there a condition missing here?

names = args
options = {}
else
raise "count not parse states: #{args}"
end

Copy link

Choose a reason for hiding this comment

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

I am wondering whether it would make more sense to have what follows as a separate (private) method which is called directly from this method with the correct arguments.

e.g. lines 74-75 becoming new_method_name(args.first, args.last) etc.

@klass.class_eval <<-EORUBY, __FILE__, __LINE__ + 1
names.each do |name|
@state_machine.add_state(name, @klass, options)

if @klass.instance_methods.include?("#{name}?")
warn "#{@klass.name}: The aasm state name #{name} is already used!"
end

@klass.class_eval <<-EORUBY, __FILE__, __LINE__ + 1
def #{name}?
aasm(:#{@name}).current_state == :#{name}
end
EORUBY

unless @klass.const_defined?("STATE_#{name.upcase}")
@klass.const_set("STATE_#{name.upcase}", name)
unless @klass.const_defined?("STATE_#{name.upcase}")
@klass.const_set("STATE_#{name.upcase}", name)
end
end
end

Expand Down
8 changes: 8 additions & 0 deletions spec/models/states_on_one_line_example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class StatesOnOneLineExample
include AASM

aasm :one_line do
state :initial, :initial => true
state :first, :second
end
end
3 changes: 1 addition & 2 deletions spec/unit/readme_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class Job

aasm do
state :sleeping, :initial => true
state :running
state :cleaning
state :running, :cleaning

event :run do
transitions :from => :sleeping, :to => :running
Expand Down
16 changes: 16 additions & 0 deletions spec/unit/states_on_one_line_example_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper'

describe StatesOnOneLineExample do
let(:example) { StatesOnOneLineExample.new }
describe 'on initialize' do
it 'should be in the initial state' do
expect(example.aasm(:one_line).current_state).to eql :initial
end
end

describe 'states' do
it 'should have all 3 states defined' do
expect(example.aasm(:one_line).states.map(&:name)).to eq [:initial, :first, :second]
end
end
end