Skip to content
Browse files

Various ConversionObserver methods handling files to convert.

.files_to_convert is a simple attr reader
Adding a file to convert appends it to the array and ensures there are no duplicates
Querying a file to convert shifts the first element off the array
Clearing the array is only there as a convenience for testing.
  • Loading branch information...
1 parent 274c821 commit 34aa2f000917918333b5bd8e4b35ada8b2665323 @ymendel committed Jun 27, 2008
Showing with 81 additions and 0 deletions.
  1. +17 −0 lib/conversion_observer.rb
  2. +64 −0 spec/conversion_observer_spec.rb
View
17 lib/conversion_observer.rb
@@ -1,6 +1,23 @@
module ConversionObserver
+ @files_to_convert = []
+
class << self
+ attr_reader :files_to_convert
+
+ def add_file_to_convert(file)
+ @files_to_convert << file
+ @files_to_convert.uniq!
+ end
+
def file_to_convert
+ files_to_convert.shift
+ end
+
+
+ private
+
+ def clear_files_to_convert
+ @files_to_convert = []
end
end
end
View
64 spec/conversion_observer_spec.rb
@@ -1,7 +1,71 @@
require File.dirname(__FILE__) + '/spec_helper.rb'
describe ConversionObserver do
+ it 'should have files to convert' do
+ ConversionObserver.should respond_to(:files_to_convert)
+ end
+
+ it 'should add a file to convert' do
+ ConversionObserver.should respond_to(:add_file_to_convert)
+ end
+
it 'should give a file to convert' do
ConversionObserver.should respond_to(:file_to_convert)
end
+
+ describe 'files to convert' do
+ it 'should default to an empty array' do
+ ConversionObserver.files_to_convert.should == []
+ end
+
+ describe 'when added' do
+ before :each do
+ ConversionObserver.send(:clear_files_to_convert)
+ end
+
+ it 'should be in the returned array' do
+ @file = stub('file')
+ ConversionObserver.add_file_to_convert(@file)
+ ConversionObserver.files_to_convert.should == [@file]
+ end
+
+ it 'should add to the end of the array' do
+ @file = stub('file')
+ @other_file = stub('other file')
+ ConversionObserver.add_file_to_convert(@file)
+ ConversionObserver.add_file_to_convert(@other_file)
+ ConversionObserver.files_to_convert.should == [@file, @other_file]
+ end
+
+ it 'should remove duplicates' do
+ @file = stub('file')
+ ConversionObserver.add_file_to_convert(@file)
+ ConversionObserver.add_file_to_convert(@file)
+ ConversionObserver.files_to_convert.should == [@file]
+ end
+ end
+
+ describe 'when retrieved' do
+ before :each do
+ ConversionObserver.send(:clear_files_to_convert)
+ @file = stub('file')
+ @files = [@file] + Array.new(3) { |i| stub("file #{i+1}") }
+ @files.each { |file| ConversionObserver.add_file_to_convert(file) }
+ end
+
+ it 'should return the first file in the array' do
+ ConversionObserver.file_to_convert.should == @file
+ end
+
+ it 'should remove the file from the array' do
+ ConversionObserver.file_to_convert
+ ConversionObserver.files_to_convert.should == @files[1..-1]
+ end
+
+ it 'should return nil if the array is empty' do
+ ConversionObserver.send(:clear_files_to_convert)
+ ConversionObserver.file_to_convert.should be_nil
+ end
+ end
+ end
end

0 comments on commit 34aa2f0

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