Sha256: 0b83dd2d65c1f902a31444846a34f2688d9696be138a627579bbc85b1ac2eecb

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

module BloodContracts
  module Contracts
    class Matcher
      extend Dry::Initializer

      param :contract_hash, ->(v) { Hashie::Mash.new(v) }

      def call(input, output, meta = Hash.new, storage:)
        options = Hashie::Mash.new(input: input, output: output, meta: meta)

        rule_names = select_matched_rules!(options).keys
        rule_names = [Storage::UNDEFINED_RULE] if rule_names.empty?
        Array(rule_names).each(&storage.method(:store))

        yield rule_names, options if block_given?

        !storage.found_unexpected_behavior?
      end

      private

      def with_rule_options(rule_name, options)
        rule_options = options.shallow_merge(meta: {})
        result = yield(rule_options)
        options.meta.merge!(rule_name.to_sym => rule_options.meta)
        result
      end

      def select_matched_rules!(options)
        contract_hash.select do |name, rule|
          with_rule_options(name, options) do |rule_options|
            rule.check.call(rule_options)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
blood_contracts-0.2.1 lib/blood_contracts/contracts/matcher.rb
blood_contracts-0.2.0 lib/blood_contracts/contracts/matcher.rb