Sha256: 8a1d31dc9a7d61b0d903a4adffd668268d49e447174792b2b050a37c07ebab9a

Contents?: true

Size: 1.76 KB

Versions: 12

Compression:

Stored size: 1.76 KB

Contents

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

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

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

    protected

    def resolve?
      !!@resolve
    end

    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

12 entries across 12 versions & 1 rubygems

Version Path
lookbook-2.3.4 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.3.3 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.3.2 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.3.0 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.2.2 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.2.1 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.2.0 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.1.1 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.1.0 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.5 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.4 lib/lookbook/services/tags/tag_options_parser.rb
lookbook-2.0.3 lib/lookbook/services/tags/tag_options_parser.rb