Class: ActiveModel::Validations::AcceptanceValidator::LazilyDefineAttributes
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::Module
|
|
Instance Chain:
self,
::Module ,
::Module::Concerning
|
|
Inherits: | Module |
Defined in: | activemodel/lib/active_model/validations/acceptance.rb |
Constant Summary
::Module
- Inherited
Class Attribute Summary
Class Method Summary
- .new(attributes) ⇒ LazilyDefineAttributes constructor
Instance Attribute Summary
- #attributes readonly protected
::Module
- Inherited
#anonymous? | A module may or may not have a name. |
Instance Method Summary
::Module
- Inherited
#alias_attribute | Allows you to make aliases for attributes, which includes getter, setter, and a predicate. |
#attr_internal | Alias for Module#attr_internal_accessor. |
#attr_internal_accessor | Declares an attribute reader and writer backed by an internally-named instance variable. |
#attr_internal_reader | Declares an attribute reader backed by an internally-named instance variable. |
#attr_internal_writer | Declares an attribute writer backed by an internally-named instance variable. |
#cattr_accessor | Alias for Module#mattr_accessor. |
#cattr_reader | Alias for Module#mattr_reader. |
#cattr_writer | Alias for Module#mattr_writer. |
#deep_dup | Returns a copy of module or class if it’s anonymous. |
#delegate | Provides a |
#delegate_missing_to | When building decorators, a common pattern may emerge: |
#deprecate | deprecate |
#mattr_accessor | Defines both class and instance accessors for class attributes. |
#mattr_reader | Defines a class attribute and creates a class and instance reader methods. |
#mattr_writer | Defines a class attribute and creates a class and instance writer methods to allow assignment to the attribute. |
#module_parent | Returns the module which contains this one according to its name. |
#module_parent_name | Returns the name of the module containing this one. |
#module_parents | Returns all the parents of this module according to its name, ordered from nested outwards. |
#redefine_method | Replaces the existing method definition, if there is one, with the passed block as its body. |
#redefine_singleton_method | Replaces the existing singleton method definition, if there is one, with the passed block as its body. |
#remove_possible_method | Removes the named method, if it exists. |
#remove_possible_singleton_method | Removes the named singleton method, if it exists. |
#silence_redefinition_of_method | Marks the named method as intended to be redefined, if it exists. |
#thread_cattr_accessor | Alias for Module#thread_mattr_accessor. |
#thread_cattr_reader | Alias for Module#thread_mattr_reader. |
#thread_cattr_writer | Alias for Module#thread_mattr_writer. |
#thread_mattr_accessor | Defines both class and instance accessors for class attributes. |
#attr_internal_define, #as_json, #method_visibility, | |
#thread_mattr_reader | Defines a per-thread class attribute and creates class and instance reader methods. |
#thread_mattr_writer | Defines a per-thread class attribute and creates a class and instance writer methods to allow assignment to the attribute. |
::Module::Concerning
- Included
#concern | A low-cruft shortcut to define a concern. |
#concerning | Define a new concern and mix it in. |
Constructor Details
.new(attributes) ⇒ LazilyDefineAttributes
# File 'activemodel/lib/active_model/validations/acceptance.rb', line 28
def initialize(attributes) @attributes = attributes.map(&:to_s) end
Instance Attribute Details
#attributes (readonly, protected)
[ GitHub ]# File 'activemodel/lib/active_model/validations/acceptance.rb', line 78
attr_reader :attributes
Instance Method Details
#==(other)
[ GitHub ]# File 'activemodel/lib/active_model/validations/acceptance.rb', line 73
def ==(other) self.class == other.class && attributes == other.attributes end
#define_on(klass)
[ GitHub ]# File 'activemodel/lib/active_model/validations/acceptance.rb', line 56
def define_on(klass) @lock&.synchronize do return unless @lock attr_readers = attributes.reject { |name| klass.attribute_method?(name) } attr_writers = attributes.reject { |name| klass.attribute_method?("#{name}=") } attr_reader(*attr_readers) attr_writer(*attr_writers) remove_method :respond_to_missing? remove_method :method_missing @lock = nil end end
#included(klass)
[ GitHub ]# File 'activemodel/lib/active_model/validations/acceptance.rb', line 32
def included(klass) @lock = Mutex.new mod = self define_method(:respond_to_missing?) do |method_name, include_private = false| mod.define_on(klass) super(method_name, include_private) || mod.matches?(method_name) end define_method(:method_missing) do |method_name, *args, &block| mod.define_on(klass) if mod.matches?(method_name) send(method_name, *args, &block) else super(method_name, *args, &block) end end end
#matches?(method_name) ⇒ Boolean
# File 'activemodel/lib/active_model/validations/acceptance.rb', line 51
def matches?(method_name) attr_name = method_name.to_s.chomp("=") attributes.any? { |name| name == attr_name } end