extracted dsl helper from event
This commit is contained in:
parent
2c69f387fd
commit
754e82cfa6
|
@ -4,6 +4,7 @@ require 'ostruct'
|
|||
require File.join(File.dirname(__FILE__), 'aasm', 'version')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'errors')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'base')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'dsl_helper')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'instance_base')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'transition')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'event')
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
module DslHelper
|
||||
|
||||
class Proxy
|
||||
attr_accessor :options
|
||||
|
||||
def initialize(options, valid_keys, source)
|
||||
@valid_keys = valid_keys
|
||||
@source = source
|
||||
|
||||
@options = options
|
||||
end
|
||||
|
||||
def method_missing(name, *args, &block)
|
||||
if @valid_keys.include?(name)
|
||||
options[name] = Array(options[name])
|
||||
options[name] << block if block
|
||||
options[name] += Array(args)
|
||||
else
|
||||
@source.send name, *args, &block
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def add_options_from_dsl(options, valid_keys, &block)
|
||||
proxy = Proxy.new(options, valid_keys, self)
|
||||
proxy.instance_eval(&block)
|
||||
proxy.options
|
||||
end
|
||||
|
||||
end
|
|
@ -1,13 +1,14 @@
|
|||
module AASM
|
||||
class Event
|
||||
include DslHelper
|
||||
|
||||
attr_reader :name, :options
|
||||
|
||||
def initialize(name, options = {}, &block)
|
||||
@name = name
|
||||
@transitions = []
|
||||
@options = options
|
||||
instance_eval(&block) if block
|
||||
@options = options # QUESTION: .dup ?
|
||||
add_options_from_dsl(@options, [:after, :before, :error, :success], &block) if block
|
||||
end
|
||||
|
||||
# a neutered version of fire - it doesn't actually fire the event, it just
|
||||
|
@ -110,13 +111,5 @@ module AASM
|
|||
end
|
||||
@transitions
|
||||
end
|
||||
|
||||
[:after, :before, :error, :success].each do |callback_name|
|
||||
define_method callback_name do |*args, &block|
|
||||
options[callback_name] = Array(options[callback_name])
|
||||
options[callback_name] << block if block
|
||||
options[callback_name] += Array(args)
|
||||
end
|
||||
end
|
||||
end
|
||||
end # AASM
|
||||
|
|
Loading…
Reference in New Issue