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
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
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'upstream/master' into add_states_on_one…
…_line

- also fix typo and move arg deconstruction to private method
  • Loading branch information
HParker committed Mar 16, 2016
commit 4355aafa64e8fc97f4d9f06cc67357ea48c81ab6
40 changes: 21 additions & 19 deletions lib/aasm/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,31 +78,23 @@ def initial_state(new_initial_state=nil)
# [0] state
# [1..] state
def state(*args)
if args.last.is_a?(Hash) && args.size == 2
names = [args.first]
options = args.last
elsif
names = args
options = {}
else
raise "count not parse states: #{args}"
end

names, options = interpret_state_args(args)
puts names.inspect
puts options.inspect
names.each do |name|
@state_machine.add_state(name, @klass, 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!"
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}
aasm_name = @name
klass.send :define_method, "#{name}?", ->() do
aasm(:"#{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 Expand Up @@ -184,6 +176,7 @@ def from_states_for_state(state, options={})
if options[:transition]
@state_machine.events[options[:transition]].transitions_to_state(state).flatten.map(&:from).flatten
else

events.map {|e| e.transitions_to_state(state)}.flatten.map(&:from).flatten
end
end
Expand All @@ -202,5 +195,14 @@ def configure(key, default_value)
end
end

def interpret_state_args(args)
if args.last.is_a?(Hash) && args.size == 2
[[args.first], args.last]
elsif args.size > 0
[args, {}]
else
raise "count not parse states: #{args}"
end
end
end
end
You are viewing a condensed version of this merge commit. You can view the full changes here.