]> cat aescling's git repositories - mastodon.git/commitdiff
Switch to Regexp.union for building the mute expression.
authorDavid Yip <yipdw@member.fsf.org>
Tue, 24 Oct 2017 23:31:34 +0000 (18:31 -0500)
committerDavid Yip <yipdw@member.fsf.org>
Tue, 24 Oct 2017 23:31:34 +0000 (18:31 -0500)
Also make the keyword-building methods private: they always probably
should have been private, but now I have encoded enough fun and games
into them that it now seems wrong for them to *not* be private.

app/models/glitch/keyword_mute.rb

index 4c3e69de49bd99d601b194a1b1421b8a4de408df..f0969c65e617ddd500606847e8411092b7965f14 100644 (file)
@@ -35,30 +35,32 @@ class Glitch::KeywordMute < ApplicationRecord
     def initialize(account_id)
       @account_id = account_id
       regex_text = Rails.cache.fetch("keyword_mutes:regex:#{account_id}") { regex_text_for_account }
-      @regex = /#{regex_text}/i unless regex_text.empty?
+      @regex = /#{regex_text}/i
     end
 
+    def =~(str)
+      regex ? regex =~ str : nil
+    end
+
+    private
+
     def keywords
       Glitch::KeywordMute.where(account_id: account_id).select(:keyword, :id, :whole_word)
     end
 
     def regex_text_for_account
-      [].tap do |arr|
-        keywords.find_each do |kw|
-          arr << (kw.whole_word ? boundary_regex_for_keyword(kw.keyword) : Regexp.escape(kw.keyword))
-        end
-      end.join('|')
+      kws = keywords.find_each.with_object([]) do |kw, a|
+        a << (kw.whole_word ? boundary_regex_for_keyword(kw.keyword) : kw.keyword)
+      end
+
+      Regexp.union(kws).source
     end
 
     def boundary_regex_for_keyword(keyword)
       sb = keyword =~ /\A[[:word:]]/ ? '\b' : ''
       eb = keyword =~ /[[:word:]]\Z/ ? '\b' : ''
 
-      "#{sb}#{Regexp.escape(keyword)}#{eb}"
-    end
-
-    def =~(str)
-      regex ? regex =~ str : nil
+      /#{sb}#{Regexp.escape(keyword)}#{eb}/
     end
   end
 end
This page took 0.023134 seconds and 3 git commands to generate.