Class: Errata::Erratum

Inherits:
Object
  • Object
show all
Defined in:
lib/erratum.rb,
lib/erratum/delete.rb,
lib/erratum/reject.rb,
lib/erratum/replace.rb,
lib/erratum/simplify.rb,
lib/erratum/truncate.rb,
lib/erratum/transform.rb

Direct Known Subclasses

Delete, Reject, Replace, Simplify, Transform, Truncate

Defined Under Namespace

Classes: Delete, Reject, Replace, Simplify, Transform, Truncate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errata, options = {}) ⇒ Erratum

Returns a new instance of Erratum.



6
7
8
9
10
11
# File 'lib/erratum.rb', line 6

def initialize(errata, options = {})
  raise "you can't set this from outside" if options[:prefix].present?
  @errata = errata
  @column = options[:section]
  @options = options
end

Instance Attribute Details

#columnObject

Returns the value of attribute column.



3
4
5
# File 'lib/erratum.rb', line 3

def column
  @column
end

#errataObject

Returns the value of attribute errata.



3
4
5
# File 'lib/erratum.rb', line 3

def errata
  @errata
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/erratum.rb', line 3

def options
  @options
end

Instance Method Details

#conditions_match?(row) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/erratum.rb', line 54

def conditions_match?(row)
  matching_methods.all? { |method_id| responder.send method_id, row }
end

#correct!(row, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/erratum.rb', line 27

def correct!(row, &block)
  return :skipped unless targets?(row)
  # old_value = row[column].to_s.dup
  yield if block_given?
  # unless name.demodulize.underscore == 'truncate' or name.demodulize.underscore == 'simplify'
  #   puts "-" * 64
  #   puts inspect
  #   puts row.inspect
  #   if row[column] != old_value
  #     puts "#{old_value} -> #{row[column]}"
  #   else
  #     puts "no change"
  #   end
  #   puts
  # end
  :corrected
end

#expression_matches?(row) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
# File 'lib/erratum.rb', line 45

def expression_matches?(row)
  return true if matching_expression.blank? or column.blank?
  if matching_expression.is_a?(Regexp)
    matching_expression.match(row[column].to_s)
  else
    row[column].to_s.include?(matching_expression)
  end
end

#inspectObject



19
20
21
# File 'lib/erratum.rb', line 19

def inspect
  "<#{self.class.name}:#{object_id} responder=#{responder.to_s} column=#{column} matching_methods=#{matching_methods.inspect}"
end

#matching_methodsObject



13
14
15
16
17
# File 'lib/erratum.rb', line 13

def matching_methods
  @_matching_methods ||= options[:condition].split(/\s*;\s*/).map do |method_id|
    "#{method_id.strip.gsub(/[^a-z0-9]/i, '_').downcase}?"
  end
end

#set_matching_expression(options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/erratum.rb', line 58

def set_matching_expression(options = {})
  if options[:x].blank?
    @matching_expression = nil
  elsif options[:x].start_with?('/')
    if options[:x].end_with?('i')
      ci = true
      options[:x] = options[:x].chop
    else
      ci = false
    end
    @matching_expression = Regexp.new(options[:x].gsub(/\A\/|\/\z/, ''), ci)
  elsif /\Aabbr\((.*)\)\z/.match(options[:x])
    @matching_expression = Regexp.new('(\A|\s)' + $1.split(/(\w\??)/).reject { |a| a == '' }.join('\.?\s?') + '\.?([^\w\.]|\z)', true)
  elsif options[:prefix] == true
    @matching_expression = Regexp.new('\A\s*' + Regexp.escape(options[:x]), true)
  else
    @matching_expression = options[:x]
  end
end

#targets?(row) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/erratum.rb', line 23

def targets?(row)
  !!(conditions_match?(row) and expression_matches?(row))
end