Class: RSpec::JsonMatchers::Utils::KeyPath::Extraction Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/json_matchers/utils/key_path/extraction.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 an extractor that performs the extraction with a #path & #object

Defined Under Namespace

Classes: ExtractionWithOnePathPart, Result

Instance Method Summary collapse

Constructor Details

#initialize(object, path) ⇒ Extraction

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.

Create a new extractor with the “source object” and the path to be used for extracting our target object

Parameters:

  • object (Object)

    The source object to extract our target object from

  • path (String, Path)

    the path of target object Will convert into Path

See Also:

  • JsonMatchers::Matchers::BeJsonWithSomethingMatcher#at_path


21
22
23
24
25
26
# File 'lib/rspec/json_matchers/utils/key_path/extraction.rb', line 21

def initialize(object, path)
  @object = object
  @path = KeyPath::Path.new(path)

  @failed = false
end

Instance Method Details

#extractExtraction::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.

Actually perform the extraction and return the result Since the object could be falsy, an object of custom class is returned instead of the object only

Assume the path to be valid

Returns:



35
36
37
38
39
40
41
42
43
44
# File 'lib/rspec/json_matchers/utils/key_path/extraction.rb', line 35

def extract
  path.each_path_part do |path_part|
    result = extract_object_with_path_part(path_part)
    return Result.new(object, false) if result.failed?

    self.object = result.object
  end

  Result.new(object, true)
end