Class: Gonzui::LicenseDetector
- Inherits:
-
Object
- Object
- Gonzui::LicenseDetector
- Defined in:
- lib/gonzui/license.rb
Instance Method Summary collapse
- #allow_later? ⇒ Boolean
- #detect ⇒ Object
-
#initialize(text) ⇒ LicenseDetector
constructor
A new instance of LicenseDetector.
Constructor Details
#initialize(text) ⇒ LicenseDetector
Returns a new instance of LicenseDetector.
68 69 70 71 72 73 |
# File 'lib/gonzui/license.rb', line 68 def initialize(text) len = 512 @chunk = (text[0, len] or "") # first 512 bytes @chunk << (text[text.length - len, len] or "") # last 512 bytes @chunk.gsub!(/[^\w.']+/, " ") end |
Instance Method Details
#allow_later? ⇒ Boolean
75 76 77 78 |
# File 'lib/gonzui/license.rb', line 75 def allow_later? regexp = /or (at your option )?any later version/i return regexp.match(@chunk) end |
#detect ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/gonzui/license.rb', line 80 def detect candidates = [] LicenseRegistry.each {|license| if m = license.regexp.match(@chunk) if allow_later? license = license.clone license.allow_later end candidates.push([m[0].length, license]) end } if candidates.empty? return OtherLicense else license_of_shortest_match = candidates.min {|a, b| a.first <=> b.first }.last return license_of_shortest_match end end |