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)


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

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

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

  res =
    map { |key| (idx = other.index(key)) && keys[idx].equal?(key) }
      .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
# File 'lib/dry/schema/path.rb', line 66

def include?(other)
  return false unless same_root?(other)
  return false if index? && other.index? && !last.equal?(other.last)
  self >= other
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)


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

def index?
  last.is_a?(Integer)
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.



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

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)


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

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