Sha256: a0e3df4f9b2e1fb239ab884cd5ae1c13e04ef2ae64beed65b883d6aa03eab092

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

module Pact
  module MatchingRules
    class Extract

      def self.call matchable
        new(matchable).call
      end

      def initialize matchable
        @matchable = matchable
        @rules = Hash.new
      end

      def call
        recurse matchable, "$", nil
        rules
      end

      private

      attr_reader :matchable, :rules

      def recurse object, path, match_type

        case object
        when Hash then recurse_hash(object, path, match_type)
        when Array then recurse_array(object, path, match_type)
        when Pact::SomethingLike then recurse_something_like(object, path, match_type)
        when Pact::Term then record_regex_rule object, path
        else
          record_rule object, path, match_type
        end

      end

      def recurse_hash hash, path, match_type
        hash.each do | (key, value) |
          recurse value, "#{path}.#{key.to_s}", match_type
        end
      end

      def recurse_array new_array, path, match_type
        new_array.each_with_index do | value, index |
          recurse value, "#{path}[#{index}]", match_type
        end
      end

      def recurse_something_like something_like, path, match_type
        recurse something_like.contents, path, "type"
      end

      def record_regex_rule term, path
        rules[path] ||= {}
        rules[path]['match'] = 'regex'
        rules[path]['regex'] = term.matcher.inspect[1..-2]
      end

      def record_rule object, path, match_type
        rules[path] ||= {}
        rules[path]['match'] = 'type'
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pact-support-0.4.0 lib/pact/matching_rules/extract.rb
pact-support-0.3.1 lib/pact/matching_rules/extract.rb
pact-support-0.3.0 lib/pact/matching_rules/extract.rb