2010-04-22 06:00:13 -04:00
|
|
|
require 'active_support/concern'
|
|
|
|
require 'active_support/ordered_options'
|
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!
|
2011-06-29 12:57:59 -04:00
|
|
|
self.class.compile_methods!(keys)
|
2010-09-27 08:50:39 -04:00
|
|
|
end
|
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# 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)
|
2011-06-29 12:57:59 -04:00
|
|
|
keys.reject { |m| method_defined?(m) }.each do |key|
|
2010-09-27 08:50:39 -04:00
|
|
|
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
|
|
|
|
|
2012-06-05 11:50:48 -04:00
|
|
|
# Allows you to add shortcut so that you don't have to refer to attribute
|
2012-12-05 01:11:54 -05:00
|
|
|
# through config. Also look at the example for config to contrast.
|
2012-09-14 15:25:21 -04:00
|
|
|
#
|
2012-06-05 11:50:48 -04:00
|
|
|
# Defines both class and instance config accessors.
|
2010-11-30 09:44:12 -05:00
|
|
|
#
|
|
|
|
# class User
|
|
|
|
# include ActiveSupport::Configurable
|
|
|
|
# config_accessor :allowed_access
|
|
|
|
# end
|
|
|
|
#
|
2012-09-14 15:25:21 -04:00
|
|
|
# User.allowed_access # => nil
|
2012-06-05 11:50:48 -04:00
|
|
|
# User.allowed_access = false
|
2012-09-14 15:25:21 -04:00
|
|
|
# User.allowed_access # => false
|
|
|
|
#
|
2010-11-30 09:44:12 -05:00
|
|
|
# user = User.new
|
2012-06-05 11:50:48 -04:00
|
|
|
# user.allowed_access # => false
|
2010-11-30 09:44:12 -05:00
|
|
|
# user.allowed_access = true
|
|
|
|
# user.allowed_access # => true
|
|
|
|
#
|
2012-09-14 15:25:21 -04:00
|
|
|
# User.allowed_access # => false
|
2012-06-05 11:50:48 -04:00
|
|
|
#
|
|
|
|
# The attribute name must be a valid method name in Ruby.
|
|
|
|
#
|
|
|
|
# class User
|
|
|
|
# include ActiveSupport::Configurable
|
|
|
|
# config_accessor :"1_Badname"
|
|
|
|
# end
|
|
|
|
# # => NameError: invalid config attribute name
|
|
|
|
#
|
|
|
|
# To opt out of the instance writer method, pass <tt>instance_writer: false</tt>.
|
|
|
|
# To opt out of the instance reader method, pass <tt>instance_reader: false</tt>.
|
|
|
|
#
|
|
|
|
# class User
|
|
|
|
# include ActiveSupport::Configurable
|
|
|
|
# config_accessor :allowed_access, instance_reader: false, instance_writer: false
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# User.allowed_access = false
|
2012-12-05 01:11:54 -05:00
|
|
|
# User.allowed_access # => false
|
2012-06-05 11:50:48 -04:00
|
|
|
#
|
|
|
|
# User.new.allowed_access = true # => NoMethodError
|
|
|
|
# User.new.allowed_access # => NoMethodError
|
|
|
|
#
|
|
|
|
# Or pass <tt>instance_accessor: false</tt>, to opt out both instance methods.
|
|
|
|
#
|
|
|
|
# class User
|
|
|
|
# include ActiveSupport::Configurable
|
|
|
|
# config_accessor :allowed_access, instance_accessor: false
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# User.allowed_access = false
|
2012-12-05 01:11:54 -05:00
|
|
|
# User.allowed_access # => false
|
2012-06-05 11:50:48 -04:00
|
|
|
#
|
|
|
|
# User.new.allowed_access = true # => NoMethodError
|
2012-09-14 15:25:21 -04:00
|
|
|
# User.new.allowed_access # => NoMethodError
|
|
|
|
#
|
|
|
|
# Also you can pass a block to set up the attribute with a default value.
|
|
|
|
#
|
|
|
|
# class User
|
|
|
|
# include ActiveSupport::Configurable
|
|
|
|
# config_accessor :hair_colors do
|
|
|
|
# [:brown, :black, :blonde, :red]
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# User.hair_colors # => [:brown, :black, :blonde, :red]
|
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|
|
2013-12-19 14:27:52 -05:00
|
|
|
raise NameError.new('invalid config attribute name') unless name =~ /\A[_A-Za-z]\w*\z/
|
2012-06-05 11:50:48 -04:00
|
|
|
|
2012-06-24 15:39:49 -04:00
|
|
|
reader, reader_line = "def #{name}; config.#{name}; end", __LINE__
|
|
|
|
writer, writer_line = "def #{name}=(value); config.#{name} = value; end", __LINE__
|
2010-04-22 06:00:13 -04:00
|
|
|
|
2012-06-24 15:39:49 -04:00
|
|
|
singleton_class.class_eval reader, __FILE__, reader_line
|
|
|
|
singleton_class.class_eval writer, __FILE__, writer_line
|
2012-06-05 11:50:48 -04:00
|
|
|
|
|
|
|
unless options[:instance_accessor] == false
|
2012-06-24 15:39:49 -04:00
|
|
|
class_eval reader, __FILE__, reader_line unless options[:instance_reader] == false
|
|
|
|
class_eval writer, __FILE__, writer_line unless options[:instance_writer] == false
|
2012-06-05 11:50:48 -04:00
|
|
|
end
|
2012-09-14 15:25:21 -04:00
|
|
|
send("#{name}=", yield) if block_given?
|
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
|
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
|
|
|
|
|