Module: Ariadne::FetchOrFallbackHelper
- Includes:
- LoggerHelper
- Included in:
- IconHelper, UI::TimeAgo::Component
- 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
-
#check_incoming_attribute(preferred_attribute, given_attribute) ⇒ Object
TODO: test this.
-
#check_incoming_tag(preferred_tag, given_tag) ⇒ Object
TODO: test this.
-
#check_incoming_value(preferred_value, given_pair) ⇒ Object
TODO: test this.
- #fetch_or_fallback(allowed_values, given_value, fallback = nil, deprecated_values: nil) ⇒ Object
- #fetch_or_raise(allowed_values, given_value, against: nil) ⇒ Object
-
#fetch_or_raise_boolean(given_value) ⇒ Object
TODO: use these two more.
- #fetch_or_raise_integer(given_value) ⇒ Object
Methods included from LoggerHelper
#logger, #silence_deprecations?, #silence_warnings?
Instance Method Details
#check_incoming_attribute(preferred_attribute, given_attribute) ⇒ Object
TODO: test this
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'app/lib/ariadne/fetch_or_fallback_helper.rb', line 96 def check_incoming_attribute(preferred_attribute, given_attribute) return preferred_attribute if given_attribute.blank? || preferred_attribute != given_attribute unless silence_warnings? = <<~MSG Ariadne: note that `#{preferred_attribute}` is the preferred attribute for `#{self.class.name}`; you passed `#{given_attribute}` (which will still be used) MSG logger.warn() end given_attribute end |
#check_incoming_tag(preferred_tag, given_tag) ⇒ Object
TODO: test this
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/lib/ariadne/fetch_or_fallback_helper.rb', line 80 def check_incoming_tag(preferred_tag, given_tag) return preferred_tag if given_tag.blank? || preferred_tag == given_tag unless silence_warnings? = <<~MSG Ariadne: note that `#{preferred_tag}` is the preferred tag for `#{self.class.name}`; you passed `#{given_tag}` (which will still be used) MSG logger.warn() end given_tag end |
#check_incoming_value(preferred_value, given_pair) ⇒ Object
TODO: test this
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'app/lib/ariadne/fetch_or_fallback_helper.rb', line 112 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? = <<~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() end given_value end |
#fetch_or_fallback(allowed_values, given_value, fallback = nil, deprecated_values: nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/lib/ariadne/fetch_or_fallback_helper.rb', line 29 def fetch_or_fallback(allowed_values, given_value, fallback = nil, deprecated_values: nil) if allowed_values.include?(given_value) given_value elsif deprecated_values&.include?(given_value) ::Ariadne::ViewComponents.deprecation.warn("#{given_value} is deprecated and will be removed in a future version.") unless Rails.env.production? || silence_deprecations? given_value else if fallback_raises && ENV["RAILS_ENV"] != "production" raise InvalidValueError, <<~MSG fetch_or_fallback was called with an invalid value. Expected one of: #{allowed_values.inspect} Got: #{given_value.inspect} This will not raise in production, but will instead fallback to: #{fallback.inspect} MSG end fallback end end |
#fetch_or_raise(allowed_values, given_value, against: nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/lib/ariadne/fetch_or_fallback_helper.rb', line 52 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 elsif fallback_raises 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
71 72 73 |
# File 'app/lib/ariadne/fetch_or_fallback_helper.rb', line 71 def fetch_or_raise_boolean(given_value) fetch_or_raise(TRUE_OR_FALSE, given_value) end |
#fetch_or_raise_integer(given_value) ⇒ Object
75 76 77 |
# File 'app/lib/ariadne/fetch_or_fallback_helper.rb', line 75 def fetch_or_raise_integer(given_value) fetch_or_raise(INTEGER_TYPES, given_value, against: given_value.class.name) end |