extracted dsl helper from event

This commit is contained in:
Vladimir Meremyanin 2013-03-06 14:09:19 +04:00
parent 2c69f387fd
commit 754e82cfa6
3 changed files with 34 additions and 10 deletions

View File

@ -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')

30
lib/aasm/dsl_helper.rb Normal file
View File

@ -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

View File

@ -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