Permalink
Browse files

refactored to autodetect adapter

  • Loading branch information...
1 parent ae5042e commit 907a40683585ce3a6a49b7636a66f8d2e91a14c0 @rsl committed Dec 6, 2012
View
18 Rakefile
@@ -24,21 +24,23 @@ rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
end
+Rake::TestTask.new do |t|
+ t.libs << 'lib' << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
desc 'Default: Run Stringex test suite using ActiveRecord as the ORM'
-task :default => %w{setup_active_record_suite test:active_record}
+task :default => [:setup_active_record_suite, :test]
task :setup_active_record_suite do
`rm -f #{File.dirname(__FILE__)}/test/acts_as_url.sqlite3`
ENV['ADAPTER'] = 'active_record'
end
-Rake::TestTask.new('test:active_record') do |t|
- t.libs << 'lib' << 'test'
- t.pattern = 'test/**/*_test.rb'
- t.verbose = true
-end
-if task('test:active_record').full_comment
- task('test:active_record').full_comment.replace 'Run Stringex test suite using ActiveRecord as the ORM'
+desc 'Run Stringex test suite using ActiveRecord as the ORM'
+namespace :test do
+ task :active_record => [:setup_active_record_suite, :test]
end
desc 'Generate RDoc for Stringex'
View
2 lib/stringex.rb
@@ -8,4 +8,4 @@
String.send :include, Stringex::StringExtensions::PublicInstanceMethods
String.send :extend, Stringex::StringExtensions::PublicClassMethods
-Stringex::ActsAsUrl.load_adapters
+Stringex::ActsAsUrl::Adapter.load_available
View
26 lib/stringex/acts_as_url.rb
@@ -3,18 +3,12 @@
module Stringex
module ActsAsUrl # :nodoc:
- def self.included(base)
- base.extend ClassMethods
+ def self.mix_into(klass)
+ klass.send :include, ActsAsUrlInstanceMethods
+ klass.send :extend, ActsAsUrlClassMethods
end
- def self.load_adapters
- Adapter.constants.each do |name|
- adapter = Adapter.const_get(name)
- adapter.load if adapter.loadable?
- end
- end
-
- module ClassMethods # :doc:
+ module ActsAsUrlClassMethods # :doc:
# Creates a callback to automatically create an url-friendly representation
# of the <tt>attribute</tt> argument. Example:
#
@@ -28,7 +22,9 @@ module ClassMethods # :doc:
# The default attribute <tt>acts_as_url</tt> uses to save the permalink is <tt>url</tt>
# but this can be changed in the options hash. Available options are:
#
- # <tt>:adapter</tt>:: If specified, ORM adapter to use. Default is :active_record.
+ # <tt>:adapter</tt>:: If specified, will indicate what ORM adapter to use. Default functionality
+ # is to use the first available adapter. This should work for most cases
+ # unless you are using multiple ORMs in a single project.
# <tt>:allow_slash</tt>:: If true, allow the generated url to contain slashes. Default is false[y].
# <tt>:allow_duplicates</tt>:: If true, allow duplicate urls instead of appending numbers to
# differentiate between urls. Default is false[y].
@@ -67,10 +63,10 @@ def initialize_urls
end
end
- private
-
- def ensure_unique_url
- acts_as_url_configuration.adapter.ensure_unique_url! self
+ module ActsAsUrlInstanceMethods
+ def ensure_unique_url
+ acts_as_url_configuration.adapter.ensure_unique_url! self
+ end
end
end
end
View
24 lib/stringex/acts_as_url/adapter.rb
@@ -1,2 +1,24 @@
require "stringex/acts_as_url/adapter/base"
-require "stringex/acts_as_url/adapter/active_record"
+require "stringex/acts_as_url/adapter/active_record"
+
+module Stringex
+ module ActsAsUrl
+ module Adapter
+ def self.add_loaded_adapter(adapter)
+ @loaded_adapters << adapter
+ end
+
+ def self.load_available
+ @loaded_adapters = []
+ constants.each do |name|
+ adapter = const_get(name)
+ adapter.load if adapter.loadable?
+ end
+ end
+
+ def self.first_available
+ @loaded_adapters[0]
+ end
+ end
+ end
+end
View
4 lib/stringex/acts_as_url/adapter/active_record.rb
@@ -29,12 +29,12 @@ def url_attribute(instance)
end
def self.loadable?
- defined? ::ActiveRecord
+ defined?(::ActiveRecord) && defined?(::ActiveRecord::Base)
end
def self.load
ensure_loadable
- ::ActiveRecord::Base.send :include, Stringex::ActsAsUrl
+ Stringex::ActsAsUrl.mix_into ::ActiveRecord::Base
end
private
View
9 lib/stringex/acts_as_url/adapter/base.rb
@@ -25,12 +25,13 @@ def initialize_urls!(klass)
end
end
- def self.loadable?
- false
+ def self.ensure_loadable
+ raise "The #{self} adapter cannot be loaded" unless loadable?
+ Stringex::ActsAsUrl::Adapter.add_loaded_adapter self
end
- def self.ensure_loadable
- raise "#{self} is not an loadable adapter" unless loadable?
+ def self.loadable?
+ false
end
private
View
5 lib/stringex/configuration.rb
@@ -10,7 +10,10 @@ def initialize(options = {})
# NOTE: This does not cache itself so that instance and class can be cached on the adapter
# without worrying about thread safety or race conditions
def adapter
- case settings.adapter
+ adapter_name = settings.adapter || Stringex::ActsAsUrl::Adapter.first_available
+ case adapter_name
+ when Class
+ adapter_name.send :new, self
when :active_record
Stringex::ActsAsUrl::Adapter::ActiveRecord.new self
else
View
1 lib/stringex/configuration/acts_as_url.rb
@@ -11,7 +11,6 @@ def initialize(options = {})
def default_settings
Stringex::Configuration::StringExtensions.new.default_settings.merge({
- :adapter => :active_record,
:allow_duplicates => false,
:duplicate_count_separator => "-",
:only_when_blank => false,

0 comments on commit 907a406

Please sign in to comment.