Module: Primer::FetchOrFallbackHelper
- Included in:
- Component, ResponsiveArg
- Defined in:
- app/lib/primer/fetch_or_fallback_helper.rb
Overview
:nodoc:
Constant Summary collapse
- InvalidValueError =
Class.new(StandardError)
Instance Method Summary collapse
- #fetch_or_fallback(allowed_values, given_value, fallback = nil, deprecated_values: nil) ⇒ Object
- #fetch_or_fallback_boolean(given_value, fallback = false) ⇒ Object
-
#silence_deprecations? ⇒ Boolean
rubocop:enable Style/OptionalBooleanParameter.
Instance Method Details
#fetch_or_fallback(allowed_values, given_value, fallback = nil, deprecated_values: nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/lib/primer/fetch_or_fallback_helper.rb', line 28 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) ::Primer::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_fallback_boolean(given_value, fallback = false) ⇒ Object
51 52 53 54 55 56 57 |
# File 'app/lib/primer/fetch_or_fallback_helper.rb', line 51 def fetch_or_fallback_boolean(given_value, fallback = false) if [true, false].include?(given_value) given_value else fallback end end |
#silence_deprecations? ⇒ Boolean
rubocop:enable Style/OptionalBooleanParameter
60 61 62 |
# File 'app/lib/primer/fetch_or_fallback_helper.rb', line 60 def silence_deprecations? Rails.application.config.primer_view_components.silence_deprecations end |