Sha256: cfc0d384c3b3af52b34761b09a4ab813f5efcd082e5ada0b3defab2b01acbeca

Contents?: true

Size: 1.57 KB

Versions: 32

Compression:

Stored size: 1.57 KB

Contents

module Lookbook
  class TagOptionsParser < Service
    RESOLVERS = [
      FileResolver,
      MethodResolver,
      EvalResolver,
      YamlResolver
    ]

    def initialize(input, resolver_opts = {})
      @input = input.to_s.strip
      @resolver_opts = resolver_opts
      @fallback = resolver_opts.fetch(:fallback, {})
    end

    def call
      options_string, remaining_text = parse_input(@input)
      resolved_options = resolver(options_string).call(options_string, **@resolver_opts)
      options = prepare_options(resolved_options)
      [options, remaining_text]
    end

    protected

    def resolver(options_string)
      if options_string.present?
        handler = RESOLVERS.find { |r| r.resolveable?(options_string) }
        if handler.nil?
          Lookbook.logger.error "Invalid tag options string '#{options_string}'"
          method(:fallback_resolver)
        else
          handler
        end
      else
        method(:fallback_resolver)
      end
    end

    def fallback_resolver(*args)
      @fallback
    end

    def parse_input(input)
      matchers.each_with_object(["", input]) do |matcher, result|
        input.match(matcher) do |match_data|
          result[0] = match_data[1] # options string
          result[1] = input.gsub(matcher, "").strip # any remaining text
          return result
        end
      end
    end

    def matchers
      RESOLVERS.map { |r| r::MATCHER }
    end

    def prepare_options(options)
      options = options.is_a?(Array) ? {choices: options} : options
      options.is_a?(Hash) ? Store.new(options) : options
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
lookbook-2.0.2 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.1 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.0 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.0.rc.3 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-1.5.5 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-1.5.4 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.0.rc.2 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.0.rc.1 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.0.beta.9 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.0.beta.8 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.0.beta.7 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.0.beta.6 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.0.beta.5 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.0.beta.4 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.0.beta.3 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.0.beta.2 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-1.5.3 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.0.beta.1 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-1.5.2 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.0.beta.0 lib/lookbook/services/tags/tag_options_parser.rb