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.0.0.beta.9 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.0.beta.8 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.0.beta.7 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.0.beta.6 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.0.beta.5 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.0.beta.4 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.0.beta.3 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.0.beta.2 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-1.5.3 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.0.beta.1 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-1.5.2 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-2.0.0.beta.0 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-1.5.1 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-1.5.0 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-1.4.5 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-1.4.4 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-1.4.3 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-1.4.2 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-1.4.1 lib/lookbook/services/tags/key_value_tag_parser.rb
lookbook-1.4.0 lib/lookbook/services/tags/key_value_tag_parser.rb