class Mobility::Plugins::ActiveModel::Dirty::HandlerMethodsBuilder
Module builder which mimics dirty method handlers on a given dirty class. Used to mimic ActiveModel::Dirty
and ActiveRecord::Dirty
, which have similar but slightly different sets of handler methods. Doing it this way with introspection allows us to support basically all AR/AM versions without changes here.
Attributes
klass[R]
Public Class Methods
new(klass)
click to toggle source
@param [Class] klass Dirty
class to mimic
# File lib/mobility/plugins/active_model/dirty.rb, line 123 def initialize(klass) @klass = klass define_handler_methods end
Public Instance Methods
define_handler_methods()
click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 134 def define_handler_methods public_patterns.each do |pattern| method_name = pattern % 'attribute' kwargs = pattern == '%s_changed?' ? ', **kwargs' : '' module_eval <<-EOM, __FILE__, __LINE__ + 1 def #{method_name}(attr_name, *rest#{kwargs}) if (mutations_from_mobility.attribute_changed?(attr_name) || mutations_from_mobility.attribute_previously_changed?(attr_name)) mutations_from_mobility.send(#{method_name.inspect}, attr_name, *rest#{kwargs}) else super end end EOM end end
each_pattern(attr_name) { |pattern % attr_name, pattern % 'attribute'| ... }
click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 128 def each_pattern(attr_name) patterns.each do |pattern| yield pattern % attr_name, pattern % 'attribute' end end
patterns()
click to toggle source
Get method suffixes. Creating an object just to get the list of suffixes is simplest given they change from Rails version to version.
# File lib/mobility/plugins/active_model/dirty.rb, line 154 def patterns @patterns ||= (klass.attribute_method_matchers.map { |p| "#{p.prefix}%s#{p.suffix}" } - excluded_patterns) end
Private Instance Methods
excluded_patterns()
click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 167 def excluded_patterns ['%s', 'restore_%s!'] end
public_patterns()
click to toggle source
# File lib/mobility/plugins/active_model/dirty.rb, line 161 def public_patterns @public_patterns ||= patterns.select do |p| klass.public_method_defined?(p % 'attribute') end end