Sha256: 07c287e3529d9fff1586258f052de478f3749618726d74cbb631e78a1f39403a

Contents?: true

Size: 1.42 KB

Versions: 33

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Theme
  module Conversions
    class BaseGlob
      class << self
        def register(parser)
          parser.accept(self) { |_val| convert(parser) }
        end

        def convert(parser)
          argv = parser.default_argv
          values = []

          option_indexes(argv).each do |option_index|
            values += option_values(argv, parser, option_index)
          end

          values
        end

        def options
          raise "`#{self.class.name}#options` must be defined"
        end

        private

        def option_values(argv, parser, option_index)
          return [] if option_index.nil?

          start_index = option_index + 1
          option_by_key = options_map(parser)
          values = []

          argv[start_index..-1].each do |value|
            return values unless option_by_key[value].nil?
            values << value
          end

          values
        end

        def option_indexes(argv)
          argv
            .each_with_index
            .select { |item, _index| options.include?(item) }
            .map(&:last)
        end

        def options_map(parser)
          map = {}
          parser.top.list.each do |option|
            map[option.short.first] = option
            map[option.long.first] = option
          end
          map
        end

        def parameter?(value)
          value.start_with?("-")
        end
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
shopify-cli-2.36.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.35.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.34.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.33.1 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.33.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.32.1 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.32.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.31.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.30.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.29.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.28.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.27.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.26.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.25.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.24.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.23.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.22.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.21.0 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.20.1 lib/project_types/theme/conversions/base_glob.rb
shopify-cli-2.20.0 lib/project_types/theme/conversions/base_glob.rb