Class: Errata::Erratum

Inherits:
Object
  • Object
show all
Defined in:
lib/errata/erratum.rb,
lib/errata/erratum/delete.rb,
lib/errata/erratum/reject.rb,
lib/errata/erratum/replace.rb,
lib/errata/erratum/simplify.rb,
lib/errata/erratum/truncate.rb,
lib/errata/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.


15
16
17
18
# File 'lib/errata/erratum.rb', line 15

def initialize(errata, options = {})
  @errata = errata
  @options = options.dup
end

Instance Attribute Details

#errataObject (readonly)

Returns the value of attribute errata.


12
13
14
# File 'lib/errata/erratum.rb', line 12

def errata
  @errata
end

#optionsObject (readonly)

Returns the value of attribute options.


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

def options
  @options
end

Instance Method Details

#conditions_match?(row) ⇒ Boolean

Returns:

  • (Boolean)

47
48
49
# File 'lib/errata/erratum.rb', line 47

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

#expression_matches?(row) ⇒ Boolean

Returns:

  • (Boolean)

38
39
40
41
42
43
44
45
# File 'lib/errata/erratum.rb', line 38

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

#matching_expressionObject


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/errata/erratum.rb', line 51

def matching_expression
  return @matching_expression[0] if @matching_expression.is_a? ::Array
  @matching_expression = []
  @matching_expression[0] = if options['x'].blank?
    nil
  elsif (options['x'].start_with?('/') and options['x'].end_with?('/')) or options['x'].start_with?('%r{')
    if as_regexp = options['x'].as_regexp
      ::Regexp.new(*as_regexp)
    end
  elsif /\Aabbr\((.*)\)\z/.match options['x']
    abbr = $1.split(/(\w\??)/).reject { |a| a == '' }.join('\.?\s?') + '\.?([^\w\.]|\z)'
    expr = '(\A|\s)' + abbr
    ::Regexp.new expr, true
  elsif %w{reject truncate}.include? options['action']
    expr = '\A\s*' + ::Regexp.escape(options['x'])
    ::Regexp.new expr, true
  else
    options['x']
  end
  @matching_expression[0]
end

#matching_methodsObject


28
29
30
31
32
# File 'lib/errata/erratum.rb', line 28

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

#responderObject


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

def responder
  errata.responder
end

#sectionObject


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

def section
  options['section']
end

#targets?(row) ⇒ Boolean

Returns:

  • (Boolean)

34
35
36
# File 'lib/errata/erratum.rb', line 34

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