diff --git a/README.md b/README.md index 4a488a8..1367cfa 100644 --- a/README.md +++ b/README.md @@ -750,6 +750,11 @@ Job.aasm.states_for_select ``` +### RubyMotion support + +To use AASM with a RubyMotion project, use it with the [motion-bundler](https://github.com/archan937/motion-bundler) gem. + + ### Testing AASM provides some matchers for [RSpec](http://rspec.info): `transition_from`, `have_state`, `allow_event` and `allow_transition_to`. Add `require 'aasm/rspec'` to your `spec_helper.rb` file and use them like this @@ -815,7 +820,7 @@ After installing Aasm you can run generator: ```sh % rails generate aasm NAME [COLUMN_NAME] ``` -Replace NAME with the Model name, COLUMN_NAME is optional(default is 'aasm_state'). +Replace NAME with the Model name, COLUMN_NAME is optional(default is 'aasm_state'). This will create a model (if one does not exist) and configure it with aasm block. For Active record orm a migration file is added to add aasm state column to table. diff --git a/lib/aasm.rb b/lib/aasm.rb index 701fd19..3c88f90 100644 --- a/lib/aasm.rb +++ b/lib/aasm.rb @@ -12,4 +12,5 @@ require 'aasm/core/state' require 'aasm/localizer' require 'aasm/state_machine' require 'aasm/persistence' +require 'aasm/persistence/plain_persistence' # RubyMotion support require 'aasm/aasm' diff --git a/lib/aasm/base.rb b/lib/aasm/base.rb index 35cd5b3..6a912e9 100644 --- a/lib/aasm/base.rb +++ b/lib/aasm/base.rb @@ -31,17 +31,16 @@ module AASM # make sure to raise an error if no_direct_assignment is enabled # and attribute is directly assigned though - @klass.class_eval %Q( - def #{@state_machine.config.column}=(state_name) - if self.class.aasm(:#{@name}).state_machine.config.no_direct_assignment - raise AASM::NoDirectAssignmentError.new( - 'direct assignment of AASM column has been disabled (see AASM configuration for this class)' - ) - else - super - end + aasm_name = @name + @klass.send :define_method, "#{@state_machine.config.column}=", ->(state_name) do + if self.class.aasm(:"#{aasm_name}").state_machine.config.no_direct_assignment + raise AASM::NoDirectAssignmentError.new( + 'direct assignment of AASM column has been disabled (see AASM configuration for this class)' + ) + else + super(state_name) end - ) + end end # This method is both a getter and a setter @@ -70,11 +69,10 @@ module AASM 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 + aasm_name = @name + @klass.send :define_method, "#{name}?", ->() do + aasm(:"#{aasm_name}").current_state == :"#{name}" + end unless @klass.const_defined?("STATE_#{name.upcase}") @klass.const_set("STATE_#{name.upcase}", name) @@ -92,21 +90,21 @@ module AASM # 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. - @klass.class_eval <<-EORUBY, __FILE__, __LINE__ + 1 - def may_#{name}?(*args) - aasm(:#{@name}).may_fire_event?(:#{name}, *args) - end + aasm_name = @name - def #{name}!(*args, &block) - aasm(:#{@name}).current_event = :#{name}! - aasm_fire_event(:#{@name}, :#{name}, {:persist => true}, *args, &block) - end + @klass.send :define_method, "may_#{name}?", ->(*args) do + aasm(:"#{aasm_name}").may_fire_event?(:"#{name}", *args) + end - def #{name}(*args, &block) - aasm(:#{@name}).current_event = :#{name} - aasm_fire_event(:#{@name}, :#{name}, {:persist => false}, *args, &block) - end - EORUBY + @klass.send :define_method, "#{name}!", ->(*args, &block) do + aasm(:"#{aasm_name}").current_event = :"#{name}!" + aasm_fire_event(:"#{aasm_name}", :"#{name}", {:persist => true}, *args, &block) + end + + @klass.send :define_method, "#{name}", ->(*args, &block) do + aasm(:"#{aasm_name}").current_event = :"#{name}" + aasm_fire_event(:"#{aasm_name}", :"#{name}", {:persist => false}, *args, &block) + end end def after_all_transitions(*callbacks, &block) diff --git a/lib/aasm/persistence.rb b/lib/aasm/persistence.rb index 3405f28..fca25f7 100644 --- a/lib/aasm/persistence.rb +++ b/lib/aasm/persistence.rb @@ -23,7 +23,7 @@ module AASM def include_persistence(base, type) require File.join(File.dirname(__FILE__), 'persistence', "#{type}_persistence") - base.send(:include, constantize("AASM::Persistence::#{capitalize(type)}Persistence")) + base.send(:include, constantize("#{capitalize(type)}Persistence")) end def capitalize(string_or_symbol) @@ -31,7 +31,7 @@ module AASM end def constantize(string) - instance_eval(string) + AASM::Persistence.const_get(string) end end # class << self