Class: Errata::Erratum
- Inherits:
-
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
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
#column ⇒ Object
Returns the value of attribute column.
3
4
5
|
# File 'lib/erratum.rb', line 3
def column
@column
end
|
#errata ⇒ Object
Returns the value of attribute errata.
3
4
5
|
# File 'lib/erratum.rb', line 3
def errata
@errata
end
|
#options ⇒ Object
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
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)
yield if block_given?
:corrected
end
|
#expression_matches?(row) ⇒ 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
|
#inspect ⇒ Object
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_methods ⇒ Object
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
23
24
25
|
# File 'lib/erratum.rb', line 23
def targets?(row)
!!(conditions_match?(row) and expression_matches?(row))
end
|