2011-11-26 17:30:47 +00:00
|
|
|
module AASM
|
|
|
|
module Persistence
|
2013-02-22 08:40:01 +00:00
|
|
|
class << self
|
2009-04-09 05:25:16 +00:00
|
|
|
|
2013-02-22 08:40:01 +00:00
|
|
|
def load_persistence(base)
|
|
|
|
# Use a fancier auto-loading thingy, perhaps. When there are more persistence engines.
|
|
|
|
hierarchy = base.ancestors.map {|klass| klass.to_s}
|
|
|
|
|
|
|
|
if hierarchy.include?("ActiveRecord::Base")
|
2016-03-19 12:42:57 +00:00
|
|
|
require_persistence :active_record
|
2014-11-26 18:39:57 +00:00
|
|
|
include_persistence base, :active_record
|
2013-02-22 08:40:01 +00:00
|
|
|
elsif hierarchy.include?("Mongoid::Document")
|
2016-03-19 12:42:57 +00:00
|
|
|
require_persistence :mongoid
|
2014-11-26 18:39:57 +00:00
|
|
|
include_persistence base, :mongoid
|
Add [sequel](http://sequel.jeremyevans.net/) support.
I am trying to migrate some activerecord code to sequel, and this makes
aasm work for sequel out of the box. This is not yet feature complete
as for activerecord, missing some scope related features. However this
could be a start, and to be honest, since we're not using them, this is
far good enough for us.
I have tried my best to make this aligned with activerecord and mongoid,
and I've overridden `aasm_read_state` for sequel, because there's no
`new_record?` but only `new?` in sequel, and there's no `blank?`,
either. (which I believe is provided in activesupport, which sequel
does not depend on, and I don't want to force people to depend on that.)
Thanks for considering.
2014-04-22 14:28:08 +00:00
|
|
|
elsif hierarchy.include?("Sequel::Model")
|
2016-03-19 12:42:57 +00:00
|
|
|
require_persistence :sequel
|
2014-11-26 18:39:57 +00:00
|
|
|
include_persistence base, :sequel
|
2015-12-31 18:47:55 +00:00
|
|
|
elsif hierarchy.include?("Dynamoid::Document")
|
2016-03-19 12:42:57 +00:00
|
|
|
require_persistence :dynamoid
|
2015-12-31 18:47:55 +00:00
|
|
|
include_persistence base, :dynamoid
|
2014-12-03 00:33:39 +00:00
|
|
|
elsif hierarchy.include?("Redis::Objects")
|
2016-03-19 12:42:57 +00:00
|
|
|
require_persistence :redis
|
2014-12-03 00:33:39 +00:00
|
|
|
include_persistence base, :redis
|
2016-03-18 01:53:19 +00:00
|
|
|
elsif hierarchy.include?("CDQManagedObject")
|
|
|
|
include_persistence base, :core_data_query
|
2014-11-26 18:23:54 +00:00
|
|
|
else
|
2014-11-26 18:39:57 +00:00
|
|
|
include_persistence base, :plain
|
2013-02-22 08:40:01 +00:00
|
|
|
end
|
2011-11-26 17:30:47 +00:00
|
|
|
end
|
2013-02-22 08:40:01 +00:00
|
|
|
|
2014-11-26 18:39:57 +00:00
|
|
|
private
|
2013-02-22 08:40:01 +00:00
|
|
|
|
2016-03-18 12:36:28 +00:00
|
|
|
def require_persistence(type)
|
2014-11-26 18:39:57 +00:00
|
|
|
require File.join(File.dirname(__FILE__), 'persistence', "#{type}_persistence")
|
2016-03-18 12:36:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_persistence(base, type)
|
2016-01-30 23:25:42 +00:00
|
|
|
base.send(:include, constantize("#{capitalize(type)}Persistence"))
|
2014-11-26 18:39:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def capitalize(string_or_symbol)
|
|
|
|
string_or_symbol.to_s.split('_').map {|segment| segment[0].upcase + segment[1..-1]}.join('')
|
|
|
|
end
|
|
|
|
|
|
|
|
def constantize(string)
|
2016-01-30 23:25:42 +00:00
|
|
|
AASM::Persistence.const_get(string)
|
2013-02-22 08:40:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end # class << self
|
2008-02-22 20:46:31 +00:00
|
|
|
end
|
2011-11-26 17:30:47 +00:00
|
|
|
end # AASM
|