Skip to content
Browse files

Add MD5 to Concatenation

  • Loading branch information...
1 parent 0ba31ee commit 92afccd42184680d2190b92a842ff03118b1ff5c @josh josh committed with Oct 31, 2009
Showing with 23 additions and 7 deletions.
  1. +12 −4 lib/sprockets/concatenation.rb
  2. +11 −3 test/test_concatenation.rb
View
16 lib/sprockets/concatenation.rb
@@ -1,26 +1,34 @@
+require 'digest/md5'
+
module Sprockets
class Concatenation
attr_reader :source_lines
-
+
def initialize
@source_lines = []
@source_file_mtimes = {}
+ @source_md5 = nil
end
-
+
def record(source_line)
+ @source_md5 = nil
source_lines << source_line
record_mtime_for(source_line.source_file)
source_line
end
-
+
def to_s
source_lines.join
end
+ def md5
+ @source_md5 ||= Digest::MD5.hexdigest(to_s)
+ end
+
def mtime
@source_file_mtimes.values.max
end
-
+
def save_to(filename)
timestamp = mtime
File.open(filename, "w") { |file| file.write(to_s) }
View
14 test/test_concatenation.rb
@@ -5,24 +5,32 @@ def setup
@concatenation = Sprockets::Concatenation.new
@environment = environment_for_fixtures
end
-
+
def test_record
assert_equal [], @concatenation.source_lines
assert_equal "hello\n", @concatenation.record(source_line("hello\n")).to_s
assert_equal "world\n", @concatenation.record(source_line("world\n")).to_s
assert_equal ["hello\n", "world\n"], @concatenation.source_lines.map { |source_line| source_line.to_s }
end
-
+
def test_to_s
@concatenation.record(source_line("hello\n"))
@concatenation.record(source_line("world\n"))
assert_equal "hello\nworld\n", @concatenation.to_s
end
-
+
def test_save_to
filename = File.join(FIXTURES_PATH, "output.js")
@concatenation.save_to(filename)
assert_equal @concatenation.to_s, IO.read(filename)
File.unlink(filename)
end
+
+ def test_md5
+ @concatenation.record(source_line("hello\n"))
+ assert_equal "b1946ac92492d2347c6235b4d2611184", @concatenation.md5
+
+ @concatenation.record(source_line("world\n"))
+ assert_equal "0f723ae7f9bf07744445e93ac5595156", @concatenation.md5
+ end
end

0 comments on commit 92afccd

Please sign in to comment.
Something went wrong with that request. Please try again.