/
license.rb
74 lines (58 loc) · 1.36 KB
/
license.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
module LicenseFinder::License
class << self
def all
@all ||= []
end
end
class Text
def initialize(text)
@text = normalized(text)
end
def to_s
@text
end
private
def normalized(text)
text.gsub(/\s+/, ' ').gsub(/['`"]{1,2}/, "\"")
end
end
class Base
class << self
def inherited(descendant)
LicenseFinder::License.all << descendant
end
def demodulized_name
name.gsub(/^.*::/, '')
end
def slug
demodulized_name.downcase
end
def pretty_name
demodulized_name
end
def license_text
unless defined?(@license_text)
template = File.join(LicenseFinder::ROOT_PATH, "templates", "#{demodulized_name}.txt").to_s
@license_text = Text.new(File.read(template)).to_s if File.exists?(template)
end
@license_text
end
def license_regex
/#{Regexp.escape(license_text).gsub(/<[^<>]+>/, '(.*)')}/ if license_text
end
end
def initialize(text)
self.text = text
end
attr_reader :text
def text=(text)
@text = Text.new(text).to_s
end
def matches?
!!(text =~ self.class.license_regex if self.class.license_regex)
end
end
end
Dir[File.join(File.dirname(__FILE__), 'license', '*.rb')].each do |license|
require license
end