Browse files

added a Factory to create the adapter in Adapter::Base class

still need the load method but better placed in the Factory code instead of rolify class methods
refs #47
  • Loading branch information...
1 parent 7da1e09 commit 8f80f8782dc467b31095ba6ff7491fdd1108eab5 @EppO EppO committed Apr 4, 2012
Showing with 8 additions and 4 deletions.
  1. +3 −4 lib/rolify.rb
  2. +5 −0 lib/rolify/adapters/base.rb
View
7 lib/rolify.rb
@@ -6,6 +6,7 @@
require 'rolify/configure'
require 'rolify/dynamic'
require 'rolify/resource'
+require 'rolify/adapters/base'
module Rolify
extend Configure
@@ -20,8 +21,7 @@ def rolify(options = { :role_cname => 'Role' })
rolify_options.merge!({ :join_table => "#{self.to_s.tableize}_#{options[:role_cname].tableize}" }) if Rolify.orm == "active_record"
has_and_belongs_to_many :roles, rolify_options
- load "rolify/adapters/#{Rolify.orm}/role_adapter.rb"
- self.adapter = Rolify::Adapter::RoleAdapter.new(options[:role_cname])
+ self.adapter = Rolify::Adapter::Base.create("role_adapter", options[:role_cname])
self.role_cname = options[:role_cname]
load_dynamic_methods if Rolify.dynamic_shortcuts
@@ -34,8 +34,7 @@ def resourcify(options = { :role_cname => 'Role' })
resourcify_options.merge!({ :as => :resource })
has_many :roles, resourcify_options
- load "rolify/adapters/#{Rolify.orm}/resource_adapter.rb"
- self.adapter = Rolify::Adapter::ResourceAdapter.new(options[:role_cname])
+ self.adapter = Rolify::Adapter::Base.create("resource_adapter", options[:role_cname])
self.role_cname = options[:role_cname]
end
View
5 lib/rolify/adapters/base.rb
@@ -8,6 +8,11 @@ def initialize(role_cname)
def role_class
@role_cname.constantize
end
+
+ def self.create(adapter, role_cname)
+ load "rolify/adapters/#{Rolify.orm}/#{adapter}.rb"
+ Rolify::Adapter.const_get(adapter.camelize.to_sym).new(role_cname)
+ end
end
class RoleAdapterBase < Adapter::Base

0 comments on commit 8f80f87

Please sign in to comment.