Class: Validate::Constraints::ValidationContext::Path

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/validate/constraints/validation_context.rb

Instance Method Summary collapse

Constructor Details

#initialize(paths = []) ⇒ Path

Returns a new instance of Path.



99
100
101
# File 'lib/validate/constraints/validation_context.rb', line 99

def initialize(paths = [])
  @paths = paths
end

Instance Method Details

#at(index) ⇒ Object

Raises:



120
121
122
123
124
125
# File 'lib/validate/constraints/validation_context.rb', line 120

def at(index)
  raise Error::IndexError if index.negative?

  return nil if index.zero?
  @paths.fetch(index - 1)
end

#child(path) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/validate/constraints/validation_context.rb', line 103

def child(path)
  case path
  when KeyPath, AttrPath
    Path.new(@paths.dup << path)
  when Path
    Path.new(@paths.dup << path.to_a)
  else
    raise ArgumentError, 'invalid path'
  end
end

#inspectObject



127
128
129
130
131
# File 'lib/validate/constraints/validation_context.rb', line 127

def inspect
  return "#<#{self.class.name} <root>>" if @paths.empty?

  "#<#{self.class.name} #{to_s}>"
end

#to_sObject



114
115
116
117
118
# File 'lib/validate/constraints/validation_context.rb', line 114

def to_s
  return '.' if @paths.empty?

  @paths.join
end