This commit is contained in:
Brandon Keepers
2014-11-28 14:40:02 -06:00
parent bc66f558b9
commit 26d789612b

View File

@@ -27,21 +27,40 @@ module Linguist
[] # No heuristics matched [] # No heuristics matched
end end
@heuristics = [] # Internal: Define a new heuristic.
#
# languages - String names of languages to disambiguate.
# heuristic - Block which takes data as an argument and returns a Language or nil.
#
# Examples
#
# create "Perl", "Prolog" do |data|
# if data.include?("use strict")
# Language["Perl"]
# elsif data.include?(":-")
# Language["Prolog"]
# end
# end
#
def self.create(*languages, &heuristic) def self.create(*languages, &heuristic)
@heuristics << new(languages, &heuristic) @heuristics << new(languages, &heuristic)
end end
# Internal: Array of defined heuristics
@heuristics = []
# Internal
def initialize(languages, &heuristic) def initialize(languages, &heuristic)
@languages = languages @languages = languages
@heuristic = heuristic @heuristic = heuristic
end end
# Internal: Check if this heuristic matches the candidate languages.
def matches?(candidates) def matches?(candidates)
candidates.all? { |l| @languages.include?(l.name) } candidates.all? { |l| @languages.include?(l.name) }
end end
# Internal: Perform the heuristic
def call(data) def call(data)
@heuristic.call(data) @heuristic.call(data)
end end