Class: Phillumeny::ActiveModel::ValidatePresenceOfAny
- Inherits:
-
Object
- Object
- Phillumeny::ActiveModel::ValidatePresenceOfAny
- Defined in:
- lib/phillumeny/active_model.rb
Overview
Used for testing conditional validation of presence where you need one of the other
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
Instance Method Summary collapse
-
#description ⇒ String
private
Description used when matcher is used.
-
#failure_message ⇒ String
private
Compiles the error message for display.
-
#initialize(args) ⇒ void
constructor
private
Sets up arguments for the matcher.
-
#matches?(subject) ⇒ Boolean
private
Runs the logic to determine if expectations are being met.
-
#valid_value(attribute, value) ⇒ self
Explicitly set the value to test for an attribute if ‘X’ is not acceptable.
-
#valid_values(**values) ⇒ self
Explicitly set the value to test for an attribute if ‘X’ is not acceptable.
Constructor Details
#initialize(args) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets up arguments for the matcher
77 78 79 |
# File 'lib/phillumeny/active_model.rb', line 77 def initialize(args) @attributes = args end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
47 48 49 |
# File 'lib/phillumeny/active_model.rb', line 47 def attributes @attributes end |
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
48 49 50 |
# File 'lib/phillumeny/active_model.rb', line 48 def subject @subject end |
Instance Method Details
#description ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Description used when matcher is used
55 56 57 |
# File 'lib/phillumeny/active_model.rb', line 55 def description "validate the presence of any of these attributes: #{attributes.join(', ')}" end |
#failure_message ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Compiles the error message for display
64 65 66 67 68 69 70 |
# File 'lib/phillumeny/active_model.rb', line 64 def = [subject.inspect] attributes.each do |attribute| << subject.errors.(attribute) end .compact.join("\n") end |
#matches?(subject) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Runs the logic to determine if expectations are being met
86 87 88 89 90 91 92 93 94 |
# File 'lib/phillumeny/active_model.rb', line 86 def matches?(subject) @subject = subject store_initial_values invalid_when_none_present? && attributes.all? do |attribute| clear_attributes_and_errors initialize_value(attribute) free_of_errors_on_attribute?(attribute) ? other_attributes_valid?(attribute) : false end end |
#valid_value(attribute, value) ⇒ self
Explicitly set the value to test for an attribute if ‘X’ is not acceptable
104 105 106 107 |
# File 'lib/phillumeny/active_model.rb', line 104 def valid_value(attribute, value) attribute_values[attribute] = value self end |
#valid_values(**values) ⇒ self
Explicitly set the value to test for an attribute if ‘X’ is not acceptable
119 120 121 122 |
# File 'lib/phillumeny/active_model.rb', line 119 def valid_values(**values) attribute_values.merge!(values) self end |