Sha256: f8f737f801516e9b8e965315296833b90ef252b6b6bbf203add36fc08bded6cc

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require 'ncs_navigator/mdes'

module NcsNavigator::Mdes::Differences
  ##
  # @private implementation detail
  class ValueCriterion
    COMPARATORS = {
      :equality => lambda { |a, b| a == b },
      :predicate => lambda { |a, b| !(a ^ b) }
    }

    VALUE_EXTRACTORS = {
      :identity => lambda { |o| o },
      :word_chars_downcase =>
        lambda { |o| o ? o.downcase.gsub(/[^ \w]+/, ' ').gsub(/\s+/, ' ').strip : o }
    }

    attr_reader :comparator, :value_extractor

    def initialize(options={})
      @comparator = select_comparator(options.delete(:comparator))
      @value_extractor = select_value_extractor(options.delete(:value_extractor))
    end

    def apply(v1, v2, diff_options)
      cv1 = value_extractor.call(v1)
      cv2 = value_extractor.call(v2)

      unless comparator.call(cv1, cv2)
        Value.new(cv1, cv2)
      end
    end

    def select_comparator(param)
      case param
      when nil
        COMPARATORS[:equality]
      when Symbol
        COMPARATORS[param] or fail "Unknown comparator #{param.inspect}"
      else
        param
      end
    end
    private :select_comparator

    def select_value_extractor(param)
      case param
      when nil
        VALUE_EXTRACTORS[:identity]
      when Symbol
        VALUE_EXTRACTORS[param] or fail "Unknown extractor #{param.inspect}"
      else
        param
      end
    end
    private :select_value_extractor
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ncs_mdes-0.13.0 lib/ncs_navigator/mdes/differences/value_criterion.rb
ncs_mdes-0.12.0 lib/ncs_navigator/mdes/differences/value_criterion.rb