Class: SmartCore::Validator::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_core/validator/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_attrObject (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

Returns:

  • (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_procObject



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