Module: Essence::FetchOrFallbackHelper
- Included in:
- ApplicationComponent
- Defined in:
- app/helpers/essence/fetch_or_fallback_helper.rb
Constant Summary collapse
- InvalidValueError =
Class.new(StandardError)
Instance Method Summary collapse
- #fetch_or_fallback(allowed_values, given_value, fallback = nil) ⇒ Object
- #fetch_or_fallback_boolean(given_value, fallback: false) ⇒ Object
- #integer_or_fallback(given_value, fallback = nil) ⇒ Object
Instance Method Details
#fetch_or_fallback(allowed_values, given_value, fallback = nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'app/helpers/essence/fetch_or_fallback_helper.rb', line 7 def fetch_or_fallback(allowed_values, given_value, fallback = nil) if allowed_values.include?(given_value) given_value else if Rails.env.local? 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
26 27 28 29 30 31 32 |
# File 'app/helpers/essence/fetch_or_fallback_helper.rb', line 26 def fetch_or_fallback_boolean(given_value, fallback: false) if [true, false].include?(given_value) given_value else fallback end end |
#integer_or_fallback(given_value, fallback = nil) ⇒ Object
34 35 36 |
# File 'app/helpers/essence/fetch_or_fallback_helper.rb', line 34 def integer_or_fallback(given_value, fallback = nil) given_value.is_a?(Integer) ? given_value : fallback end |