Sha256: 26224544c212dd9764999acabeda6c762be5bf402143955c00fe972a770ec624
Contents?: true
Size: 1.81 KB
Versions: 2
Compression:
Stored size: 1.81 KB
Contents
# frozen_string_literal: true require_relative 'refinements' module KeyTree # rubocop:disable Style/Documentation using Refinements # # Representation of the key path to a value in a key tree # class Path < Array # # KeyTree::Path[+key_or_path+, ...] # # Make a new key path from one or more keys or paths # def self.[](*key_paths) key_paths.reduce(Path.new) do |result, key_path| result << key_path.to_key_path end end # # KeyTree::Path.new(+key_or_path+) # # Make a new key path from a dot separated string, single symbol, # or array of strings or symbols. # # Example: # KeyTree::Path.new("a.b.c") # => ["a", "b", "c"] # def initialize(key_path = nil) case key_path when NilClass nil when Array concat(key_path.map(&:to_sym)) else initialize(key_path.to_key_path) end end def to_key_path self end def to_s join('.') end def inspect %("#{self}") end def <<(other) concat(other.to_key_path) end def +(other) dup.concat(other.to_key_path) end # Returns a key path without the leading +prefix+ # # :call-seq: # Path - other => Path def -(other) other = other.to_key_path raise KeyError unless prefix?(other) super(other.length) end # Is +other+ a prefix? # # :call-seq: # prefix?(other) => boolean def prefix?(other) other = other.to_key_path return false if other.length > length key_enum = each other.all? { |other_key| key_enum.next == other_key } end alias === prefix? # Would +other+ conflict? # def conflict?(other) prefix?(other) || other.prefix?(self) if self != other end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
key_tree-0.6.1 | lib/key_tree/path.rb |
key_tree-0.6.0 | lib/key_tree/path.rb |