Sha256: 8ff18b538dd6d764d64b1b811614b52220dd579f2892a8b172afa9583b468e01

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

require 'dry/schema/constants'

module Dry
  module Schema
    # Path represents a list of keys in a hash
    #
    # @api private
    class Path
      # !@attribute [r] keys
      #   @return [Array<Symbol>]
      attr_reader :keys

      # Coerce a spec into a path object
      #
      # @param [Symbol, String, Hash, Array<Symbol>] spec
      #
      # @return [Path]
      #
      # @api private
      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 self
          spec
        else
          raise ArgumentError, '+spec+ must be either a Symbol, Array or Hash'
        end
      end

      # Extract a list of keys from a hash
      #
      # @api private
      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

      # @api private
      def initialize(keys)
        @keys = keys
      end

      # @api private
      def ==(other)
        keys == Path[other].keys
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-schema-0.2.0 lib/dry/schema/path.rb
dry-schema-0.1.1 lib/dry/schema/path.rb