Class: Licensee::LicenseFile
- Inherits:
-
Object
- Object
- Licensee::LicenseFile
- Defined in:
- lib/licensee/license_file.rb
Constant Summary collapse
- FILENAMES =
%w[ LICENSE LICENSE.txt LICENSE.md UNLICENSE ]
Instance Attribute Summary collapse
-
#contents ⇒ Object
(also: #to_s, #content)
Returns the value of attribute contents.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .directory_exists?(base_path) ⇒ Boolean
- .file_exists?(file, base_path) ⇒ Boolean
- .find(base_path) ⇒ Object
Instance Method Summary collapse
- #diff(options = nil) ⇒ Object
-
#initialize(path = nil) ⇒ LicenseFile
constructor
A new instance of LicenseFile.
- #length ⇒ Object
- #length_delta(license) ⇒ Object
- #licenses_sorted ⇒ Object
- #match ⇒ Object
- #matches ⇒ Object
- #percent_changed(license) ⇒ Object
- #potential_licenses ⇒ Object
Constructor Details
#initialize(path = nil) ⇒ LicenseFile
Returns a new instance of LicenseFile.
14 15 16 |
# File 'lib/licensee/license_file.rb', line 14 def initialize(path=nil) @path = File.(path) end |
Instance Attribute Details
#contents ⇒ Object Also known as: to_s, content
Returns the value of attribute contents.
12 13 14 |
# File 'lib/licensee/license_file.rb', line 12 def contents @contents end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/licensee/license_file.rb', line 11 def path @path end |
Class Method Details
.directory_exists?(base_path) ⇒ Boolean
30 31 32 |
# File 'lib/licensee/license_file.rb', line 30 def self.directory_exists?(base_path) File.directory?(base_path) end |
.file_exists?(file, base_path) ⇒ Boolean
34 35 36 |
# File 'lib/licensee/license_file.rb', line 34 def self.file_exists?(file, base_path) File.exists? File.(file, base_path) end |
.find(base_path) ⇒ Object
24 25 26 27 28 |
# File 'lib/licensee/license_file.rb', line 24 def self.find(base_path) raise "Invalid directory" unless directory_exists? base_path file = self::FILENAMES.find { |file| file_exists?(file, base_path) } new(File.(file, base_path)) if file end |
Instance Method Details
#diff(options = nil) ⇒ Object
73 74 75 |
# File 'lib/licensee/license_file.rb', line 73 def diff(=nil) Diffy::Diff.new(match.body, content).to_s() end |
#length ⇒ Object
38 39 40 |
# File 'lib/licensee/license_file.rb', line 38 def length @length ||= content.length end |
#length_delta(license) ⇒ Object
42 43 44 |
# File 'lib/licensee/license_file.rb', line 42 def length_delta(license) (length - license.length).abs end |
#licenses_sorted ⇒ Object
50 51 52 |
# File 'lib/licensee/license_file.rb', line 50 def licenses_sorted @licenses_sorted ||= potential_licenses.sort_by { |l| length_delta(l) } end |
#match ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/licensee/license_file.rb', line 61 def match @match ||= licenses_sorted.find do |license| confidence = 1 - percent_changed(license) next unless confidence >= Licensee::CONFIDENCE_THRESHOLD license.match = confidence end end |
#matches ⇒ Object
54 55 56 57 58 59 |
# File 'lib/licensee/license_file.rb', line 54 def matches @matches ||= begin licenses_sorted.each { |l| l.match = 1 - percent_changed(l) } licenses_sorted.sort_by { |l| l.match }.select { |l| l.match > 0}.reverse end end |
#percent_changed(license) ⇒ Object
69 70 71 |
# File 'lib/licensee/license_file.rb', line 69 def percent_changed(license) (Levenshtein.distance(content, license.body).to_f / content.length.to_f).abs end |