Class: Validate::Constraints::ValidationContext

Inherits:
Object
  • Object
show all
Defined in:
lib/validate/constraints/validation_context.rb

Direct Known Subclasses

None

Defined Under Namespace

Classes: AttrPath, KeyPath, None, Path

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, path = Path.new, violations = []) ⇒ ValidationContext

Returns a new instance of ValidationContext.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/validate/constraints/validation_context.rb', line 21

def initialize(value, path = Path.new, violations = [])
  @value      = value
  @path       = path
  @violations = violations
  @keys = Hash.new do |hash, key|
    unless @value.respond_to?(:[]) || @value.respond_to_missing?(:[])
      raise Error::KeyError,
            "#{key.inspect}: value doesn't respond to :[]"
    end
    begin
      hash[key] = child_context(@value[key], KeyPath.new(key))
    rescue => e
      raise Error::KeyError,
            "#{key.inspect}: #{e.message}",
            cause: e
    end
  end
  @attrs = Hash.new do |hash, attr|
    unless @value.respond_to?(attr) || @value.respond_to_missing?(attr)
      raise Error::NameError,
            "#{attr.inspect}: value doesn't respond to #{attr.inspect}"
    end
    hash[attr] = child_context(@value.send(attr), AttrPath.new(attr))
  end
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



18
19
20
# File 'lib/validate/constraints/validation_context.rb', line 18

def value
  @value
end

Class Method Details

.key(key, violations = []) ⇒ Object



14
15
16
# File 'lib/validate/constraints/validation_context.rb', line 14

def self.key(key, violations = [])
  new(key, Path.new([KeyPath.new(key)]), violations)
end

.noneObject



6
7
8
# File 'lib/validate/constraints/validation_context.rb', line 6

def self.none
  @none ||= None.new
end

.root(value, violations = []) ⇒ Object



10
11
12
# File 'lib/validate/constraints/validation_context.rb', line 10

def self.root(value, violations = [])
  new(value, Path.new, violations)
end

Instance Method Details

#[](key) ⇒ Object



47
48
49
# File 'lib/validate/constraints/validation_context.rb', line 47

def [](key)
  @keys[key]
end

#add_violation(constraint) ⇒ Object



55
56
57
58
# File 'lib/validate/constraints/validation_context.rb', line 55

def add_violation(constraint)
  @violations << create_violation(constraint)
  self
end

#attr(name) ⇒ Object



51
52
53
# File 'lib/validate/constraints/validation_context.rb', line 51

def attr(name)
  @attrs[name]
end

#clear_violationsObject



60
61
62
63
# File 'lib/validate/constraints/validation_context.rb', line 60

def clear_violations
  @violations.clear
  self
end

#has_violations?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/validate/constraints/validation_context.rb', line 65

def has_violations?
  !@violations.empty?
end

#merge(other) ⇒ Object



75
76
77
78
79
80
# File 'lib/validate/constraints/validation_context.rb', line 75

def merge(other)
  other.violations.each do |violation|
    @violations << Constraint::Violation.new(violation.value, @path.child(violation.path), violation.constraint)
  end
  self
end

#to_err(backtrace = []) ⇒ Object



69
70
71
72
73
# File 'lib/validate/constraints/validation_context.rb', line 69

def to_err(backtrace = [])
  err = Error::ConstraintViolationError.new(@violations.freeze)
  err.set_backtrace(backtrace)
  err
end