Class: Knight::Rule::Format
- Inherits:
-
Knight::Rule
- Object
- Knight::Rule
- Knight::Rule::Format
- Defined in:
- lib/knight/rule/format.rb
Overview
A rule for checking the format of a value
Constant Summary collapse
- DEFAULT_MESSAGE =
'%{attribute} has an invalid format'.freeze
Instance Attribute Summary collapse
-
#regexp ⇒ Regexp
readonly
private
Regexp format.
Attributes inherited from Knight::Rule
#attribute_name, #message, #options
Instance Method Summary collapse
-
#initialize(attribute_name, regexp, options = {}) ⇒ undefined
constructor
Initialize a format rule.
-
#matches?(value) ⇒ true, false
private
Check value presence.
-
#to_hash ⇒ Hash
Return the rule as a hash.
Methods inherited from Knight::Rule
Constructor Details
#initialize(attribute_name, regexp, options = {}) ⇒ undefined
Initialize a format rule
30 31 32 33 |
# File 'lib/knight/rule/format.rb', line 30 def initialize(attribute_name, regexp, = {}) super(attribute_name, ) @regexp = regexp end |
Instance Attribute Details
#regexp ⇒ Regexp (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.
Regexp format
15 16 17 |
# File 'lib/knight/rule/format.rb', line 15 def regexp @regexp end |
Instance Method Details
#matches?(value) ⇒ true, false
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.
Check value presence
43 44 45 |
# File 'lib/knight/rule/format.rb', line 43 def matches?(value) !!(regexp =~ value) end |
#to_hash ⇒ Hash
Return the rule as a hash
55 56 57 58 59 |
# File 'lib/knight/rule/format.rb', line 55 def to_hash super.merge({ format: regexp }).freeze end |