2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2017-10-21 09:11:29 -04:00
|
|
|
require "active_support/concern"
|
|
|
|
require "active_support/ordered_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
|
2020-08-22 14:25:30 -04:00
|
|
|
# configuration options as an <tt>OrderedOptions</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
|
|
|
|
#
|
2019-03-11 18:53:17 -04:00
|
|
|
# To omit the instance writer method, pass <tt>instance_writer: false</tt>.
|
|
|
|
# To omit the instance reader method, pass <tt>instance_reader: false</tt>.
|
2012-06-05 11:50:48 -04:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
2019-03-11 18:53:17 -04:00
|
|
|
# Or pass <tt>instance_accessor: false</tt>, to omit both instance methods.
|
2012-06-05 11:50:48 -04:00
|
|
|
#
|
|
|
|
# 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]
|
2018-07-29 13:11:54 -04:00
|
|
|
def config_accessor(*names, instance_reader: true, instance_writer: true, instance_accessor: true) # :doc:
|
2010-04-22 06:00:13 -04:00
|
|
|
names.each do |name|
|
2016-08-06 11:58:50 -04:00
|
|
|
raise NameError.new("invalid config attribute name") unless /\A[_A-Za-z]\w*\z/.match?(name)
|
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
|
|
|
|
2018-07-29 13:11:54 -04:00
|
|
|
if instance_accessor
|
|
|
|
class_eval reader, __FILE__, reader_line if instance_reader
|
|
|
|
class_eval writer, __FILE__, writer_line if instance_writer
|
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
|
2015-01-20 07:03:27 -05:00
|
|
|
private :config_accessor
|
2009-10-14 14:59:00 -04:00
|
|
|
end
|
|
|
|
|
2020-08-22 14:25:30 -04:00
|
|
|
# Reads and writes attributes from a configuration <tt>OrderedOptions</tt>.
|
2011-03-06 04:26:24 -05:00
|
|
|
#
|
2020-03-29 19:30:52 -04: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
|