Module: Ariadne::FetchOrFallbackHelper

Includes:
LoggerHelper
Included in:
Component, IconHelper
Defined in:
app/lib/ariadne/fetch_or_fallback_helper.rb

Overview

:nodoc:

Constant Summary collapse

InvalidValueError =
Class.new(StandardError)
TRUE_OR_FALSE =
Set.new([true, false]).freeze
INTEGER_TYPES =
Set.new(["Integer"]).freeze

Instance Method Summary collapse

Methods included from LoggerHelper

#logger, #silence_deprecations?, #silence_warnings?

Instance Method Details

#check_incoming_attribute(preferred_attribute, given_attribute) ⇒ Object

TODO: test this



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/lib/ariadne/fetch_or_fallback_helper.rb', line 73

def check_incoming_attribute(preferred_attribute, given_attribute)
  return preferred_attribute if given_attribute.blank? || preferred_attribute != given_attribute

  unless silence_warnings?
    message = <<~MSG
      Ariadne: note that `#{preferred_attribute}` is the preferred attribute here;
      you passed `#{given_attribute}` (which will still be used)
    MSG

    logger.warn(message)
  end

  given_attribute
end

#check_incoming_tag(preferred_tag, given_tag) ⇒ Object

TODO: test this



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/lib/ariadne/fetch_or_fallback_helper.rb', line 57

def check_incoming_tag(preferred_tag, given_tag)
  return preferred_tag if given_tag.blank? || preferred_tag == given_tag

  unless silence_warnings?
    message = <<~MSG
      Ariadne: note that `#{preferred_tag}` is the preferred tag here;
      you passed `#{given_tag}` (which will still be used)
    MSG

    logger.warn(message)
  end

  given_tag
end

#check_incoming_value(preferred_value, given_pair) ⇒ Object

TODO: test this



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/lib/ariadne/fetch_or_fallback_helper.rb', line 89

def check_incoming_value(preferred_value, given_pair)
  return preferred_value if given_pair.blank? || !given_pair.is_a?(Hash)

  given_key = given_pair.keys.first
  given_value = given_pair.values.first

  return preferred_value if given_value.blank?

  unless silence_warnings?

    message = <<~MSG
      Ariadne: note that `#{preferred_value}` is the preferred value for `#{given_key}` here;
      you passed `#{given_value}` (which will still be used)
    MSG

    logger.warn(message)
  end

  given_value
end

#fetch_or_raise(allowed_values, given_value, against: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/lib/ariadne/fetch_or_fallback_helper.rb', line 29

def fetch_or_raise(allowed_values, given_value, against: nil)
  if !allowed_values.is_a?(Array) && !allowed_values.is_a?(Set)
    raise ArgumentError, "allowed_values must be an array or a set; it was #{allowed_values.class}"
  end

  check_against_given_value = against || given_value
  if allowed_values.include?(check_against_given_value)
    given_value
  else
    raise InvalidValueError, <<~MSG
      fetch_or_raise was called with an invalid value.

      Expected one of: #{allowed_values.inspect}
      Got: #{given_value.inspect}
    MSG
  end
end

#fetch_or_raise_boolean(given_value) ⇒ Object

TODO: use these two more



48
49
50
# File 'app/lib/ariadne/fetch_or_fallback_helper.rb', line 48

def fetch_or_raise_boolean(given_value)
  fetch_or_raise(TRUE_OR_FALSE, given_value)
end

#fetch_or_raise_integer(given_value) ⇒ Object



52
53
54
# File 'app/lib/ariadne/fetch_or_fallback_helper.rb', line 52

def fetch_or_raise_integer(given_value)
  fetch_or_raise(INTEGER_TYPES, given_value, against: given_value.class.name)
end