2010-04-22 06:00:13 -04:00
|
|
|
require 'active_support/concern'
|
|
|
|
require 'active_support/ordered_options'
|
|
|
|
require 'active_support/core_ext/kernel/singleton_class'
|
|
|
|
require 'active_support/core_ext/module/delegation'
|
2011-05-06 08:32:48 -04:00
|
|
|
require 'active_support/core_ext/array/extract_options'
|
2009-10-14 14:59:00 -04:00
|
|
|
|
|
|
|
module ActiveSupport
|
2010-08-14 11:01:11 -04:00
|
|
|
# Configurable provides a <tt>config</tt> method to store and retrieve
|
|
|
|
# configuration options as an <tt>OrderedHash</tt>.
|
2009-10-14 14:59:00 -04:00
|
|
|
module Configurable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2010-09-27 08:50:39 -04:00
|
|
|
class Configuration < ActiveSupport::InheritableOptions
|
2010-09-27 14:42:02 -04:00
|
|
|
def compile_methods!
|
|
|
|
self.class.compile_methods!(keys.reject {|key| respond_to?(key)})
|
2010-09-27 08:50:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# compiles reader methods so we don't have to go through method_missing
|
2010-09-27 14:42:02 -04:00
|
|
|
def self.compile_methods!(keys)
|
2010-09-27 08:50:39 -04:00
|
|
|
keys.each do |key|
|
|
|
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
2010-09-27 08:48:06 -04:00
|
|
|
def #{key}; _get(#{key.inspect}); end
|
2010-09-27 08:50:39 -04:00
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-14 14:59:00 -04:00
|
|
|
module ClassMethods
|
|
|
|
def config
|
2011-02-28 08:31:29 -05:00
|
|
|
@_config ||= if respond_to?(:superclass) && superclass.respond_to?(:config)
|
2010-09-27 08:50:39 -04:00
|
|
|
superclass.config.inheritable_copy
|
|
|
|
else
|
|
|
|
# create a new "anonymous" class that will host the compiled reader methods
|
2010-09-27 08:51:31 -04:00
|
|
|
Class.new(Configuration).new
|
2010-09-27 08:50:39 -04:00
|
|
|
end
|
2009-10-14 14:59:00 -04:00
|
|
|
end
|
|
|
|
|
2010-04-22 06:00:13 -04:00
|
|
|
def configure
|
|
|
|
yield config
|
|
|
|
end
|
|
|
|
|
2010-11-30 09:44:12 -05:00
|
|
|
# Allows you to add shortcut so that you don't have to refer to attribute through config.
|
|
|
|
# Also look at the example for config to contrast.
|
|
|
|
#
|
|
|
|
# class User
|
|
|
|
# include ActiveSupport::Configurable
|
|
|
|
# config_accessor :allowed_access
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# user = User.new
|
|
|
|
# user.allowed_access = true
|
|
|
|
# user.allowed_access # => true
|
|
|
|
#
|
2010-04-22 06:00:13 -04:00
|
|
|
def config_accessor(*names)
|
2011-05-06 08:32:48 -04:00
|
|
|
options = names.extract_options!
|
|
|
|
|
2010-04-22 06:00:13 -04:00
|
|
|
names.each do |name|
|
2011-05-06 08:32:48 -04:00
|
|
|
reader, line = "def #{name}; config.#{name}; end", __LINE__
|
|
|
|
writer, line = "def #{name}=(value); config.#{name} = value; end", __LINE__
|
2010-04-22 06:00:13 -04:00
|
|
|
|
2011-05-06 08:32:48 -04:00
|
|
|
singleton_class.class_eval reader, __FILE__, line
|
|
|
|
singleton_class.class_eval writer, __FILE__, line
|
|
|
|
class_eval reader, __FILE__, line unless options[:instance_reader] == false
|
|
|
|
class_eval writer, __FILE__, line unless options[:instance_writer] == false
|
2009-10-14 14:59:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-14 11:01:11 -04:00
|
|
|
# Reads and writes attributes from a configuration <tt>OrderedHash</tt>.
|
2011-03-06 04:26:24 -05:00
|
|
|
#
|
2011-03-06 11:49:39 -05:00
|
|
|
# require 'active_support/configurable'
|
2011-03-06 04:26:24 -05:00
|
|
|
#
|
2010-08-14 11:01:11 -04:00
|
|
|
# class User
|
|
|
|
# include ActiveSupport::Configurable
|
2011-03-06 11:49:39 -05:00
|
|
|
# end
|
2010-08-14 11:01:11 -04:00
|
|
|
#
|
|
|
|
# user = User.new
|
2011-03-06 11:49:39 -05:00
|
|
|
#
|
2010-08-14 11:01:11 -04:00
|
|
|
# user.config.allowed_access = true
|
|
|
|
# user.config.level = 1
|
|
|
|
#
|
|
|
|
# user.config.allowed_access # => true
|
|
|
|
# user.config.level # => 1
|
2011-03-06 11:49:39 -05:00
|
|
|
#
|
2009-10-14 14:59:00 -04:00
|
|
|
def config
|
2010-09-27 08:50:39 -04:00
|
|
|
@_config ||= self.class.config.inheritable_copy
|
2009-10-14 14:59:00 -04:00
|
|
|
end
|
|
|
|
end
|
2010-08-14 11:01:11 -04:00
|
|
|
end
|
|
|
|
|