Sha256: 9fca893f49835cd0e21cf0782355cf2c3c30acd67e5394918e243ee017f354df

Contents?: true

Size: 1.67 KB

Versions: 18

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module Primer
  # Base class for responsive Stack and StackItem arguments. Used internally.
  class ResponsiveArg
    BREAKPOINTS = [:narrow, :regular, :wide].freeze

    include FetchOrFallbackHelper

    class << self
      def for(values)
        cache[[values, arg_name].hash] ||= new(values)
      end

      # :nocov:
      def arg_name
        raise NotImplementedError, "Subclasses must implement the `#{__method__}' method"
      end
      # :nocov:

      private

      def cache
        Thread.current[:pvc_stack_cache] ||= {}
      end
    end

    def to_data_attributes
      @data_attributes ||= data_attributes_for(self.class.arg_name, values)
    end

    private

    def data_attributes_for(property, values)
      if values.is_a?(Hash)
        values.slice(*BREAKPOINTS).each_with_object({}) do |(key, value), memo|
          next unless value
          property_with_breakpoint = "#{property}-#{key}"
          memo[property_with_breakpoint] = value
        end
      else
        values ? { property => values } : {}
      end
    end

    def fetch_or_fallback_all(options, values, default)
      if values.is_a?(Hash)
        values.each_with_object({}) do |(key, value), memo|
          memo[key] = fetch_or_fallback(options, value, default).yield_self do |value|
            block_given? ? yield(value) : value
          end
        end
      else
        fetch_or_fallback(options, values, default).yield_self do |value|
          block_given? ? yield(value) : value
        end
      end
    end

    # :nocov:
    def values
      raise NotImplementedError, "Subclasses must implement the `#{__method__}' method"
    end
    # :nocov:
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
primer_view_components-0.36.5 app/components/primer/responsive_arg.rb
openproject-primer_view_components-0.52.2 app/components/primer/responsive_arg.rb
primer_view_components-0.36.4 app/components/primer/responsive_arg.rb
openproject-primer_view_components-0.52.1 app/components/primer/responsive_arg.rb
openproject-primer_view_components-0.52.0 app/components/primer/responsive_arg.rb
primer_view_components-0.36.3 app/components/primer/responsive_arg.rb
openproject-primer_view_components-0.51.0 app/components/primer/responsive_arg.rb
primer_view_components-0.36.2 app/components/primer/responsive_arg.rb
openproject-primer_view_components-0.50.1 app/components/primer/responsive_arg.rb
openproject-primer_view_components-0.50.0 app/components/primer/responsive_arg.rb
primer_view_components-0.36.1 app/components/primer/responsive_arg.rb
primer_view_components-0.36.0 app/components/primer/responsive_arg.rb
openproject-primer_view_components-0.49.2 app/components/primer/responsive_arg.rb
primer_view_components-0.35.2 app/components/primer/responsive_arg.rb
openproject-primer_view_components-0.49.1 app/components/primer/responsive_arg.rb
openproject-primer_view_components-0.49.0 app/components/primer/responsive_arg.rb
primer_view_components-0.35.1 app/components/primer/responsive_arg.rb
primer_view_components-0.35.0 app/components/primer/responsive_arg.rb