Sha256: fb1899d4f7dcbbe1ad684a29c317df643a54162111616b4c0fe3714b9e06bdd3
Contents?: true
Size: 1.59 KB
Versions: 25
Compression:
Stored size: 1.59 KB
Contents
namespace :censor_bear do desc 'Format words' task format: :environment do from_file = File.read(CensorBear.config.format_from_file_path) File.open(CensorBear.config.format_dest_file_path, 'w') do |f| from_file.each_line do |word| line = "#{word.strip}={BANNED}|{BANNED}|{BANNED}|{BANNED}\r\n" f.write(line) f.flush end end end desc 'Import words' task import: :environment do is_overwrite = true file = File.read(CensorBear.config.import_file_path) # lines = doc.split(/\n/) file.each_line do |line| line.strip! key, rule_str = line.split('=') rules = rule_str.split('|') ugc = parse(rules[0]).blank? ? 'replace' : parse(rules[0]) username = parse(rules[1]) || 'ignore' signature = parse(rules[2]) || 'ignore' dialog = parse(rules[3]) || 'ignore' nickname = parse(rules[1]) || 'ignore' replacement = ugc == 'replace' ? rules[0] : '**' payload = { key: key, ugc: ugc, username: username, signature: signature, dialog: dialog, nickname: nickname, replacement: replacement, user_id: CensorBear.config.user_class.constantize.last&.id, created_at: Time.now, updated_at: Time.now } word = CensorBear::StopWord.find_by(key: key) if word word.update_columns(payload) if is_overwrite else CensorBear::StopWord.create(payload) end end end def parse(rule) return nil if rule.blank? r = rule.match(/^{(\w+)}$/) r.blank? ? nil : r[1].downcase end end
Version data entries
25 entries across 25 versions & 1 rubygems