class JsonRspecMatchMaker::ExpectedValue

Handles fetching the expected value from the expected instance

Attributes

value[R]

Public Class Methods

new(match_function, expected_instance, error_key, prefix) click to toggle source
# File lib/json_rspec_match_maker/expected_value.rb, line 7
def initialize(match_function, expected_instance, error_key, prefix)
  @value = fetch_expected_value(expected_instance, match_function, error_key, prefix)
end

Public Instance Methods

==(other) click to toggle source
# File lib/json_rspec_match_maker/expected_value.rb, line 11
def ==(other)
  raise ArgumentError unless other.is_a? TargetValue
  other.value == value
end

Private Instance Methods

fetch_expected_value(instance, function, key, prefix) click to toggle source
# File lib/json_rspec_match_maker/expected_value.rb, line 18
def fetch_expected_value(instance, function, key, prefix)
  if function == :default
    key.split('.').inject(instance) do |expected, method_name|
      method_name = method_name.chomp('_attributes')
      method_name == prefix ? expected : expected&.send(method_name)
    rescue NoMethodError
      nil
    end
  else
    function.call(instance)
  end
end