Class: RSpec::JsonMatchers::Utils::KeyPath::Path Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/json_matchers/utils/key_path/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.

Represents a path pointing to an element of a Hash or Array

Constant Summary collapse

PATH_PART_SPLITTER =

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.

The “path part” separator Period is used since it’s the least used char as part of a key name (it can never by used for index anyway) As a side effect this char CANNOT be used, escaping is not planned to be added

".".freeze
INVALID_PATH_REGEX =

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.

The regular expression for checking “invalid” path The separator should NOT at the start/end of the string, or repeating itself without other chars in between

/
(
^#{Regexp.escape(PATH_PART_SPLITTER)}
|
#{Regexp.escape(PATH_PART_SPLITTER)}{2,}
|
#{Regexp.escape(PATH_PART_SPLITTER)}$
)
/x

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ 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.

Note:

It does not copy the string object since there is not need This might lead to some strange bug

Creates a RSpec::JsonMatchers::Utils::KeyPath::Path with a String (mainly from external) (will store it internally) or a RSpec::JsonMatchers::Utils::KeyPath::Path (mainly from internal) (will get and assign the string path it internally)

Parameters:

Raises:



45
46
47
48
49
50
51
52
53
54
# File 'lib/rspec/json_matchers/utils/key_path/path.rb', line 45

def initialize(path)
  case path
  when Path
    @string_path = path.string_path
  when String
    @string_path = path
  else
    fail TypeError, "Only String and Path is expected"
  end
end

Instance Method Details

#each_path_part {|part| ... } ⇒ 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.

Run a loop on all “path parts” without exposing the parts a block is required

Yield Parameters:

  • part (String)

    a “path part” if the #string_path

Returns:

  • (Path)

    The path object itself



66
67
68
69
# File 'lib/rspec/json_matchers/utils/key_path/path.rb', line 66

def each_path_part(&block)
  path_parts.each(&block)
  self
end

#extract(object) ⇒ Extraction::Result

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.

Return successful extraction result when path is empty Return failed extraction result when path is invalid Delegate to Extractor otherwise

Parameters:

  • object (Object)

    The “source object” to extract our “target object” from

Returns:



79
80
81
82
83
# File 'lib/rspec/json_matchers/utils/key_path/path.rb', line 79

def extract(object)
  return Extraction::Result.new(object, true) if empty?
  return Extraction::Result.new(object, false) if invalid?
  Extraction.new(object, self).extract
end

#valid?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)


56
57
58
# File 'lib/rspec/json_matchers/utils/key_path/path.rb', line 56

def valid?
  !invalid?
end