Sha256: 5b6531100d8a17c4d61bb5fafdec62f07149fc3d61ceb482b38f1788bd7592a3

Contents?: true

Size: 1.11 KB

Versions: 5

Compression:

Stored size: 1.11 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]

        array(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

5 entries across 5 versions & 1 rubygems

Version Path
ruby-dagger-0.4.0 lib/dagger/generate/regexp.rb
ruby-dagger-0.3.3 lib/dagger/generate/regexp.rb
ruby-dagger-0.3.2 lib/dagger/generate/regexp.rb
ruby-dagger-0.3.1 lib/dagger/generate/regexp.rb
ruby-dagger-0.3.0 lib/dagger/generate/regexp.rb