Sha256: 0fbe9ca2cd4bed0861a960760d4eab3913867c286303e183cefaa31e93411601

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require_relative '../generator'

module Dagger
  module Generate
    # Generate a value by collecting regexp matches for keys,
    # and filling format strings.
    #
    # _default.key:
    #   - regexp:
    #       srckey:
    #         - regexp
    #         - ...
    #       ...
    #     string:
    #       - format string
    #       - ...
    class Regexp < Dagger::Generator
      def process(sources)
        matches = {}
        sources.each do |key, regexps|
          matches.merge!(match_regexps(key, regexps))
        end
        update(dictionary: matches)
      end

      private

      # Match the value of a key agains regexps, returning the named
      # captured data.
      #
      # :call-seq:
      #   match_regexps(key, regexps) => Hash
      def match_regexps(key, regexps)
        string = dictionary[key]

        enumerable(regexps).each_with_object({}) do |regexp, matches|
          matchdata = ::Regexp.new(regexp).match(string)
          next if matchdata.nil?
          matches.merge!(matchdata.named_captures.transform_keys(&:to_sym))
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-dagger-0.2.0 lib/dagger/generator/regexp.rb