Class: RSpec::GraphqlAssistant::Matchers::Include

Inherits:
Object
  • Object
show all
Includes:
MatcherHelper
Defined in:
lib/rspec/graphql_assistant/matchers/include.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MatcherHelper

#key_to_array, #lookup_value

Constructor Details

#initialize(key, expected, **args) ⇒ Include

Returns a new instance of Include.



8
9
10
11
12
# File 'lib/rspec/graphql_assistant/matchers/include.rb', line 8

def initialize(key, expected, **args)
  @expected = expected
  @key = key
  @root = args.fetch(:root, :data)
end

Instance Attribute Details

#expectedObject (readonly)

Returns the value of attribute expected.



6
7
8
# File 'lib/rspec/graphql_assistant/matchers/include.rb', line 6

def expected
  @expected
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/rspec/graphql_assistant/matchers/include.rb', line 6

def key
  @key
end

#rootObject (readonly)

Returns the value of attribute root.



6
7
8
# File 'lib/rspec/graphql_assistant/matchers/include.rb', line 6

def root
  @root
end

#sampleObject (readonly)

Returns the value of attribute sample.



6
7
8
# File 'lib/rspec/graphql_assistant/matchers/include.rb', line 6

def sample
  @sample
end

Instance Method Details

#descriptionObject



26
27
28
# File 'lib/rspec/graphql_assistant/matchers/include.rb', line 26

def description
  "'#{key}' has attributes '#{expected}'"
end

#failure_messageObject



22
23
24
# File 'lib/rspec/graphql_assistant/matchers/include.rb', line 22

def failure_message
  "'#{key}' not include all attributes '#{expected}'"
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
# File 'lib/rspec/graphql_assistant/matchers/include.rb', line 14

def matches?(actual)
  value = lookup_value(actual)

  return value.any? { |k, _v| expected.include? k } if value.is_a?(Hash)

  value.all? { |el| el.any? { |k, _v| expected.include? k } } if value.is_a?(Array)
end