Class: Knight::Rule::Inclusion
- Inherits:
-
Knight::Rule
- Object
- Knight::Rule
- Knight::Rule::Inclusion
- Defined in:
- lib/knight/rule/inclusion.rb
Overview
A rule for checking the inclusion of a value
Constant Summary collapse
- DEFAULT_MESSAGE =
'%{attribute} is not included in the list'.freeze
Instance Attribute Summary collapse
-
#within ⇒ Range, Array
readonly
private
Inclusion value.
Attributes inherited from Knight::Rule
#attribute_name, #message, #options
Instance Method Summary collapse
-
#initialize(attribute_name, within, options = {}) ⇒ undefined
constructor
Initialize an inclusion 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, within, options = {}) ⇒ undefined
Initialize an inclusion rule
30 31 32 33 |
# File 'lib/knight/rule/inclusion.rb', line 30 def initialize(attribute_name, within, = {}) super(attribute_name, ) @within = within end |
Instance Attribute Details
#within ⇒ Range, Array (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.
Inclusion value
15 16 17 |
# File 'lib/knight/rule/inclusion.rb', line 15 def within @within 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/inclusion.rb', line 43 def matches?(value) within.respond_to?(:cover?) ? within.cover?(value) : within.include?(value) end |
#to_hash ⇒ Hash
Return the rule as a hash
55 56 57 58 59 |
# File 'lib/knight/rule/inclusion.rb', line 55 def to_hash super.merge({ within: within }).freeze end |