class Redactor

Constants

DEFAULT_REPLACEMENT
VERSION

Attributes

default_replacement[RW]
rules[RW]

Public Class Methods

new(rules = [], &block) click to toggle source
# File lib/redactor.rb, line 6
def initialize(rules = [], &block)
  @default_replacement = DEFAULT_REPLACEMENT
  @rules = Array(rules)

  DSL.run(self, block) if block_given?
end

Public Instance Methods

extract(from) click to toggle source

returns a list of Extract objects (including position and value) matching predefined rules

# File lib/redactor.rb, line 19
def extract(from)
  rules.reduce([]) do |extracts, rule|
    new_extracts = rule.extract(from).select do |extract|
      # do not consider new extract if colliding with existing one
      !extracts.any? { |e| e.collides?(extract) }
    end
    extracts.concat(new_extracts)
  end
end
format(text, &block) click to toggle source

replaces parts of text by [REDACTED]. The replacement string can be customized by passing a block taking the Extract object as an argument

# File lib/redactor.rb, line 31
def format(text, &block)
  extract(text).each_with_object(text.clone) do |extract, redacted_text|
    sub = block_given? ? block.call(extract) : default_replacement
    redacted_text[extract.start..extract.finish] = sub
  end
end
register_rule(rule) click to toggle source
# File lib/redactor.rb, line 13
def register_rule(rule)
  rules.push(rule)
end