Skip to content

Commit

Permalink
new dsl, 2nd step: now the old dsl uses the new one
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Oct 23, 2011
1 parent 0d2d9a8 commit 4dcc7a5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 41 deletions.
56 changes: 19 additions & 37 deletions lib/aasm/aasm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ class UndefinedState < RuntimeError
end

def self.included(base) #:nodoc:
base.class_eval do
def self.aasm(&block)
AASM::Base.new(self, &block)
end
end

base.extend AASM::ClassMethods

AASM::Persistence.set_persistence(base)
Expand All @@ -27,61 +21,49 @@ def inherited(klass)
super
end

def aasm(&block)
@aasm ||= AASM::Base.new(self)
@aasm.instance_eval(&block) if block
@aasm
end

def aasm_initial_state(set_state=nil)
if set_state
# deprecated
AASM::StateMachine[self].initial_state = set_state
else
AASM::StateMachine[self].initial_state
end
end

# deprecated
def aasm_initial_state=(state)
AASM::StateMachine[self].initial_state = state
end

# deprecated
def aasm_state(name, options={})
sm = AASM::StateMachine[self]
sm.create_state(name, options)
sm.initial_state = name if options[:initial] || !sm.initial_state

define_method("#{name.to_s}?") do
aasm_current_state == name
end
aasm.state(name, options)
end

# deprecated
def aasm_event(name, options = {}, &block)
sm = AASM::StateMachine[self]

unless sm.events.has_key?(name)
sm.events[name] = AASM::SupportingClasses::Event.new(name, options, &block)
end

# an addition over standard aasm so that, before firing an event, you can ask
# may_event? and get back a boolean that tells you whether the guard method
# on the transition will let this happen.
define_method("may_#{name.to_s}?") do |*args|
aasm_test_event(name, *args)
end

define_method("#{name.to_s}!") do |*args|
aasm_fire_event(name, true, *args)
end

define_method("#{name.to_s}") do |*args|
aasm_fire_event(name, false, *args)
end
aasm.event(name, options, &block)
end

# deprecated
def aasm_states
AASM::StateMachine[self].states
aasm.states
end

# deprecated
def aasm_events
AASM::StateMachine[self].events
aasm.events
end

# deprecated
def aasm_states_for_select
AASM::StateMachine[self].states.map { |state| state.for_select }
aasm.states_for_select
end

def human_event_name(event)
Expand Down Expand Up @@ -132,7 +114,7 @@ def human_state
AASM::Localizer.new.human_state(self)
end

private
private

def set_aasm_current_state_with_persistence(state)
save_success = true
Expand Down
46 changes: 42 additions & 4 deletions lib/aasm/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,53 @@ module AASM
class Base
def initialize(clazz, &block)
@clazz = clazz
instance_eval &block
end

def state(name, options={})
@clazz.aasm_state(name, options)
# @clazz.aasm_state(name, options)
sm = AASM::StateMachine[@clazz]
sm.create_state(name, options)
sm.initial_state = name if options[:initial] || !sm.initial_state

@clazz.send(:define_method, "#{name.to_s}?") do
aasm_current_state == name
end
end

def event(name, options={}, &block)
@clazz.aasm_event(name, options, &block)
# @clazz.aasm_event(name, options, &block)
sm = AASM::StateMachine[@clazz]

unless sm.events.has_key?(name)
sm.events[name] = AASM::SupportingClasses::Event.new(name, options, &block)
end

# an addition over standard aasm so that, before firing an event, you can ask
# may_event? and get back a boolean that tells you whether the guard method
# on the transition will let this happen.
@clazz.send(:define_method, "may_#{name.to_s}?") do |*args|
aasm_test_event(name, *args)
end

@clazz.send(:define_method, "#{name.to_s}!") do |*args|
aasm_fire_event(name, true, *args)
end

@clazz.send(:define_method, "#{name.to_s}") do |*args|
aasm_fire_event(name, false, *args)
end
end

def states
AASM::StateMachine[@clazz].states
end

def events
AASM::StateMachine[@clazz].events
end

def states_for_select
states.map { |state| state.for_select }
end
end
end

0 comments on commit 4dcc7a5

Please sign in to comment.