Sha256: 98843ca1caec93fcd80bca2a042c5336a7a1e0b42cd5fd1f4a24f55f7ab12ac9

Contents?: true

Size: 682 Bytes

Versions: 46

Compression:

Stored size: 682 Bytes

Contents

module Lookbook
  class KeyValueTagParser < Service
    KEY_VALUE_REGEX = /^([^\s]+)\s+(.+)$/

    attr_reader :text

    def initialize(text)
      @text = text.to_s
    end

    def call
      text.strip.match(KEY_VALUE_REGEX) do |matches|
        key = matches[1]
        value = begin
          YAML.safe_load(matches[2] || "~")
        rescue ::Psych::SyntaxError => exception
          raise ParserError.new("Invalid YAML in tag text '#{@text}'", scope: "key_value_tag.parser", original: exception)
        end
        return [key, value]
      end
      raise ParserError.new("Could not parse key:value pair from '#{@text}'", scope: "key_value_tag.parser")
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
lookbook-2.3.4 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.3.3 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.3.2 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.3.0 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.2.2 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.2.1 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.2.0 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.1.1 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.1.0 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.5 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.4 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.3 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.2 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.1 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.0 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.0.rc.3 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-1.5.5 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-1.5.4 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.0.rc.2 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.0.rc.1 lib/lookbook/services/tags/key_value_tag_parser.rb