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

Constant Summary collapse

EMPTY =

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

new(EMPTY_ARRAY).freeze

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.



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

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>)


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

def keys
  @keys
end

Class Method Details

.[](spec) ⇒ 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.



43
44
45
# File 'lib/dry/schema/path.rb', line 43

def self.[](spec)
  call(spec)
end

.call(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:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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) ⇒ 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



50
51
52
53
54
# 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

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.



88
89
90
91
92
# 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) ⇒ 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.



79
80
81
82
83
84
85
# 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) ⇒ 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.



69
70
71
# File 'lib/dry/schema/path.rb', line 69

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)


74
75
76
# File 'lib/dry/schema/path.rb', line 74

def include?(other)
  keys[0, other.keys.length].eql?(other.keys)
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.



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

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)


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

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

#to_h(value = EMPTY_ARRAY.dup) ⇒ 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.



62
63
64
65
66
# 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