mirror of
				https://github.com/KevinMidboe/linguist.git
				synced 2025-10-29 17:50:22 +00:00 
			
		
		
		
	Allow any enum to be passed to Repository for stat collecting
Fixes #28
This commit is contained in:
		| @@ -20,35 +20,17 @@ module Linguist | |||||||
|  |  | ||||||
|     # Public: Initialize a new Repository |     # Public: Initialize a new Repository | ||||||
|     # |     # | ||||||
|     # paths - An Array of Blob objects or a Hash of String path keys |     # enum - Enumerator that responds to `each` and | ||||||
|     #         and Blob values. |     #        yields Blob objects | ||||||
|     # |     # | ||||||
|     # Returns a Repository |     # Returns a Repository | ||||||
|     def initialize(paths) |     def initialize(enum) | ||||||
|       case paths |       @enum = enum | ||||||
|       when Array |  | ||||||
|         @paths = paths.inject({}) do |h, blob| |  | ||||||
|           h[blob.name] = blob |  | ||||||
|           h |  | ||||||
|         end |  | ||||||
|       when Hash |  | ||||||
|         @paths = paths |  | ||||||
|       else |  | ||||||
|         raise ArgumentError, "#{paths.class} is not an Array or Hash" |  | ||||||
|       end |  | ||||||
|  |  | ||||||
|       @computed_stats = false |       @computed_stats = false | ||||||
|       @language = @size = nil |       @language = @size = nil | ||||||
|       @sizes = Hash.new { 0 } |       @sizes = Hash.new { 0 } | ||||||
|     end |     end | ||||||
|  |  | ||||||
|     # Public: Lookup blob for path. |  | ||||||
|     # |  | ||||||
|     # Returns a Blob |  | ||||||
|     def [](path) |  | ||||||
|       @paths[path] |  | ||||||
|     end |  | ||||||
|  |  | ||||||
|     # Public: Returns a breakdown of language stats. |     # Public: Returns a breakdown of language stats. | ||||||
|     # |     # | ||||||
|     # Examples |     # Examples | ||||||
| @@ -84,7 +66,7 @@ module Linguist | |||||||
|     def compute_stats |     def compute_stats | ||||||
|       return if @computed_stats |       return if @computed_stats | ||||||
|  |  | ||||||
|       @paths.each do |path, blob| |       @enum.each do |blob| | ||||||
|         # Skip vendored or generated blobs |         # Skip vendored or generated blobs | ||||||
|         next if blob.vendored? || blob.generated? || blob.language.nil? |         next if blob.vendored? || blob.generated? || blob.language.nil? | ||||||
|  |  | ||||||
|   | |||||||
| @@ -13,11 +13,6 @@ class TestRepository < Test::Unit::TestCase | |||||||
|     repo(File.expand_path("../..", __FILE__)) |     repo(File.expand_path("../..", __FILE__)) | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   def test_lookup_path |  | ||||||
|     assert linguist_repo['lib/linguist.rb'] |  | ||||||
|     assert_equal Language['Ruby'], linguist_repo['lib/linguist.rb'].language |  | ||||||
|   end |  | ||||||
|  |  | ||||||
|   def test_linguist_language |   def test_linguist_language | ||||||
|     assert_equal Language['JavaScript'], linguist_repo.language |     assert_equal Language['JavaScript'], linguist_repo.language | ||||||
|   end |   end | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user