Sha256: 8457ea9f30ccb6fc98b7d3368cd0ab725218b1e7d0c24f5414d193f7d0a1fa6e

Contents?: true

Size: 1.02 KB

Versions: 5

Compression:

Stored size: 1.02 KB

Contents

module Coconductor
  module Matchers
    class FieldAware < Licensee::Matchers::Exact
      include Coconductor::Matchers::Matcher

      def match
        return @match if defined? @match

        potential_matches.find do |code_of_conduct|
          file.content_normalized =~ regex_for(code_of_conduct)
        end
      end

      def confidence
        100
      end

      private

      FIELD_PLACEHOLDER = 'COCONDUCTOR_FIELD_COCONDUCTOR'.freeze
      FIELD_PLACEHOLDER_REGEX = /coconductor\\ field\\ coconductor/

      def regex_for(code_of_conduct)
        coc = code_of_conduct.dup
        coc.instance_variable_set '@content_normalized', nil
        coc.instance_variable_set '@content_without_title_and_version', nil
        field_regex = /#{Regexp.union(coc.fields.map(&:raw_text))}/i
        coc.content = coc.content.gsub(field_regex, FIELD_PLACEHOLDER)
        regex = Regexp.escape(coc.content_normalized)
        regex = regex.gsub(FIELD_PLACEHOLDER_REGEX, '([a-z ]+?)')
        Regexp.new(regex)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
coconductor-0.9.2 lib/coconductor/matchers/field_aware.rb
coconductor-0.9.1 lib/coconductor/matchers/field_aware.rb
coconductor-0.9.0 lib/coconductor/matchers/field_aware.rb
coconductor-0.8.3 lib/coconductor/matchers/field_aware.rb
coconductor-0.8.2 lib/coconductor/matchers/field_aware.rb