Class: AbstractCheck
- Inherits:
-
Object
- Object
- AbstractCheck
- Defined in:
- lib/abstract_check.rb
Overview
The parent class of all checks. It provides all necessary logic except for the check itself.
Direct Known Subclasses
Constant Summary collapse
- @@classes =
[]
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .check_name(name) ⇒ Object
- .children ⇒ Object
-
.get_check_name ⇒ Object
Returns the name of the check.
- .inherited(c) ⇒ Object
Instance Method Summary collapse
-
#add_error(msg) ⇒ Object
Adds an error message for this check.
-
#check(path_obj, options) ⇒ Object
Override this method to perform your custom check.
-
#initialize(file_path) ⇒ AbstractCheck
constructor
Creates the check object, requires a path to check as the argument.
-
#run_check(options) ⇒ Object
Called by the Recokoner class to run the check.
-
#unit_parse(content, unit_hash) ⇒ Object
Performs simple unit parsing of strings of the form “[number] [units]”.
Constructor Details
#initialize(file_path) ⇒ AbstractCheck
Creates the check object, requires a path to check as the argument.
63 64 65 66 |
# File 'lib/abstract_check.rb', line 63 def initialize(file_path) @errors = [] @path = RFile.new(file_path) end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
6 7 8 |
# File 'lib/abstract_check.rb', line 6 def errors @errors end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/abstract_check.rb', line 7 def path @path end |
Class Method Details
.check_name(name) ⇒ Object
18 19 20 |
# File 'lib/abstract_check.rb', line 18 def self.check_name(name) self.class_eval("def self.get_check_name; '#{name}'; end") end |
.children ⇒ Object
14 15 16 |
# File 'lib/abstract_check.rb', line 14 def self.children return @@classes end |
.get_check_name ⇒ Object
Returns the name of the check. By default it un-camel-cases the class name. (Code taken from the underscore method of Rails’ Inflector Module.)
24 25 26 27 28 29 30 |
# File 'lib/abstract_check.rb', line 24 def self.get_check_name self.name.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |
.inherited(c) ⇒ Object
10 11 12 |
# File 'lib/abstract_check.rb', line 10 def self.inherited(c) @@classes << c end |
Instance Method Details
#add_error(msg) ⇒ Object
Adds an error message for this check. Use this if your check finds that the check is invalid.
75 76 77 |
# File 'lib/abstract_check.rb', line 75 def add_error(msg) @errors << "#{self.class.get_check_name.capitalize} found a problem with '#{@path.path}': #{msg}" end |
#check(path_obj, options) ⇒ Object
Override this method to perform your custom check. Its first argument is a RFile path object, the second argument is any options passed in along with the check in the YAML file.
82 83 84 |
# File 'lib/abstract_check.rb', line 82 def check(path_obj,) raise "Called abstract methd: check" end |
#run_check(options) ⇒ Object
Called by the Recokoner class to run the check
69 70 71 |
# File 'lib/abstract_check.rb', line 69 def run_check() check(@path,) end |
#unit_parse(content, unit_hash) ⇒ Object
Performs simple unit parsing of strings of the form “[number] [units]”.
The unit hash contains a hash where the keys are regular expressions that match a unit. Each value is a factor that will be multiplied against the parsed number.
Example converting to inches (12 inches = 1 foot): uhash = { /^inch/ => 1, /^feet/ => 12, /^foot/ => 12, ‘default’ => 1}
-
AbstractCheck.unit_parse(“1”,uhash) #=> 1
-
AbstractCheck.unit_parse(“1 inch”,uhash) #=> 1
-
AbstractCheck.unit_parse(“1 foot”,uhash) #=> 12
-
AbstractCheck.unit_parse(“2 feet”,uhash) #=> 24 inches
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/abstract_check.rb', line 46 def unit_parse(content,unit_hash) m = /(\d*\.?\d+)\s*(.*)/.match content raise "Could not parse '#{content}'" unless m if m.length == 2 factor = unit_hash['default'] || 1 elsif m.length == 3 key,factor = unit_hash.detect{|key,value| key.match(m[2])} raise "Invalid unit in '#{content}'" unless factor end if /\./.match m[1] m[1].to_f * factor else m[1].to_i * factor end end |