2017-07-16 13:11:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:38:02 -04:00
|
|
|
require "concurrent/map"
|
2009-10-07 10:05:54 -04:00
|
|
|
|
2009-08-05 00:36:05 -04:00
|
|
|
module ActiveModel
|
2012-06-21 17:56:40 -04:00
|
|
|
# Raised when an attribute is not defined.
|
2012-07-28 02:19:38 -04:00
|
|
|
#
|
|
|
|
# class User < ActiveRecord::Base
|
|
|
|
# has_many :pets
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# user = User.first
|
|
|
|
# user.pets.select(:id).first.user_id
|
2012-11-25 06:33:47 -05:00
|
|
|
# # => ActiveModel::MissingAttributeError: missing attribute: user_id
|
2009-08-05 00:36:05 -04:00
|
|
|
class MissingAttributeError < NoMethodError
|
|
|
|
end
|
2013-04-28 05:38:02 -04:00
|
|
|
|
2013-12-21 18:40:50 -05:00
|
|
|
# == Active \Model \Attribute \Methods
|
2010-06-14 05:06:05 -04:00
|
|
|
#
|
2013-12-21 18:40:50 -05:00
|
|
|
# Provides a way to add prefixes and suffixes to your methods as
|
|
|
|
# well as handling the creation of <tt>ActiveRecord::Base</tt>-like
|
|
|
|
# class methods such as +table_name+.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2013-04-28 05:38:02 -04:00
|
|
|
# The requirements to implement <tt>ActiveModel::AttributeMethods</tt> are to:
|
2010-01-16 20:42:53 -05:00
|
|
|
#
|
2013-04-28 05:38:02 -04:00
|
|
|
# * <tt>include ActiveModel::AttributeMethods</tt> in your class.
|
2015-04-24 08:32:58 -04:00
|
|
|
# * Call each of its methods you want to add, such as +attribute_method_suffix+
|
2013-04-28 05:38:02 -04:00
|
|
|
# or +attribute_method_prefix+.
|
2012-06-21 13:26:14 -04:00
|
|
|
# * Call +define_attribute_methods+ after the other methods are called.
|
|
|
|
# * Define the various generic +_attribute+ methods that you have declared.
|
2013-12-21 18:40:50 -05:00
|
|
|
# * Define an +attributes+ method which returns a hash with each
|
|
|
|
# attribute name in your model as hash key and the attribute value as hash value.
|
|
|
|
# Hash keys must be strings.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# A minimal implementation could be:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# class Person
|
|
|
|
# include ActiveModel::AttributeMethods
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2012-06-21 13:26:14 -04:00
|
|
|
# attribute_method_affix prefix: 'reset_', suffix: '_to_default!'
|
2010-01-16 20:42:53 -05:00
|
|
|
# attribute_method_suffix '_contrived?'
|
|
|
|
# attribute_method_prefix 'clear_'
|
2012-06-21 13:26:14 -04:00
|
|
|
# define_attribute_methods :name
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# attr_accessor :name
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2013-04-28 05:38:02 -04:00
|
|
|
# def attributes
|
2013-12-21 18:40:50 -05:00
|
|
|
# { 'name' => @name }
|
2013-04-28 05:38:02 -04:00
|
|
|
# end
|
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# private
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# def attribute_contrived?(attr)
|
|
|
|
# true
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# def clear_attribute(attr)
|
|
|
|
# send("#{attr}=", nil)
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# def reset_attribute_to_default!(attr)
|
2012-06-21 13:26:14 -04:00
|
|
|
# send("#{attr}=", 'Default Name')
|
2010-01-16 20:42:53 -05:00
|
|
|
# end
|
|
|
|
# end
|
2009-08-05 00:36:05 -04:00
|
|
|
module AttributeMethods
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2011-11-14 14:12:17 -05:00
|
|
|
NAME_COMPILABLE_REGEXP = /\A[a-zA-Z_]\w*[!?=]?\z/
|
|
|
|
CALL_COMPILABLE_REGEXP = /\A[a-zA-Z_]\w*[!?]?\z/
|
2011-04-19 11:56:46 -04:00
|
|
|
|
2010-08-31 02:56:23 -04:00
|
|
|
included do
|
2017-05-29 12:01:50 -04:00
|
|
|
class_attribute :attribute_aliases, instance_writer: false, default: {}
|
|
|
|
class_attribute :attribute_method_matchers, instance_writer: false, default: [ ClassMethods::AttributeMethodMatcher.new ]
|
2010-08-31 02:56:23 -04:00
|
|
|
end
|
|
|
|
|
2009-08-05 00:36:05 -04:00
|
|
|
module ClassMethods
|
|
|
|
# Declares a method available for all attributes with the given prefix.
|
|
|
|
# Uses +method_missing+ and <tt>respond_to?</tt> to rewrite the method.
|
|
|
|
#
|
|
|
|
# #{prefix}#{attr}(*args, &block)
|
|
|
|
#
|
|
|
|
# to
|
|
|
|
#
|
|
|
|
# #{prefix}attribute(#{attr}, *args, &block)
|
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# An instance method <tt>#{prefix}attribute</tt> must exist and accept
|
2010-06-14 05:06:05 -04:00
|
|
|
# at least the +attr+ argument.
|
2009-08-05 00:36:05 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# class Person
|
|
|
|
# include ActiveModel::AttributeMethods
|
2012-06-21 13:51:39 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# attr_accessor :name
|
2009-08-05 00:36:05 -04:00
|
|
|
# attribute_method_prefix 'clear_'
|
2012-05-14 12:34:35 -04:00
|
|
|
# define_attribute_methods :name
|
2009-08-05 00:36:05 -04:00
|
|
|
#
|
|
|
|
# private
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# def clear_attribute(attr)
|
|
|
|
# send("#{attr}=", nil)
|
|
|
|
# end
|
2009-08-05 00:36:05 -04:00
|
|
|
# end
|
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# person = Person.new
|
2012-06-21 13:26:14 -04:00
|
|
|
# person.name = 'Bob'
|
2010-01-16 20:42:53 -05:00
|
|
|
# person.name # => "Bob"
|
2009-08-05 00:36:05 -04:00
|
|
|
# person.clear_name
|
2010-01-16 20:42:53 -05:00
|
|
|
# person.name # => nil
|
2009-08-05 00:36:05 -04:00
|
|
|
def attribute_method_prefix(*prefixes)
|
2012-06-25 13:45:39 -04:00
|
|
|
self.attribute_method_matchers += prefixes.map! { |prefix| AttributeMethodMatcher.new prefix: prefix }
|
2012-05-20 14:11:20 -04:00
|
|
|
undefine_attribute_methods
|
2009-08-05 00:36:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Declares a method available for all attributes with the given suffix.
|
|
|
|
# Uses +method_missing+ and <tt>respond_to?</tt> to rewrite the method.
|
|
|
|
#
|
|
|
|
# #{attr}#{suffix}(*args, &block)
|
|
|
|
#
|
|
|
|
# to
|
|
|
|
#
|
|
|
|
# attribute#{suffix}(#{attr}, *args, &block)
|
|
|
|
#
|
2012-06-21 13:26:14 -04:00
|
|
|
# An <tt>attribute#{suffix}</tt> instance method must exist and accept at
|
|
|
|
# least the +attr+ argument.
|
2009-08-05 00:36:05 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# class Person
|
|
|
|
# include ActiveModel::AttributeMethods
|
2012-06-21 13:51:39 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# attr_accessor :name
|
2009-08-05 00:36:05 -04:00
|
|
|
# attribute_method_suffix '_short?'
|
2012-05-14 12:34:35 -04:00
|
|
|
# define_attribute_methods :name
|
2009-08-05 00:36:05 -04:00
|
|
|
#
|
|
|
|
# private
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# def attribute_short?(attr)
|
|
|
|
# send(attr).length < 5
|
|
|
|
# end
|
2009-08-05 00:36:05 -04:00
|
|
|
# end
|
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# person = Person.new
|
2012-06-21 13:26:14 -04:00
|
|
|
# person.name = 'Bob'
|
2010-01-16 20:42:53 -05:00
|
|
|
# person.name # => "Bob"
|
|
|
|
# person.name_short? # => true
|
2009-08-05 00:36:05 -04:00
|
|
|
def attribute_method_suffix(*suffixes)
|
2012-06-25 13:45:39 -04:00
|
|
|
self.attribute_method_matchers += suffixes.map! { |suffix| AttributeMethodMatcher.new suffix: suffix }
|
2012-05-20 14:11:20 -04:00
|
|
|
undefine_attribute_methods
|
2009-08-05 00:36:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Declares a method available for all attributes with the given prefix
|
|
|
|
# and suffix. Uses +method_missing+ and <tt>respond_to?</tt> to rewrite
|
|
|
|
# the method.
|
|
|
|
#
|
|
|
|
# #{prefix}#{attr}#{suffix}(*args, &block)
|
|
|
|
#
|
|
|
|
# to
|
|
|
|
#
|
|
|
|
# #{prefix}attribute#{suffix}(#{attr}, *args, &block)
|
|
|
|
#
|
|
|
|
# An <tt>#{prefix}attribute#{suffix}</tt> instance method must exist and
|
|
|
|
# accept at least the +attr+ argument.
|
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# class Person
|
|
|
|
# include ActiveModel::AttributeMethods
|
2012-06-21 13:51:39 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# attr_accessor :name
|
2012-06-21 13:26:14 -04:00
|
|
|
# attribute_method_affix prefix: 'reset_', suffix: '_to_default!'
|
2012-05-14 12:34:35 -04:00
|
|
|
# define_attribute_methods :name
|
2009-08-05 00:36:05 -04:00
|
|
|
#
|
|
|
|
# private
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# def reset_attribute_to_default!(attr)
|
2013-12-21 18:40:50 -05:00
|
|
|
# send("#{attr}=", 'Default Name')
|
2010-01-16 20:42:53 -05:00
|
|
|
# end
|
2009-08-05 00:36:05 -04:00
|
|
|
# end
|
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# person = Person.new
|
2009-08-05 00:36:05 -04:00
|
|
|
# person.name # => 'Gem'
|
|
|
|
# person.reset_name_to_default!
|
2013-12-21 18:40:50 -05:00
|
|
|
# person.name # => 'Default Name'
|
2009-08-05 00:36:05 -04:00
|
|
|
def attribute_method_affix(*affixes)
|
2012-06-25 13:45:39 -04:00
|
|
|
self.attribute_method_matchers += affixes.map! { |affix| AttributeMethodMatcher.new prefix: affix[:prefix], suffix: affix[:suffix] }
|
2012-05-20 14:11:20 -04:00
|
|
|
undefine_attribute_methods
|
2009-08-05 00:36:05 -04:00
|
|
|
end
|
|
|
|
|
2012-05-07 23:47:35 -04:00
|
|
|
# Allows you to make aliases for attributes.
|
|
|
|
#
|
|
|
|
# class Person
|
2012-06-21 13:51:39 -04:00
|
|
|
# include ActiveModel::AttributeMethods
|
|
|
|
#
|
2012-05-07 23:47:35 -04:00
|
|
|
# attr_accessor :name
|
2012-06-21 13:51:39 -04:00
|
|
|
# attribute_method_suffix '_short?'
|
|
|
|
# define_attribute_methods :name
|
|
|
|
#
|
2012-05-07 23:47:35 -04:00
|
|
|
# alias_attribute :nickname, :name
|
2012-06-21 13:51:39 -04:00
|
|
|
#
|
|
|
|
# private
|
|
|
|
#
|
2012-06-22 12:44:29 -04:00
|
|
|
# def attribute_short?(attr)
|
2012-06-21 13:51:39 -04:00
|
|
|
# send(attr).length < 5
|
|
|
|
# end
|
2012-05-07 23:47:35 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new
|
2012-06-21 13:51:39 -04:00
|
|
|
# person.name = 'Bob'
|
|
|
|
# person.name # => "Bob"
|
|
|
|
# person.nickname # => "Bob"
|
|
|
|
# person.name_short? # => true
|
2012-11-25 06:33:47 -05:00
|
|
|
# person.nickname_short? # => true
|
2009-08-10 12:58:44 -04:00
|
|
|
def alias_attribute(new_name, old_name)
|
2012-06-22 10:44:01 -04:00
|
|
|
self.attribute_aliases = attribute_aliases.merge(new_name.to_s => old_name.to_s)
|
2009-08-10 12:58:44 -04:00
|
|
|
attribute_method_matchers.each do |matcher|
|
2011-04-19 11:56:46 -04:00
|
|
|
matcher_new = matcher.method_name(new_name).to_s
|
|
|
|
matcher_old = matcher.method_name(old_name).to_s
|
2012-01-31 05:43:54 -05:00
|
|
|
define_proxy_call false, self, matcher_new, matcher_old
|
2009-08-10 12:58:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-01 19:23:08 -04:00
|
|
|
# Is +new_name+ an alias?
|
|
|
|
def attribute_alias?(new_name)
|
|
|
|
attribute_aliases.key? new_name.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the original name for the alias +name+
|
|
|
|
def attribute_alias(name)
|
|
|
|
attribute_aliases[name.to_s]
|
|
|
|
end
|
|
|
|
|
2011-01-27 10:27:35 -05:00
|
|
|
# Declares the attributes that should be prefixed and suffixed by
|
2015-05-29 16:27:23 -04:00
|
|
|
# <tt>ActiveModel::AttributeMethods</tt>.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2015-04-24 08:40:41 -04:00
|
|
|
# To use, pass attribute names (as strings or symbols). Be sure to declare
|
2012-06-21 13:26:14 -04:00
|
|
|
# +define_attribute_methods+ after you define any prefix, suffix or affix
|
|
|
|
# methods, or they will not hook in.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# class Person
|
|
|
|
# include ActiveModel::AttributeMethods
|
2012-06-21 13:51:39 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# attr_accessor :name, :age, :address
|
|
|
|
# attribute_method_prefix 'clear_'
|
|
|
|
#
|
|
|
|
# # Call to define_attribute_methods must appear after the
|
|
|
|
# # attribute_method_prefix, attribute_method_suffix or
|
2015-04-24 09:02:38 -04:00
|
|
|
# # attribute_method_affix declarations.
|
2012-05-14 12:34:35 -04:00
|
|
|
# define_attribute_methods :name, :age, :address
|
2010-01-16 20:42:53 -05:00
|
|
|
#
|
|
|
|
# private
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 20:42:53 -05:00
|
|
|
# def clear_attribute(attr)
|
2014-01-06 05:45:21 -05:00
|
|
|
# send("#{attr}=", nil)
|
2010-01-16 20:42:53 -05:00
|
|
|
# end
|
|
|
|
# end
|
2012-05-14 12:34:35 -04:00
|
|
|
def define_attribute_methods(*attr_names)
|
|
|
|
attr_names.flatten.each { |attr_name| define_attribute_method(attr_name) }
|
2011-02-13 12:27:33 -05:00
|
|
|
end
|
|
|
|
|
2012-06-21 16:51:37 -04:00
|
|
|
# Declares an attribute that should be prefixed and suffixed by
|
2015-05-29 16:27:23 -04:00
|
|
|
# <tt>ActiveModel::AttributeMethods</tt>.
|
2012-06-21 16:51:37 -04:00
|
|
|
#
|
2015-04-24 08:40:41 -04:00
|
|
|
# To use, pass an attribute name (as string or symbol). Be sure to declare
|
2012-06-21 16:51:37 -04:00
|
|
|
# +define_attribute_method+ after you define any prefix, suffix or affix
|
|
|
|
# method, or they will not hook in.
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::AttributeMethods
|
|
|
|
#
|
|
|
|
# attr_accessor :name
|
|
|
|
# attribute_method_suffix '_short?'
|
2012-06-21 17:19:40 -04:00
|
|
|
#
|
|
|
|
# # Call to define_attribute_method must appear after the
|
|
|
|
# # attribute_method_prefix, attribute_method_suffix or
|
2015-04-24 09:02:38 -04:00
|
|
|
# # attribute_method_affix declarations.
|
2012-06-21 16:51:37 -04:00
|
|
|
# define_attribute_method :name
|
|
|
|
#
|
|
|
|
# private
|
|
|
|
#
|
|
|
|
# def attribute_short?(attr)
|
|
|
|
# send(attr).length < 5
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new
|
|
|
|
# person.name = 'Bob'
|
|
|
|
# person.name # => "Bob"
|
|
|
|
# person.name_short? # => true
|
2011-02-13 12:27:33 -05:00
|
|
|
def define_attribute_method(attr_name)
|
|
|
|
attribute_method_matchers.each do |matcher|
|
2011-09-09 04:02:40 -04:00
|
|
|
method_name = matcher.method_name(attr_name)
|
|
|
|
|
|
|
|
unless instance_method_already_implemented?(method_name)
|
2019-04-12 21:41:57 -04:00
|
|
|
generate_method = "define_method_#{matcher.target}"
|
2009-08-05 00:36:05 -04:00
|
|
|
|
2012-02-21 13:29:44 -05:00
|
|
|
if respond_to?(generate_method, true)
|
2017-02-21 05:59:56 -05:00
|
|
|
send(generate_method, attr_name.to_s)
|
2011-02-13 12:27:33 -05:00
|
|
|
else
|
2019-04-12 21:41:57 -04:00
|
|
|
define_proxy_call true, generated_attribute_methods, method_name, matcher.target, attr_name.to_s
|
2009-08-05 00:36:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-07-16 02:40:11 -04:00
|
|
|
attribute_method_matchers_cache.clear
|
2009-08-05 00:36:05 -04:00
|
|
|
end
|
|
|
|
|
2012-06-21 17:56:40 -04:00
|
|
|
# Removes all the previously dynamically defined methods from the class.
|
2012-06-21 17:19:40 -04:00
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::AttributeMethods
|
|
|
|
#
|
|
|
|
# attr_accessor :name
|
|
|
|
# attribute_method_suffix '_short?'
|
|
|
|
# define_attribute_method :name
|
|
|
|
#
|
|
|
|
# private
|
|
|
|
#
|
|
|
|
# def attribute_short?(attr)
|
|
|
|
# send(attr).length < 5
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new
|
|
|
|
# person.name = 'Bob'
|
|
|
|
# person.name_short? # => true
|
|
|
|
#
|
|
|
|
# Person.undefine_attribute_methods
|
|
|
|
#
|
|
|
|
# person.name_short? # => NoMethodError
|
2009-08-05 00:36:05 -04:00
|
|
|
def undefine_attribute_methods
|
|
|
|
generated_attribute_methods.module_eval do
|
|
|
|
instance_methods.each { |m| undef_method(m) }
|
|
|
|
end
|
2011-07-16 02:40:11 -04:00
|
|
|
attribute_method_matchers_cache.clear
|
2009-08-05 00:36:05 -04:00
|
|
|
end
|
|
|
|
|
2016-12-19 04:41:09 -05:00
|
|
|
private
|
2017-07-14 00:26:45 -04:00
|
|
|
def generated_attribute_methods
|
|
|
|
@generated_attribute_methods ||= Module.new.tap { |mod| include mod }
|
|
|
|
end
|
|
|
|
|
2016-12-19 04:41:09 -05:00
|
|
|
def instance_method_already_implemented?(method_name)
|
2011-09-13 18:46:43 -04:00
|
|
|
generated_attribute_methods.method_defined?(method_name)
|
2009-08-05 00:36:05 -04:00
|
|
|
end
|
|
|
|
|
2011-07-17 00:51:34 -04:00
|
|
|
# The methods +method_missing+ and +respond_to?+ of this module are
|
|
|
|
# invoked often in a typical rails, both of which invoke the method
|
2015-08-11 18:23:12 -04:00
|
|
|
# +matched_attribute_method+. The latter method iterates through an
|
2011-07-17 00:51:34 -04:00
|
|
|
# array doing regular expression matches, which results in a lot of
|
2013-12-02 09:05:02 -05:00
|
|
|
# object creations. Most of the time it returns a +nil+ match. As the
|
2011-07-17 00:51:34 -04:00
|
|
|
# match result is always the same given a +method_name+, this cache is
|
|
|
|
# used to alleviate the GC, which ultimately also speeds up the app
|
|
|
|
# significantly (in our case our test suite finishes 10% faster with
|
|
|
|
# this cache).
|
2016-12-19 04:37:57 -05:00
|
|
|
def attribute_method_matchers_cache
|
2015-09-19 09:56:26 -04:00
|
|
|
@attribute_method_matchers_cache ||= Concurrent::Map.new(initial_capacity: 4)
|
2011-07-17 00:51:34 -04:00
|
|
|
end
|
|
|
|
|
2016-12-19 04:37:57 -05:00
|
|
|
def attribute_method_matchers_matching(method_name)
|
2012-12-13 08:47:33 -05:00
|
|
|
attribute_method_matchers_cache.compute_if_absent(method_name) do
|
2011-09-04 17:07:07 -04:00
|
|
|
# Must try to match prefixes/suffixes first, or else the matcher with no prefix/suffix
|
|
|
|
# will match every time.
|
|
|
|
matchers = attribute_method_matchers.partition(&:plain?).reverse.flatten(1)
|
2019-04-12 02:19:09 -04:00
|
|
|
matchers.map { |matcher| matcher.match(method_name) }.compact
|
2011-07-16 02:40:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-14 14:12:17 -05:00
|
|
|
# Define a method `name` in `mod` that dispatches to `send`
|
2015-04-24 08:43:24 -04:00
|
|
|
# using the given `extra` args. This falls back on `define_method`
|
2011-11-14 14:12:17 -05:00
|
|
|
# and `send` if the given names cannot be compiled.
|
2019-04-12 21:41:57 -04:00
|
|
|
def define_proxy_call(include_private, mod, name, target, *extra)
|
2016-07-24 15:23:23 -04:00
|
|
|
defn = if NAME_COMPILABLE_REGEXP.match?(name)
|
2012-06-25 13:45:39 -04:00
|
|
|
"def #{name}(*args)"
|
2011-11-14 14:12:17 -05:00
|
|
|
else
|
2012-06-25 13:45:39 -04:00
|
|
|
"define_method(:'#{name}') do |*args|"
|
2011-11-14 14:12:17 -05:00
|
|
|
end
|
|
|
|
|
2018-02-27 23:33:37 -05:00
|
|
|
extra = (extra.map!(&:inspect) << "*args").join(", ")
|
2011-11-14 14:12:17 -05:00
|
|
|
|
2019-04-12 21:41:57 -04:00
|
|
|
body = if CALL_COMPILABLE_REGEXP.match?(target)
|
|
|
|
"#{"self." unless include_private}#{target}(#{extra})"
|
2011-11-14 14:12:17 -05:00
|
|
|
else
|
2019-04-12 21:41:57 -04:00
|
|
|
"send(:'#{target}', #{extra})"
|
2011-11-14 14:12:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
|
|
#{defn}
|
2019-04-12 21:41:57 -04:00
|
|
|
#{body}
|
2011-11-14 14:12:17 -05:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
2012-06-21 16:37:09 -04:00
|
|
|
class AttributeMethodMatcher #:nodoc:
|
2019-04-12 21:41:57 -04:00
|
|
|
attr_reader :prefix, :suffix, :target
|
2009-08-05 00:36:05 -04:00
|
|
|
|
2019-04-12 10:23:32 -04:00
|
|
|
AttributeMethodMatch = Struct.new(:target, :attr_name)
|
2009-08-05 00:36:05 -04:00
|
|
|
|
|
|
|
def initialize(options = {})
|
2016-08-06 12:38:02 -04:00
|
|
|
@prefix, @suffix = options.fetch(:prefix, ""), options.fetch(:suffix, "")
|
2012-01-19 20:08:53 -05:00
|
|
|
@regex = /^(?:#{Regexp.escape(@prefix)})(.*)(?:#{Regexp.escape(@suffix)})$/
|
2019-04-12 21:41:57 -04:00
|
|
|
@target = "#{@prefix}attribute#{@suffix}"
|
2010-08-30 17:08:10 -04:00
|
|
|
@method_name = "#{prefix}%s#{suffix}"
|
2009-08-05 00:36:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def match(method_name)
|
2010-08-30 17:08:10 -04:00
|
|
|
if @regex =~ method_name
|
2019-04-12 21:41:57 -04:00
|
|
|
AttributeMethodMatch.new(target, $1)
|
2009-08-05 00:36:05 -04:00
|
|
|
end
|
|
|
|
end
|
2009-08-10 12:53:10 -04:00
|
|
|
|
|
|
|
def method_name(attr_name)
|
2010-08-30 17:08:10 -04:00
|
|
|
@method_name % attr_name
|
2009-08-10 12:53:10 -04:00
|
|
|
end
|
2011-09-04 17:07:07 -04:00
|
|
|
|
|
|
|
def plain?
|
|
|
|
prefix.empty? && suffix.empty?
|
|
|
|
end
|
2009-08-05 00:36:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-28 05:38:02 -04:00
|
|
|
# Allows access to the object attributes, which are held in the hash
|
|
|
|
# returned by <tt>attributes</tt>, as though they were first-class
|
|
|
|
# methods. So a +Person+ class with a +name+ attribute can for example use
|
|
|
|
# <tt>Person#name</tt> and <tt>Person#name=</tt> and never directly use
|
2015-04-24 09:02:38 -04:00
|
|
|
# the attributes hash -- except for multiple assignments with
|
2013-04-28 05:38:02 -04:00
|
|
|
# <tt>ActiveRecord::Base#attributes=</tt>.
|
2009-08-05 00:36:05 -04:00
|
|
|
#
|
2013-04-28 05:38:02 -04:00
|
|
|
# It's also possible to instantiate related objects, so a <tt>Client</tt>
|
|
|
|
# class belonging to the +clients+ table with a +master_id+ foreign key
|
|
|
|
# can instantiate master through <tt>Client#master</tt>.
|
2011-09-12 17:12:12 -04:00
|
|
|
def method_missing(method, *args, &block)
|
|
|
|
if respond_to_without_attributes?(method, true)
|
|
|
|
super
|
|
|
|
else
|
2015-08-11 18:23:12 -04:00
|
|
|
match = matched_attribute_method(method.to_s)
|
2011-09-12 18:58:20 -04:00
|
|
|
match ? attribute_missing(match, *args, &block) : super
|
2009-08-05 00:36:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-28 05:38:02 -04:00
|
|
|
# +attribute_missing+ is like +method_missing+, but for attributes. When
|
|
|
|
# +method_missing+ is called we check to see if there is a matching
|
|
|
|
# attribute method. If so, we tell +attribute_missing+ to dispatch the
|
|
|
|
# attribute. This method can be overloaded to customize the behavior.
|
2011-09-12 18:58:20 -04:00
|
|
|
def attribute_missing(match, *args, &block)
|
|
|
|
__send__(match.target, match.attr_name, *args, &block)
|
|
|
|
end
|
|
|
|
|
2013-04-28 05:38:02 -04:00
|
|
|
# A +Person+ instance with a +name+ attribute can ask
|
|
|
|
# <tt>person.respond_to?(:name)</tt>, <tt>person.respond_to?(:name=)</tt>,
|
|
|
|
# and <tt>person.respond_to?(:name?)</tt> which will all return +true+.
|
2009-08-05 00:36:05 -04:00
|
|
|
alias :respond_to_without_attributes? :respond_to?
|
|
|
|
def respond_to?(method, include_private_methods = false)
|
|
|
|
if super
|
2011-09-12 17:29:45 -04:00
|
|
|
true
|
2009-08-05 00:36:05 -04:00
|
|
|
elsif !include_private_methods && super(method, true)
|
|
|
|
# If we're here then we haven't found among non-private methods
|
2010-06-14 05:06:05 -04:00
|
|
|
# but found among all methods. Which means that the given method is private.
|
2011-09-12 17:29:45 -04:00
|
|
|
false
|
|
|
|
else
|
2015-08-11 18:23:12 -04:00
|
|
|
!matched_attribute_method(method.to_s).nil?
|
2009-08-05 00:36:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-19 04:41:09 -05:00
|
|
|
private
|
|
|
|
def attribute_method?(attr_name)
|
2012-01-15 06:06:50 -05:00
|
|
|
respond_to_without_attributes?(:attributes) && attributes.include?(attr_name)
|
2009-08-05 00:36:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a struct representing the matching attribute method.
|
|
|
|
# The struct's attributes are prefix, base and suffix.
|
2015-08-11 18:23:12 -04:00
|
|
|
def matched_attribute_method(method_name)
|
2014-12-04 17:50:31 -05:00
|
|
|
matches = self.class.send(:attribute_method_matchers_matching, method_name)
|
|
|
|
matches.detect { |match| attribute_method?(match.attr_name) }
|
2009-08-05 00:36:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def missing_attribute(attr_name, stack)
|
|
|
|
raise ActiveModel::MissingAttributeError, "missing attribute: #{attr_name}", stack
|
|
|
|
end
|
2017-04-14 13:46:07 -04:00
|
|
|
|
|
|
|
def _read_attribute(attr)
|
|
|
|
__send__(attr)
|
|
|
|
end
|
2018-06-22 11:54:31 -04:00
|
|
|
|
|
|
|
module AttrNames # :nodoc:
|
|
|
|
DEF_SAFE_NAME = /\A[a-zA-Z_]\w*\z/
|
|
|
|
|
|
|
|
# We want to generate the methods via module_eval rather than
|
|
|
|
# define_method, because define_method is slower on dispatch.
|
|
|
|
# Evaluating many similar methods may use more memory as the instruction
|
|
|
|
# sequences are duplicated and cached (in MRI). define_method may
|
|
|
|
# be slower on dispatch, but if you're careful about the closure
|
|
|
|
# created, then define_method will consume much less memory.
|
|
|
|
#
|
|
|
|
# But sometimes the database might return columns with
|
|
|
|
# characters that are not allowed in normal method names (like
|
|
|
|
# 'my_column(omg)'. So to work around this we first define with
|
|
|
|
# the __temp__ identifier, and then use alias method to rename
|
|
|
|
# it to what we want.
|
|
|
|
#
|
|
|
|
# We are also defining a constant to hold the frozen string of
|
|
|
|
# the attribute name. Using a constant means that we do not have
|
|
|
|
# to allocate an object on each call to the attribute method.
|
|
|
|
# Making it frozen means that it doesn't get duped when used to
|
|
|
|
# key the @attributes in read_attribute.
|
|
|
|
def self.define_attribute_accessor_method(mod, attr_name, writer: false)
|
|
|
|
method_name = "#{attr_name}#{'=' if writer}"
|
|
|
|
if attr_name.ascii_only? && DEF_SAFE_NAME.match?(attr_name)
|
|
|
|
yield method_name, "'#{attr_name}'.freeze"
|
|
|
|
else
|
|
|
|
safe_name = attr_name.unpack1("h*")
|
|
|
|
const_name = "ATTR_#{safe_name}"
|
|
|
|
const_set(const_name, attr_name) unless const_defined?(const_name)
|
|
|
|
temp_method_name = "__temp__#{safe_name}#{'=' if writer}"
|
|
|
|
attr_name_expr = "::ActiveModel::AttributeMethods::AttrNames::#{const_name}"
|
|
|
|
yield temp_method_name, attr_name_expr
|
2018-12-20 11:39:18 -05:00
|
|
|
mod.alias_method method_name, temp_method_name
|
|
|
|
mod.undef_method temp_method_name
|
2018-06-22 11:54:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-08-05 00:36:05 -04:00
|
|
|
end
|
|
|
|
end
|