Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 89 lines (68 sloc) 1.751 kb
c58f027 Make license file matching polymorphic
Ian Lesperance and Matthew Kane Parker authored
1 module LicenseFinder::License
2 class << self
3 def all
4 @all ||= []
5 end
522327d whitelist with any known variant of a license name
Matthew Kane Parker authored
6
7 def find_by_name(license_name)
8 all.detect { |l| l.names.map(&:downcase).include? license_name.to_s.downcase }
9 end
c58f027 Make license file matching polymorphic
Ian Lesperance and Matthew Kane Parker authored
10 end
11
12 class Text
13 def initialize(text)
14 @text = normalized(text)
15 end
16
17 def to_s
18 @text
19 end
20
21 private
22
23 def normalized(text)
f1f3d05 Support BSD, New BSD, and Simplified BSD licenses
Ian Lesperance and Matthew Kane Parker authored
24 text.gsub(/\s+/, ' ').gsub(/['`"]{1,2}/, "\"")
c58f027 Make license file matching polymorphic
Ian Lesperance and Matthew Kane Parker authored
25 end
26 end
27
28 class Base
29 class << self
12597db link license names to license urls in html report
Matthew Kane Parker authored
30 attr_accessor :license_url, :alternative_names
31
c58f027 Make license file matching polymorphic
Ian Lesperance and Matthew Kane Parker authored
32 def inherited(descendant)
33 LicenseFinder::License.all << descendant
34 end
35
12597db link license names to license urls in html report
Matthew Kane Parker authored
36 def names
522327d whitelist with any known variant of a license name
Matthew Kane Parker authored
37 ([demodulized_name, pretty_name] + self.alternative_names).uniq
12597db link license names to license urls in html report
Matthew Kane Parker authored
38 end
39
40 def alternative_names
41 @alternative_names ||= []
42 end
43
c58f027 Make license file matching polymorphic
Ian Lesperance and Matthew Kane Parker authored
44 def demodulized_name
45 name.gsub(/^.*::/, '')
46 end
47
48 def slug
49 demodulized_name.downcase
50 end
51
52 def pretty_name
53 demodulized_name
54 end
55
56 def license_text
57 unless defined?(@license_text)
6c897f6 Moved license data
Ian Lesperance authored
58 template = File.join(LicenseFinder::ROOT_PATH, "data", "licenses", "#{demodulized_name}.txt").to_s
c58f027 Make license file matching polymorphic
Ian Lesperance and Matthew Kane Parker authored
59
60 @license_text = Text.new(File.read(template)).to_s if File.exists?(template)
61 end
62 @license_text
63 end
f1f3d05 Support BSD, New BSD, and Simplified BSD licenses
Ian Lesperance and Matthew Kane Parker authored
64
65 def license_regex
66 /#{Regexp.escape(license_text).gsub(/<[^<>]+>/, '(.*)')}/ if license_text
67 end
c58f027 Make license file matching polymorphic
Ian Lesperance and Matthew Kane Parker authored
68 end
69
70 def initialize(text)
71 self.text = text
72 end
73
74 attr_reader :text
75
76 def text=(text)
77 @text = Text.new(text).to_s
78 end
79
80 def matches?
f1f3d05 Support BSD, New BSD, and Simplified BSD licenses
Ian Lesperance and Matthew Kane Parker authored
81 !!(text =~ self.class.license_regex if self.class.license_regex)
c58f027 Make license file matching polymorphic
Ian Lesperance and Matthew Kane Parker authored
82 end
83 end
84 end
85
86 Dir[File.join(File.dirname(__FILE__), 'license', '*.rb')].each do |license|
87 require license
88 end
Something went wrong with that request. Please try again.