Class: Licensee::LicenseFile

Inherits:
Object
  • Object
show all
Defined in:
lib/licensee/license_file.rb

Constant Summary collapse

FILENAMES =
%w[
  LICENSE
  LICENSE.txt
  LICENSE.md
  UNLICENSE
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.expand_path(path)
end

Instance Attribute Details

#contentsObject 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

#pathObject (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

Returns:

  • (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

Returns:

  • (Boolean)


34
35
36
# File 'lib/licensee/license_file.rb', line 34

def self.file_exists?(file, base_path)
  File.exists? File.expand_path(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.expand_path(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(options=nil)
  Diffy::Diff.new(match.body, content).to_s(options)
end

#lengthObject



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_sortedObject



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

#matchObject



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

#matchesObject



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

#potential_licensesObject



46
47
48
# File 'lib/licensee/license_file.rb', line 46

def potential_licenses
  @potential_licenses ||= Licensee::Licenses.list.clone.select { |l| length_delta(l) < length }
end