class Dry::Schema::Path

Path represents a list of keys in a hash

@api private

Constants

EMPTY

Attributes

keys[R]

@return [Array<Symbol>]

Public Class Methods

[](spec) click to toggle source

@api private

# File lib/dry/schema/path.rb, line 43
def self.[](spec)
  call(spec)
end
call(spec) click to toggle source

Coerce a spec into a path object

@param [Path, Symbol, String, Hash, Array<Symbol>] spec

@return [Path]

@api private

# File lib/dry/schema/path.rb, line 27
def self.call(spec)
  case spec
  when Symbol, Array
    new(Array[*spec])
  when String
    new(spec.split(DOT).map(&:to_sym))
  when Hash
    new(keys_from_hash(spec))
  when self
    spec
  else
    raise ArgumentError, "+spec+ must be either a Symbol, Array, Hash or a #{name}"
  end
end
keys_from_hash(hash) click to toggle source

Extract a list of keys from a hash

@api private

# File lib/dry/schema/path.rb, line 50
def self.keys_from_hash(hash)
  hash.inject([]) { |a, (k, v)|
    v.is_a?(Hash) ? a.concat([k, *keys_from_hash(v)]) : a.concat([k, v])
  }
end
new(keys) click to toggle source

@api private

# File lib/dry/schema/path.rb, line 57
def initialize(keys)
  @keys = keys
end

Public Instance Methods

&(other) click to toggle source

@api private

# File lib/dry/schema/path.rb, line 88
def &(other)
  self.class.new(
    keys.take_while.with_index { |key, index| other.keys[index].eql?(key) }
  )
end
<=>(other) click to toggle source

@api private

# File lib/dry/schema/path.rb, line 79
def <=>(other)
  return keys.length <=> other.keys.length if include?(other) || other.include?(self)

  first_uncommon_index = (self & other).keys.length

  keys[first_uncommon_index] <=> other.keys[first_uncommon_index]
end
each(&block) click to toggle source

@api private

# File lib/dry/schema/path.rb, line 69
def each(&block)
  keys.each(&block)
end
include?(other) click to toggle source

@api private

# File lib/dry/schema/path.rb, line 74
def include?(other)
  keys[0, other.keys.length].eql?(other.keys)
end
last() click to toggle source

@api private

# File lib/dry/schema/path.rb, line 95
def last
  keys.last
end
same_root?(other) click to toggle source

@api private

# File lib/dry/schema/path.rb, line 100
def same_root?(other)
  root.equal?(other.root)
end
to_h(value = EMPTY_ARRAY.dup) click to toggle source

@api private

# File lib/dry/schema/path.rb, line 62
def to_h(value = EMPTY_ARRAY.dup)
  value = [value] unless value.is_a?(Array)

  keys.reverse_each.reduce(value) { |result, key| {key => result} }
end