Module: Primer::FetchOrFallbackHelper

Included in:
Component, Slot
Defined in:
lib/primer/fetch_or_fallback_helper.rb

Constant Summary collapse

InvalidValueError =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#fetch_or_fallback(allowed_values, given_value, fallback = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/primer/fetch_or_fallback_helper.rb', line 22

def fetch_or_fallback(allowed_values, given_value, fallback = nil)
  if allowed_values.include?(given_value)
    given_value
  else
    if fallback_raises && ENV["RAILS_ENV"] != "production" && ENV["STORYBOOK"] != "true"
      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



41
42
43
44
45
46
47
# File 'lib/primer/fetch_or_fallback_helper.rb', line 41

def fetch_or_fallback_boolean(given_value, fallback = false)
  if [true, false].include?(given_value)
    given_value
  else
    fallback
  end
end