diff --git a/features/process_with_sprockets.feature b/features/process_with_sprockets.feature index 3ae262a..6173abd 100644 --- a/features/process_with_sprockets.feature +++ b/features/process_with_sprockets.feature @@ -1,5 +1,5 @@ Feature: Process files with Sprockets - Scenario: Simple Sprockets work + Background: Given I have the file "dir/file.js" with the content: """ //= require other @@ -9,6 +9,8 @@ Feature: Process files with Sprockets """ another file """ + + Scenario: Simple Sprockets work When I instantiate a Sprockets handler with the following asset directories: | dir | And I work with the Sprockets asset "file" @@ -16,3 +18,18 @@ Feature: Process files with Sprockets | dir/other.js | | dir/file.js | + Scenario: Require the file twice + Given I have the file "dir/third.js" with the content: + """ + //= require other + third file + """ + When I instantiate a Sprockets handler with the following asset directories: + | dir | + And I work with the Sprockets asset "file" + And I work with the Sprockets asset "third" + Then the handler should have the following files in order: + | dir/other.js | + | dir/file.js | + | dir/third.js | + diff --git a/lib/flowerbox/delivery.rb b/lib/flowerbox/delivery.rb index 2174765..4ece455 100644 --- a/lib/flowerbox/delivery.rb +++ b/lib/flowerbox/delivery.rb @@ -3,6 +3,7 @@ module Flowerbox autoload :Server, 'flowerbox/delivery/server' autoload :TemplateRenderer, 'flowerbox/delivery/template_renderer' autoload :SprocketsHandler, 'flowerbox/delivery/sprockets_handler' + autoload :UniqueAssetList, 'flowerbox/delivery/unique_asset_list' end end diff --git a/lib/flowerbox/delivery/sprockets_handler.rb b/lib/flowerbox/delivery/sprockets_handler.rb index a4e29c6..d6386a5 100644 --- a/lib/flowerbox/delivery/sprockets_handler.rb +++ b/lib/flowerbox/delivery/sprockets_handler.rb @@ -7,11 +7,11 @@ module Flowerbox::Delivery def initialize(options) @options = options - @files = [] + @files = UniqueAssetList.new end def add(asset) - @files += paths_for(asset) + @files.add(paths_for(asset)) end def paths_for(asset) diff --git a/lib/flowerbox/delivery/unique_asset_list.rb b/lib/flowerbox/delivery/unique_asset_list.rb new file mode 100644 index 0000000..cfb58e3 --- /dev/null +++ b/lib/flowerbox/delivery/unique_asset_list.rb @@ -0,0 +1,13 @@ +module Flowerbox::Delivery + class UniqueAssetList < ::Array + def add(files) + files.each { |file| self << file if !include?(file) } + end + + private + def include?(file) + any? { |other_file| other_file == file } + end + end +end + diff --git a/spec/flowerbox/delivery/unique_asset_list_spec.rb b/spec/flowerbox/delivery/unique_asset_list_spec.rb new file mode 100644 index 0000000..33fbae5 --- /dev/null +++ b/spec/flowerbox/delivery/unique_asset_list_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe Flowerbox::Delivery::UniqueAssetList do + let(:unique_asset_list) { described_class.new } + + describe "#add" do + let(:first) { Pathname.new('one') } + let(:second) { Pathname.new('one') } + let(:third) { Pathname.new('two') } + + it 'should not add assets already added' do + unique_asset_list.add([ first, second, third ]) + + unique_asset_list.should == [ first, third ] + end + end +end