Class: Knight::Rule
- Inherits:
-
Object
- Object
- Knight::Rule
- Includes:
- AbstractType
- Defined in:
- lib/knight/rule.rb,
lib/knight/rule/format.rb,
lib/knight/rule/presence.rb,
lib/knight/rule/inclusion.rb,
lib/knight/rule/exact_length.rb,
lib/knight/rule/range_length.rb,
lib/knight/rule/maximum_length.rb,
lib/knight/rule/minimum_length.rb
Overview
An abstract class of a rule
Direct Known Subclasses
ExactLength, Format, Inclusion, MaximumLength, MinimumLength, Presence, RangeLength
Defined Under Namespace
Classes: ExactLength, Format, Inclusion, MaximumLength, MinimumLength, Presence, RangeLength
Constant Summary collapse
- DEFAULT_MESSAGE =
''.freeze
Instance Attribute Summary collapse
-
#attribute_name ⇒ Symbol
readonly
private
Attribute name to validate.
-
#message ⇒ String
readonly
private
Message to displayed when rule is violated.
-
#options ⇒ Symbol
readonly
private
Attribute name to validate.
Instance Method Summary collapse
-
#error(resource) ⇒ Error?
private
Return error if the rule not satisfied.
-
#initialize(attribute_name, options = {}) ⇒ undefined
constructor
private
Initialize a rule.
-
#to_hash ⇒ Hash
Return the rule as a hash.
Constructor Details
#initialize(attribute_name, options = {}) ⇒ undefined
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.
Initialize a rule
43 44 45 46 47 |
# File 'lib/knight/rule.rb', line 43 def initialize(attribute_name, = {}) @attribute_name = attribute_name @options = @message = @options.fetch(:message, self.class::DEFAULT_MESSAGE) end |
Instance Attribute Details
#attribute_name ⇒ Symbol (readonly)
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.
Attribute name to validate
17 18 19 |
# File 'lib/knight/rule.rb', line 17 def attribute_name @attribute_name end |
#message ⇒ String (readonly)
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.
Message to displayed when rule is violated
31 32 33 |
# File 'lib/knight/rule.rb', line 31 def @message end |
#options ⇒ Symbol (readonly)
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.
Attribute name to validate
24 25 26 |
# File 'lib/knight/rule.rb', line 24 def @options end |
Instance Method Details
#error(resource) ⇒ Error?
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.
Return error if the rule not satisfied
57 58 59 60 |
# File 'lib/knight/rule.rb', line 57 def error(resource) value = resource.public_send(attribute_name) Error.new(self, resource) unless matches?(value) end |
#to_hash ⇒ Hash
Return the rule as a hash
70 71 72 |
# File 'lib/knight/rule.rb', line 70 def to_hash { attribute: attribute_name }.freeze end |