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

Constant Summary collapse

SEMICOLON_DELIMITER =
/\s*;\s*/
SPECIAL_ABBR =
/\Aabbr\((.*)\)\z/
REJECT_ACTIONS =
%w{reject truncate}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Erratum.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/errata/erratum.rb', line 21

def initialize(responder, options = {})
  @responder = responder
  @section = options[:section]
  @matching_methods = options[:condition].split(SEMICOLON_DELIMITER).map do |method_id|
    method_id.strip.gsub(/\W/, '_').downcase + '?'
  end
  if @matching_methods.any? and @responder.nil?
    raise ::ArgumentError, %{[errata] Conditions like #{@matching_methods.first.inspect} used, but no :responder defined}
  end
  @matching_expression = if options[:x].blank?
    nil
  elsif (options[:x].start_with?('/') or options[:x].start_with?('%r{')) and as_regexp = options[:x].as_regexp
    ::Regexp.new(*as_regexp)
  elsif SPECIAL_ABBR.match options[:x]
    @abbr_query = true
    abbr = $1.split(/(\w\??)/).reject { |a| a == '' }.join('\.?\s?') + '\.?([^\w\.]|\z)'
    expr = '(\A|\s)' + abbr
    ::Regexp.new expr, true
  elsif REJECT_ACTIONS.include? options[:action]
    expr = '\A\s*' + ::Regexp.escape(options[:x])
    ::Regexp.new expr, true
  else
    options[:x]
  end
end

Instance Attribute Details

#matching_expressionObject (readonly)

Returns the value of attribute matching_expression.



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

def matching_expression
  @matching_expression
end

#matching_methodsObject (readonly)

Returns the value of attribute matching_methods.



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

def matching_methods
  @matching_methods
end

#responderObject (readonly)

Returns the value of attribute responder.



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

def responder
  @responder
end

#sectionObject (readonly)

Returns the value of attribute section.



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

def section
  @section
end

Instance Method Details

#abbr?Boolean

Returns:

  • (Boolean)


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

def abbr?
  @abbr_query == true
end

#conditions_match?(row) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/errata/erratum.rb', line 65

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

#expression_matches?(row) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
# File 'lib/errata/erratum.rb', line 55

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

#targets?(row) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/errata/erratum.rb', line 51

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