Class: Mobility::Plugins::ActiveModel::Dirty::HandlerMethodsBuilder
- Inherits:
-
Module
- Object
- Module
- Mobility::Plugins::ActiveModel::Dirty::HandlerMethodsBuilder
- Defined in:
- lib/mobility/plugins/active_model/dirty.rb
Overview
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.
Constant Summary collapse
- PATTERNS_WITH_KWARGS =
%w[ %s_changed? %s_previously_changed? will_save_change_to_%s? saved_change_to_%s? ].freeze
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
- #define_handler_methods ⇒ Object
- #each_pattern(attr_name) ⇒ Object
-
#initialize(klass) ⇒ HandlerMethodsBuilder
constructor
A new instance of HandlerMethodsBuilder.
-
#patterns ⇒ Object
Get method suffixes.
Constructor Details
#initialize(klass) ⇒ HandlerMethodsBuilder
Returns a new instance of HandlerMethodsBuilder.
131 132 133 134 |
# File 'lib/mobility/plugins/active_model/dirty.rb', line 131 def initialize(klass) @klass = klass define_handler_methods end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
120 121 122 |
# File 'lib/mobility/plugins/active_model/dirty.rb', line 120 def klass @klass end |
Instance Method Details
#define_handler_methods ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mobility/plugins/active_model/dirty.rb', line 142 def define_handler_methods public_patterns.each do |pattern| method_name = pattern % 'attribute' kwargs = PATTERNS_WITH_KWARGS.include?(pattern) ? ', **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) ⇒ Object
136 137 138 139 140 |
# File 'lib/mobility/plugins/active_model/dirty.rb', line 136 def each_pattern(attr_name) patterns.each do |pattern| yield pattern % attr_name, pattern % 'attribute' end end |
#patterns ⇒ Object
Get method suffixes. Creating an object just to get the list of suffixes is simplest given they change from Rails version to version.
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/mobility/plugins/active_model/dirty.rb', line 162 def patterns @patterns ||= begin # Method name changes in Rails 7.1 attribute_method_patterns = klass.respond_to?(:attribute_method_patterns) ? klass.attribute_method_patterns : klass.attribute_method_matchers attribute_method_patterns.map { |p| "#{p.prefix}%s#{p.suffix}" } - excluded_patterns end end |