Class: Dry::Schema::Path Private

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/dry/schema/path.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Path represents a list of keys in a hash

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keys) ⇒ Path

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Path.



51
52
53
# File 'lib/dry/schema/path.rb', line 51

def initialize(keys)
  @keys = keys
end

Instance Attribute Details

#keysArray<Symbol> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Array<Symbol>)


15
16
17
# File 'lib/dry/schema/path.rb', line 15

def keys
  @keys
end

Class Method Details

.[](spec) ⇒ Path

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerce a spec into a path object

Parameters:

  • spec (Path, Symbol, String, Hash, Array<Symbol>)

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dry/schema/path.rb', line 26

def self.[](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 Path
    spec
  else
    raise ArgumentError, '+spec+ must be either a Symbol, Array, Hash or a Path'
  end
end

.keys_from_hash(hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extract a list of keys from a hash



44
45
46
47
48
# File 'lib/dry/schema/path.rb', line 44

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

Instance Method Details

#<=>(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (ArgumentError)


75
76
77
78
79
80
81
82
83
# File 'lib/dry/schema/path.rb', line 75

def <=>(other)
  raise ArgumentError, "Can't compare paths from different branches" unless same_root?(other)

  return 0 if keys.eql?(other.keys)

  res = key_matches(other).compact.reject { |value| value.equal?(false) }

  res.size < count ? 1 : -1
end

#each(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
# File 'lib/dry/schema/path.rb', line 56

def each(&block)
  keys.each(&block)
end

#include?(other) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


66
67
68
69
70
71
72
# File 'lib/dry/schema/path.rb', line 66

def include?(other)
  return false unless same_root?(other)
  return last.equal?(other.last) if index? && other.index?
  return self.class.new([*to_a[0..-2]]).include?(other) if index?

  self >= other && !other.key_matches(self).include?(nil)
end

#index(key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



61
62
63
# File 'lib/dry/schema/path.rb', line 61

def index(key)
  keys.index(key)
end

#index?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


101
102
103
# File 'lib/dry/schema/path.rb', line 101

def index?
  last.is_a?(Integer)
end

#key_matches(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



86
87
88
# File 'lib/dry/schema/path.rb', line 86

def key_matches(other)
  map { |key| (idx = other.index(key)) && keys[idx].equal?(key) }
end

#lastObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



91
92
93
# File 'lib/dry/schema/path.rb', line 91

def last
  keys.last
end

#same_root?(other) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


96
97
98
# File 'lib/dry/schema/path.rb', line 96

def same_root?(other)
  root.equal?(other.root)
end