Class: Errata::Erratum
- Inherits:
-
Object
- Object
- Errata::Erratum
- 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
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
-
#matching_expression ⇒ Object
readonly
Returns the value of attribute matching_expression.
-
#matching_methods ⇒ Object
readonly
Returns the value of attribute matching_methods.
-
#responder ⇒ Object
readonly
Returns the value of attribute responder.
-
#section ⇒ Object
readonly
Returns the value of attribute section.
Instance Method Summary collapse
- #abbr? ⇒ Boolean
- #conditions_match?(row) ⇒ Boolean
- #expression_matches?(row) ⇒ Boolean
-
#initialize(responder, options = {}) ⇒ Erratum
constructor
A new instance of Erratum.
- #targets?(row) ⇒ Boolean
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, = {}) @responder = responder @section = [:section] @matching_methods = [: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 [:x].blank? nil elsif ([:x].start_with?('/') or [:x].start_with?('%r{')) and as_regexp = [:x].as_regexp ::Regexp.new(*as_regexp) elsif SPECIAL_ABBR.match [: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? [:action] expr = '\A\s*' + ::Regexp.escape([:x]) ::Regexp.new expr, true else [:x] end end |
Instance Attribute Details
#matching_expression ⇒ Object (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_methods ⇒ Object (readonly)
Returns the value of attribute matching_methods.
18 19 20 |
# File 'lib/errata/erratum.rb', line 18 def matching_methods @matching_methods end |
#responder ⇒ Object (readonly)
Returns the value of attribute responder.
16 17 18 |
# File 'lib/errata/erratum.rb', line 16 def responder @responder end |
#section ⇒ Object (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
47 48 49 |
# File 'lib/errata/erratum.rb', line 47 def abbr? @abbr_query == true end |
#conditions_match?(row) ⇒ 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
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
51 52 53 |
# File 'lib/errata/erratum.rb', line 51 def targets?(row) !!(conditions_match?(row) and expression_matches?(row)) end |