Sha256: 826044cf768f2a18f2f7e19d81d373e708a09231d297aff85389db1c5852dfeb

Contents?: true

Size: 1.01 KB

Versions: 4

Compression:

Stored size: 1.01 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)}\]?/
        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

4 entries across 4 versions & 1 rubygems

Version Path
coconductor-0.4.0 lib/coconductor/matchers/field_aware.rb
coconductor-0.3.1 lib/coconductor/matchers/field_aware.rb
coconductor-0.3.0 lib/coconductor/matchers/field_aware.rb
coconductor-0.2.0 lib/coconductor/matchers/field_aware.rb