1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00
aasm/lib/aasm/persistence.rb
2014-11-26 19:39:57 +01:00

37 lines
1.1 KiB
Ruby

module AASM
module Persistence
class << self
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")
include_persistence base, :active_record
elsif hierarchy.include?("Mongoid::Document")
include_persistence base, :mongoid
elsif hierarchy.include?("Sequel::Model")
include_persistence base, :sequel
else
include_persistence base, :plain
end
end
private
def include_persistence(base, type)
require File.join(File.dirname(__FILE__), 'persistence', "#{type}_persistence")
base.send(:include, constantize("AASM::Persistence::#{capitalize(type)}Persistence"))
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)
instance_eval(string)
end
end # class << self
end
end # AASM