Sha256: 9afdfb607cb9076ec2d80be0ba206bd00fe4c30d48486ed8a0386eb4786f29a6

Contents?: true

Size: 1.25 KB

Versions: 24

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

# Ariadne::FetchOrFallbackHelper
# A little helper to enable graceful fallbacks
#
# Use this helper to quietly ensure a value is
# one that you expect:
#
# allowed_values    - allowed options for *value*
# given_value       - input being coerced
# fallback          - returned if *given_value* is not included in *allowed_values*
#
# fetch_or_raise([1,2,3], 5) => 2
# fetch_or_raise([1,2,3], 1) => 1
# fetch_or_raise([1,2,3], nil) => 2
module Ariadne
  # :nodoc:
  module IconHelper
    include FetchOrFallbackHelper

    def check_icon_presence!(icon, variant)
      return true unless has_partial_icon?(icon, variant)

      icon_presence!(icon, variant)
      variant_presence!(icon, variant)
      fetch_or_raise(HeroiconsHelper::Icon::VALID_VARIANTS, variant)

      true
    end

    def has_partial_icon?(icon, variant)
      icon.present? || variant.present?
    end

    def icon_presence!(icon, variant)
      raise(ArgumentError, "You must provide an `icon` when providing a `variant`.") if icon.blank? && variant.present?

      true
    end

    def variant_presence!(icon, variant)
      raise(ArgumentError, "You must provide a `variant` when providing an `icon`.") if icon.present? && variant.blank?

      true
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
ariadne_view_components-0.0.12 app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.12-x86_64-linux app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.12-x86_64-darwin app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.12-x64-mingw32 app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.12-x64-mingw-ucrt app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.12-arm64-darwin app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.12-aarch64-linux app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.11 app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.11-x86_64-linux app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.11-x86_64-darwin app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.11-x64-mingw32 app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.11-x64-mingw-ucrt app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.11-arm64-darwin app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.11-aarch64-linux app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.10 app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.10-x86_64-linux app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.10-x86_64-darwin app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.10-x64-mingw32 app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.10-x64-mingw-ucrt app/lib/ariadne/icon_helper.rb
ariadne_view_components-0.0.10-arm64-darwin app/lib/ariadne/icon_helper.rb