Sha256: 9af500fd4276eea7f081ad0378af829fbbc16392b5b537306727fc707642f68b

Contents?: true

Size: 1.23 KB

Versions: 30

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require "json"

module Ariadne
  module ViewComponents
    # A module for constants that are used in the view components.
    class Constants
      CONSTANTS = JSON.parse(
        File.read(
          File.join(File.dirname(__FILE__), "../../../static/constants.json")
        )
      ).freeze

      class << self
        def get(component:, constant:, invert: true, symbolize: false)
          values = CONSTANTS.dig(component, constant)

          case values
          when Hash
            format_hash(values, invert, symbolize)
          when Array
            format_array(values, symbolize)
          else
            values
          end
        end

        private def format_hash(values, invert, symbolize)
          val = invert ? values.invert : values
          # remove defaults
          val = val.except("", nil)

          return val.transform_values { |v| symbolize_value(v) } if symbolize

          val
        end

        private def format_array(values, symbolize)
          val = values.select(&:present?)

          return val.map { |v| symbolize_value(v) } if symbolize

          val
        end

        private def symbolize_value(value)
          ":#{value}"
        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

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