Sha256: 4e600017abcffa042e4f955ecc285fad321e3abc0560c2f40ad780e4adcebe42

Contents?: true

Size: 1.95 KB

Versions: 16

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module UiBibz::Utils
  # Generate the col class name
  class BreakdownClassNameGenerator
    POSITIONING = %i[num offset push pull order].freeze
    BREAKPOINTS = UiBibz::Ui::Core::Component::BREAKPOINTS
    PARAMETERS =  BREAKPOINTS + POSITIONING + [:position]

    def initialize(options = {}, klass_name = 'col')
      @options = options.is_a?(Integer) ? { num: options } : options
      @klass_name = klass_name
    end

    # Possible options
    # 3 or md: 3 or md: { num: 3 }, xs: { num: 4 }
    def class_names
      return @klass_name unless col_options?

      kl = []
      @options.each do |key, value|
        kl << write_classes(key.to_sym, value) if BREAKPOINTS.include?(key.to_sym)
      end
      kl << write_classes(nil, @options)

      kl.delete_if(&:blank?)
    end

    private

    def col_options?
      (@options.keys.map(&:to_sym) & PARAMETERS).present?
    end

    # md: 8 or md: { num: 3}, xs: { num: 4 }
    def write_classes(size, opts)
      if opts.is_a?(Hash)
        @position = opts[:position]

        opts.filter_map do |k, v|
          send(k, size, v) if POSITIONING.include?(k.to_sym)
        end.join(' ')
      else
        send('num', size, opts)
      end
    end

    # col-md-9
    def num(size, number, _pos = nil)
      size == :auto ? @klass_name : ["#{@klass_name}#{position}", size, number].compact.join('-')
    end

    # col-md-offset-9
    def offset(size, number)
      ['offset', size, number].compact.join('-')
    end

    # col-md-push-9
    def push(size, number)
      [@klass_name, size, 'push', number].compact.join('-')
    end

    # order-md-9
    def order(size, number)
      ['order', size, number].compact.join('-')
    end

    # col-md-pull-9
    def pull(size, number)
      [@klass_name, size, 'pull', number].compact.join('-')
    end

    def position
      case @position || @options[:position]
      when :vertical
        'y'
      when :horizontal
        'x'
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ui_bibz-3.0.13 lib/ui_bibz/utils/breakdown_class_name_generator.rb
ui_bibz-3.0.12 lib/ui_bibz/utils/breakdown_class_name_generator.rb
ui_bibz-3.0.11 lib/ui_bibz/utils/breakdown_class_name_generator.rb
ui_bibz-3.0.10 lib/ui_bibz/utils/breakdown_class_name_generator.rb
ui_bibz-3.0.9 lib/ui_bibz/utils/breakdown_class_name_generator.rb
ui_bibz-3.0.8 lib/ui_bibz/utils/breakdown_class_name_generator.rb
ui_bibz-3.0.7 lib/ui_bibz/utils/breakdown_class_name_generator.rb
ui_bibz-3.0.6 lib/ui_bibz/utils/breakdown_class_name_generator.rb
ui_bibz-3.0.5 lib/ui_bibz/utils/breakdown_class_name_generator.rb
ui_bibz-3.0.4 lib/ui_bibz/utils/breakdown_class_name_generator.rb
ui_bibz-3.0.3 lib/ui_bibz/utils/breakdown_class_name_generator.rb
ui_bibz-3.0.2 lib/ui_bibz/utils/breakdown_class_name_generator.rb
ui_bibz-3.0.1 lib/ui_bibz/utils/breakdown_class_name_generator.rb
ui_bibz-3.0.0 lib/ui_bibz/utils/breakdown_class_name_generator.rb
ui_bibz-3.0.0.beta19 lib/ui_bibz/utils/breakdown_class_name_generator.rb
ui_bibz-3.0.0.beta18 lib/ui_bibz/utils/breakdown_class_name_generator.rb