Permalink
Browse files

Implement FilesystemProvider

  • Loading branch information...
1 parent ef95298 commit 2f1a3a8c6ce3ca351730e7d8fcc2769cd2face86 @Mange committed Nov 24, 2011
View
@@ -14,10 +14,12 @@ def self.app
require 'roadie/version'
require 'roadie/css_file_not_found'
require 'roadie/style_declaration'
-require 'roadie/inliner'
require 'roadie/asset_provider'
require 'roadie/asset_pipeline_provider'
+require 'roadie/filesystem_provider'
+
+require 'roadie/inliner'
require 'action_mailer'
require 'roadie/action_mailer_extensions'
@@ -0,0 +1,44 @@
+require 'pathname'
+
+module Roadie
+ class FilesystemProvider < AssetProvider
+ attr_reader :path
+
+ def initialize(path = nil)
+ super()
+ if path
+ @path = resolve_path(path)
+ else
+ @path = default_path
+ end
+ end
+
+ def find(name)
+ file = path.join(name)
+ if file.exist?
+ file.read.strip
+ else
+ return find("#{name}.css") if name.to_s !~ /\.css$/
+ raise CSSFileNotFound.new(name, file.to_s)
+ end
+ end
+
+ private
+ def default_path
+ resolve_path("public/stylesheets")
+ end
+
+ def resolve_path(path)
+ if path.kind_of?(Pathname)
+ @path = path
+ else
+ pathname = Pathname.new(path)
+ if pathname.absolute?
+ @path = pathname
+ else
+ @path = Roadie.app.root.join(path)
+ end
+ end
+ end
+ end
+end
@@ -1,9 +1,11 @@
require 'spec_helper'
+require 'shared_examples/asset_provider_examples'
module Roadie
describe AssetPipelineProvider do
let(:provider) { AssetPipelineProvider.new }
- subject { provider }
+
+ it_behaves_like AssetProvider
it "has a configurable prefix" do
AssetPipelineProvider.new("/prefix").prefix.should == "/prefix"
@@ -57,15 +59,5 @@ def expect_pipeline_access(name, returning = '')
}.to raise_error(Roadie::CSSFileNotFound, /not_here/)
end
end
-
- describe "#all(files)" do
- it "loads files in order and join them with a newline" do
- provider.should_receive(:find).with('one').twice.and_return('contents of one')
- provider.should_receive(:find).with('two').twice.and_return('contents of two')
-
- provider.all(%w[one two]).should == "contents of one\ncontents of two"
- provider.all(%w[two one]).should == "contents of two\ncontents of one"
- end
- end
end
end
@@ -0,0 +1,80 @@
+require 'spec_helper'
+require 'shared_examples/asset_provider_examples'
+require 'tmpdir'
+
+module Roadie
+ describe FilesystemProvider do
+ let(:provider) { FilesystemProvider.new }
+
+ it_behaves_like AssetProvider
+
+ it "has a configurable path" do
+ path = Pathname.new("/path")
+ FilesystemProvider.new(path).path.should == path
+ end
+
+ it "bases the path on the project root if passed a string with a relative path" do
+ FilesystemProvider.new("foo/bar").path.should == Roadie.app.root.join("foo", "bar")
+ FilesystemProvider.new("/foo/bar").path.should == Pathname.new("/foo/bar")
+ end
+
+ it 'has a path of "<root>/public/stylesheets" by default' do
+ provider.path.should == Roadie.app.root.join('public', 'stylesheets')
+ end
+
+ describe "#find(file)" do
+ around(:each) do |example|
+ Dir.mktmpdir do |path|
+ Dir.chdir(path) { example.run }
+ end
+ end
+
+ let(:provider) { FilesystemProvider.new(Pathname.new('.')) }
+
+ def create_file(name, contents = '')
+ Pathname.new(name).tap do |path|
+ path.dirname.mkpath unless path.dirname.directory?
+ path.open('w') { |file| file << contents }
+ end
+ end
+
+ it "loads files from the filesystem" do
+ create_file('foo.css', 'contents of foo.css')
+ provider.find('foo.css').should == 'contents of foo.css'
+ end
+
+ it 'tries the filename with a ".css" extension if it does not exist' do
+ create_file('bar', 'in bare bar')
+ create_file('bar.css', 'in bar.css')
+ create_file('foo.css', 'in foo.css')
+
+ provider.find('bar').should == 'in bare bar'
+ provider.find('foo').should == 'in foo.css'
+ end
+
+ it "strips the contents" do
+ create_file('foo.css', " contents \n ")
+ provider.find('foo.css').should == "contents"
+ end
+
+ it "supports nested directories" do
+ create_file('path/to/foo.css')
+ create_file('path/from/bar.css')
+
+ provider.find('path/to/foo.css')
+ provider.find('path/from/bar.css')
+ end
+
+ it "works with double slashes in the path" do
+ create_file('path/to/foo.css')
+ provider.find('path/to//foo.css')
+ end
+
+ it "raises a Roadie::CSSFileNotFound error when the file could not be found" do
+ expect {
+ provider.find('not_here.css')
+ }.to raise_error(Roadie::CSSFileNotFound, /not_here/)
+ end
+ end
+ end
+end
@@ -0,0 +1,11 @@
+shared_examples_for Roadie::AssetProvider do
+ describe "#all(files)" do
+ it "loads files in order and join them with a newline" do
+ provider.should_receive(:find).with('one').twice.and_return('contents of one')
+ provider.should_receive(:find).with('two').twice.and_return('contents of two')
+
+ provider.all(%w[one two]).should == "contents of one\ncontents of two"
+ provider.all(%w[two one]).should == "contents of two\ncontents of one"
+ end
+ end
+end
View
@@ -30,6 +30,10 @@ def assets
env.append_path FIXTURES_PATH.join('app','assets','stylesheets')
env
end
+
+ def root
+ FIXTURES_PATH
+ end
end
unless defined?(Rails)

0 comments on commit 2f1a3a8

Please sign in to comment.