unique assets

This commit is contained in:
John Bintz 2012-01-23 12:21:11 -05:00
parent e75abdda47
commit fb5018148f
5 changed files with 51 additions and 3 deletions

View File

@ -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 |

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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