Permalink
Browse files

configuration changes

  • Loading branch information...
1 parent 3efd02a commit a96becd069d3fa5b968b1e3db1b2726df3843d9e @rsl committed Dec 21, 2012
@@ -3,6 +3,10 @@
module Stringex
module ActsAsUrl # :nodoc:
+ def self.configure(&block)
+ Stringex::Configuration::ActsAsUrl.configure &block
+ end
+
module ActsAsUrlClassMethods # :doc:
# Creates a callback to automatically create an url-friendly representation
# of the <tt>attribute</tt> argument. Example:
@@ -1,34 +1,4 @@
-module Stringex
- module Configuration
- class Base
- attr_accessor :settings
-
- def initialize(options = {})
- @settings = OpenStruct.new(default_settings.merge(options))
- end
-
- # 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
- 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
- when :mongoid
- Stringex::ActsAsUrl::Adapter::Mongoid.new self
- else
- raise ArgumentError, "#{adapter_name} is not a defined ActsAsUrl adapter. Please feel free to implement your own and submit it back upstream."
- end
- end
-
- def default_settings
- {}
- end
- end
- end
-end
-
+require "stringex/configuration/base"
+require "stringex/configuration/configurator"
require "stringex/configuration/acts_as_url"
require "stringex/configuration/string_extensions"
@@ -9,19 +9,33 @@ def initialize(options = {})
super
end
+ def string_extensions_settings
+ [:allow_slash, :exclude, :limit].inject(Hash.new){|m, x| m[x] = settings.send(x); m}
+ end
+
+ def self.settings
+ @settings
+ end
+
+ private
+
+ def default_settings
+ Stringex::Configuration::StringExtensions.new.default_settings.merge(default_settings)
+ end
+
def default_settings
- Stringex::Configuration::StringExtensions.new.default_settings.merge({
+ self.class.default_settings
+ end
+
+ def self.default_settings
+ @default_settings ||= {
:allow_duplicates => false,
:duplicate_count_separator => "-",
:only_when_blank => false,
:scope_for_url => nil,
:sync_url => false,
:url_attribute => "url",
- })
- end
-
- def string_extensions_settings
- [:allow_slash, :exclude, :limit].inject(Hash.new){|m, x| m[x] = settings.send(x); m}
+ }
end
end
end
@@ -0,0 +1,54 @@
+module Stringex
+ module Configuration
+ class Base
+ attr_accessor :settings
+
+ def initialize(local_options = {})
+ current_settings = default_settings.merge(system_wide_customizations)
+ current_settings.merge! local_options
+
+ @settings = OpenStruct.new(current_settings)
+ end
+
+ # 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
+ 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
+ when :mongoid
+ Stringex::ActsAsUrl::Adapter::Mongoid.new self
+ else
+ raise ArgumentError, "#{adapter_name} is not a defined ActsAsUrl adapter. Please feel free to implement your own and submit it back upstream."
+ end
+ end
+
+ def self.configure(&block)
+ configurator = Stringex::Configuration::Configurator.new(self)
+ yield configurator
+ end
+
+ def self.system_wide_customizations
+ @system_wide_customizations ||= {}
+ end
+
+ private
+
+ def default_settings
+ raise ArgumentError, "You shouldn't have hit default_settings on Stringex::Configuration::Base. Check your code."
+ end
+
+ def system_wide_customizations
+ self.class.system_wide_customizations
+ end
+
+ def self.valid_configuration_details
+ default_settings.keys
+ end
+ end
+ end
+end
+
@@ -0,0 +1,24 @@
+module Stringex
+ module Configuration
+ class Configurator
+ attr_accessor :klass
+
+ def initialize(klass)
+ @klass = klass
+
+ self.klass.valid_configuration_details.each do |name|
+ define_instance_method name
+ end
+ end
+
+ def define_instance_method(name)
+ (class << self; self; end).instance_eval do
+ define_method("#{name}=") do |value|
+ customizations = klass.send(:system_wide_customizations)
+ customizations[name.intern] = value
+ end
+ end
+ end
+ end
+ end
+end
@@ -2,7 +2,11 @@ module Stringex
module Configuration
class StringExtensions < Base
def default_settings
- {
+ self.class.default_settings
+ end
+
+ def self.default_settings
+ @default_settings ||= {
:allow_slash => false,
:exclude => [],
:limit => nil
@@ -2,6 +2,8 @@
gem 'activerecord'
require 'active_record'
require "stringex"
+# Reload adapters to make sure ActsAsUrl sees the ORM
+Stringex::ActsAsUrl::Adapter.load_available
puts "-------------------------------------------------"
puts "Running ActsAsUrl tests with ActiveRecord adapter"
@@ -2,6 +2,8 @@
gem 'mongoid'
require 'mongoid'
require 'stringex'
+# Reload adapters to make sure ActsAsUrl sees the ORM
+Stringex::ActsAsUrl::Adapter.load_available
puts "-------------------------------------------------"
puts "Running ActsAsUrl tests with Mongoid adapter"
@@ -0,0 +1,27 @@
+# encoding: UTF-8
+require 'test_helper'
+require 'stringex'
+
+class ActsAsUrlConfigurationTest < Test::Unit::TestCase
+ def test_can_set_base_settings
+ default_configuration = Stringex::Configuration::ActsAsUrl.new(:url_attribute => "original")
+ assert_equal "original", default_configuration.settings.url_attribute
+
+ Stringex::ActsAsUrl.configure do |c|
+ c.url_attribute = "special"
+ end
+ new_configuration = Stringex::Configuration::ActsAsUrl.new
+ assert_equal "special", new_configuration.settings.url_attribute
+ end
+
+ def test_local_options_overrides_system_wide_configuration
+ Stringex::ActsAsUrl.configure do |c|
+ c.url_attribute = "special"
+ end
+ system_configuration = Stringex::Configuration::ActsAsUrl.new
+ assert_equal "special", system_configuration.settings.url_attribute
+
+ local_configuration = Stringex::Configuration::ActsAsUrl.new(:url_attribute => "local")
+ assert_equal "local", local_configuration.settings.url_attribute
+ end
+end
@@ -4,7 +4,7 @@
adapter = ENV['ADAPTER'] || 'active_record'
require File.join(File.expand_path(File.dirname(__FILE__)), "acts_as_url/adapter/#{adapter}.rb")
-class ActsAsUrlTest < Test::Unit::TestCase
+class ActsAsUrlIntegrationTest < Test::Unit::TestCase
include AdapterSpecificTestBehaviors
def test_should_create_url

0 comments on commit a96becd

Please sign in to comment.