Class: KeyTree::Path
- Inherits:
-
Array
- Object
- Array
- KeyTree::Path
- Defined in:
- lib/key_tree/path.rb
Overview
Representation of the key path to a value in a key tree
Class Method Summary collapse
-
.[](*key_paths) ⇒ Object
KeyTree::Path[
key_or_path, …].
Instance Method Summary collapse
- #+(other) ⇒ Object
- #<<(other) ⇒ Object
-
#conflict?(other) ⇒ Boolean
Would
otherconflict?. -
#drop(other) ⇒ Object
Returns a key path without the leading
prefix. -
#initialize(key_path = nil) ⇒ Path
constructor
KeyTree::Path.new(
key_or_path). - #inspect ⇒ Object
-
#prefix?(other) ⇒ Boolean
(also: #===)
Is
othera prefix?. - #to_s ⇒ Object
Constructor Details
#initialize(key_path = nil) ⇒ Path
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"]
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/key_tree/path.rb', line 33 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 |
Class Method Details
.[](*key_paths) ⇒ Object
KeyTree::Path[key_or_path, …]
Make a new key path from one or more keys or paths
17 18 19 20 21 |
# File 'lib/key_tree/path.rb', line 17 def self.[](*key_paths) key_paths.reduce(Path.new) do |result, key_path| result << Path.new(key_path) end end |
Instance Method Details
#+(other) ⇒ Object
58 59 60 |
# File 'lib/key_tree/path.rb', line 58 def +(other) dup.concat(other.to_key_path) end |
#<<(other) ⇒ Object
54 55 56 |
# File 'lib/key_tree/path.rb', line 54 def <<(other) concat(other.to_key_path) end |
#conflict?(other) ⇒ Boolean
Would other conflict?
88 89 90 |
# File 'lib/key_tree/path.rb', line 88 def conflict?(other) prefix?(other) || other.prefix?(self) if self != other end |
#drop(other) ⇒ Object
Returns a key path without the leading prefix
:call-seq:
drop(other) => Path
66 67 68 69 70 71 |
# File 'lib/key_tree/path.rb', line 66 def drop(other) other = other.to_key_path raise KeyError unless prefix?(other) super(other.length) end |
#inspect ⇒ Object
50 51 52 |
# File 'lib/key_tree/path.rb', line 50 def inspect %("#{self}") end |
#prefix?(other) ⇒ Boolean Also known as: ===
Is other a prefix?
:call-seq:
prefix?(other) => boolean
77 78 79 80 81 82 83 |
# File 'lib/key_tree/path.rb', line 77 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 |
#to_s ⇒ Object
46 47 48 |
# File 'lib/key_tree/path.rb', line 46 def to_s join('.') end |