1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

simplified persistence loading (refactoring)

This commit is contained in:
Thorsten Böttger 2014-11-26 19:39:57 +01:00
parent b2dfee3209
commit 1b150427fd
4 changed files with 22 additions and 13 deletions

View file

@ -7,26 +7,29 @@ module AASM
hierarchy = base.ancestors.map {|klass| klass.to_s}
if hierarchy.include?("ActiveRecord::Base")
require_files_for(:active_record)
base.send(:include, AASM::Persistence::ActiveRecordPersistence)
include_persistence base, :active_record
elsif hierarchy.include?("Mongoid::Document")
require_files_for(:mongoid)
base.send(:include, AASM::Persistence::MongoidPersistence)
include_persistence base, :mongoid
elsif hierarchy.include?("Sequel::Model")
require_files_for(:sequel)
base.send(:include, AASM::Persistence::SequelPersistence)
include_persistence base, :sequel
else
require File.join(File.dirname(__FILE__), 'persistence', 'plain_persistence')
base.send(:include, AASM::Persistence::PlainPersistence)
include_persistence base, :plain
end
end
private
private
def require_files_for(persistence)
['base', "#{persistence}_persistence"].each do |file_name|
require File.join(File.dirname(__FILE__), 'persistence', file_name)
end
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

View file

@ -1,3 +1,5 @@
require_relative 'base'
module AASM
module Persistence
module ActiveRecordPersistence

View file

@ -1,3 +1,5 @@
require_relative 'base'
module AASM
module Persistence
module MongoidPersistence

View file

@ -1,3 +1,5 @@
require_relative 'base'
module AASM
module Persistence
module SequelPersistence