Class: SmartCore::Validator::Rule
- Inherits:
-
Object
- Object
- SmartCore::Validator::Rule
- Defined in:
- lib/smart_core/validator/rule.rb
Instance Attribute Summary collapse
-
#checked_attr ⇒ Object
readonly
Returns the value of attribute checked_attr.
Instance Method Summary collapse
- #executable?(data) ⇒ Boolean
-
#initialize(checked_attr, checking_block) ⇒ Rule
constructor
A new instance of Rule.
- #to_proc ⇒ Object
- #value_from(data) ⇒ Object
Constructor Details
#initialize(checked_attr, checking_block) ⇒ Rule
Returns a new instance of Rule.
6 7 8 9 10 |
# File 'lib/smart_core/validator/rule.rb', line 6 def initialize(checked_attr, checking_block) @checked_attr = checked_attr.to_s @attr_path = @checked_attr.split('.').map(&:to_sym) @checking_block = checking_block end |
Instance Attribute Details
#checked_attr ⇒ Object (readonly)
Returns the value of attribute checked_attr.
4 5 6 |
# File 'lib/smart_core/validator/rule.rb', line 4 def checked_attr @checked_attr end |
Instance Method Details
#executable?(data) ⇒ Boolean
12 13 14 15 16 |
# File 'lib/smart_core/validator/rule.rb', line 12 def executable?(data) checked_path, checked_key = @attr_path[..-2], @attr_path.last data = data.dig(*checked_path) if checked_path.any? data&.key?(checked_key) end |
#to_proc ⇒ Object
22 23 24 |
# File 'lib/smart_core/validator/rule.rb', line 22 def to_proc @checking_block end |
#value_from(data) ⇒ Object
18 19 20 |
# File 'lib/smart_core/validator/rule.rb', line 18 def value_from(data) data.dig(*@attr_path) end |