-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from yasaichi/support-sprockets4
Support Sprockets 4
- Loading branch information
Showing
17 changed files
with
204 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# This file was generated by Appraisal | ||
|
||
source "https://rubygems.org" | ||
|
||
gem "sprockets-rails", "~> 3.0" | ||
gem "sprockets", "~> 4.0.0.beta1" | ||
|
||
group :development, :test do | ||
gem "pry-byebug" | ||
gem "pry-coolline" | ||
end | ||
|
||
gemspec :path => "../" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,53 @@ | ||
require "active_support/inflector" | ||
require "grease" | ||
require "sprockets" | ||
|
||
module Gakubuchi | ||
class EngineRegistrar | ||
EXTENSION_WITH_SIGLE_DOT = /\A\.[^\.]+\z/ | ||
|
||
def initialize(env) | ||
@env = env | ||
end | ||
|
||
def register(target, engine) | ||
klass = constantize(engine) | ||
return false if !klass.instance_of?(::Class) || registered?(target) | ||
def register(mime_type, engine_name_or_class) | ||
engine = constantize(engine_name_or_class) | ||
return false if !engine.instance_of?(::Class) || mime_type.extensions.empty? | ||
|
||
args = [target, klass] | ||
args << { silence_deprecation: true } if Sprockets::VERSION.start_with?("3") | ||
if sprockets_major_version >= 4 | ||
register_as_transformer(mime_type, engine) | ||
else | ||
register_as_engine(mime_type, engine) | ||
end | ||
|
||
@env.register_engine(*args) | ||
true | ||
end | ||
|
||
def registered?(target) | ||
@env.engines.key?(::Sprockets::Utils.normalize_extension(target)) | ||
end | ||
|
||
private | ||
|
||
def constantize(klass) | ||
klass.to_s.constantize | ||
def constantize(constant_name) | ||
constant_name.to_s.constantize | ||
rescue ::LoadError, ::NameError | ||
nil | ||
end | ||
|
||
def sprockets_major_version | ||
@sprockets_major_version ||= ::Gem::Version.new(::Sprockets::VERSION).segments.first | ||
end | ||
|
||
def register_as_engine(mime_type, engine) | ||
mime_type.extensions.select { |ext| ext =~ EXTENSION_WITH_SIGLE_DOT }.each do |ext| | ||
args = [ext, engine] | ||
args << { silence_deprecation: true } if sprockets_major_version == 3 | ||
@env.register_engine(*args) | ||
end | ||
end | ||
|
||
def register_as_transformer(mime_type, engine) | ||
content_type = mime_type.content_type | ||
|
||
@env.register_mime_type(content_type, extensions: mime_type.extensions) | ||
@env.register_transformer(content_type, engine.default_mime_type, ::Grease.apply(engine)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module Gakubuchi | ||
class Error < StandardError | ||
InvalidTemplate = Class.new(self) | ||
InvalidMimeType = Class.new(self) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require "gakubuchi/error" | ||
require "set" | ||
|
||
module Gakubuchi | ||
class MimeType | ||
CONTENT_TYPE_FORMAT = %r(\A[^/]+/[^/]+\z) | ||
|
||
attr_reader :content_type, :extensions | ||
|
||
def initialize(content_type, extensions: []) | ||
unless content_type =~ CONTENT_TYPE_FORMAT | ||
message = %(`#{content_type}' is invalid as Content-Type) | ||
raise ::Gakubuchi::Error::InvalidMimeType, message | ||
end | ||
|
||
@content_type = content_type | ||
@extensions = ::Set.new(extensions).map(&:to_s) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//= link_tree ../images | ||
//= link_directory ../javascripts .js | ||
//= link_directory ../stylesheets .css |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,68 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Gakubuchi::EngineRegistrar do | ||
let(:env) { Sprockets::Environment.new } | ||
let(:engine_registrar) { described_class.new(env) } | ||
let(:env) { Sprockets::Environment.new } | ||
|
||
describe "#register" do | ||
let(:described_method) { -> { engine_registrar.register(target, engine) } } | ||
let(:described_method) { -> { engine_registrar.register(mime_type, engine) } } | ||
let(:mime_type) { Gakubuchi::MimeType.new(content_type, extensions: extensions) } | ||
let(:sprokcets_extensions) do | ||
extension_type = major_version_of(Sprockets) >= 4 ? :transformers : :engines | ||
-> { env.public_send(extension_type) } | ||
end | ||
|
||
context "when specified engine is an uninitialized constant" do | ||
let(:target) { :foo } | ||
let(:content_type) { "application/csv+ruby" } | ||
let(:engine) { "Foo" } | ||
let(:extensions) { %w(.rcsv .csv.ruby) } | ||
|
||
describe "env.engines" do | ||
subject { -> { env.engines } } | ||
it { expect(&described_method).not_to change(&subject) } | ||
it "should return false" do | ||
expect(described_method.call).to eq false | ||
end | ||
|
||
describe "return value" do | ||
subject { described_method.call } | ||
it { is_expected.to eq false } | ||
it "shouldn't register the engine for the MIME type" do | ||
expect(&described_method).not_to change(&sprokcets_extensions) | ||
end | ||
end | ||
|
||
context "when specified target is already registered" do | ||
let(:target) { :sass } | ||
let(:engine) { "Sprockets::SassTemplate" } | ||
context "when there is no extensions corresponding the MIME type" do | ||
let(:content_type) { "application/csv+ruby" } | ||
let(:engine) { Tilt::CSVTemplate } | ||
let(:extensions) { nil } | ||
|
||
describe "env.engines" do | ||
subject { -> { env.engines } } | ||
it { expect(&described_method).not_to change(&subject) } | ||
it "should return false" do | ||
expect(described_method.call).to eq false | ||
end | ||
|
||
describe "return value" do | ||
subject { described_method.call } | ||
it { is_expected.to eq false } | ||
it "shouldn't register the engine for the MIME type" do | ||
expect(&described_method).not_to change(&sprokcets_extensions) | ||
end | ||
end | ||
|
||
context "when specified target is not registered" do | ||
let(:target) { :foo } | ||
let(:engine) { "Sprockets::SassTemplate" } | ||
context "when all parameters are valid" do | ||
let(:content_type) { "application/csv+ruby" } | ||
let(:engine) { Tilt::CSVTemplate } | ||
let(:extensions) { %w(.rcsv .csv.ruby) } | ||
let(:extensions_with_single_dot) { extensions.select { |ext| ext =~ /\A\.[^\.]+\z/ } } | ||
|
||
describe "env.engines" do | ||
subject { -> { env.engines } } | ||
let(:expectation) { a_hash_including(".foo" => Sprockets::SassTemplate) } | ||
|
||
it { expect(&described_method).to change(&subject).to(expectation) } | ||
it "should return true" do | ||
expect(described_method.call).to eq true | ||
end | ||
|
||
describe "return value" do | ||
subject { described_method.call } | ||
it { is_expected.to eq true } | ||
end | ||
end | ||
end | ||
|
||
describe "#registered?" do | ||
subject { engine_registrar.registered?(target) } | ||
it "should register the engine for the MIME type" do | ||
diff = | ||
case major_version_of(Sprockets) | ||
when 2 | ||
Hash[extensions_with_single_dot.map { |ext| [ext, engine] }] | ||
when 3 | ||
Hash[extensions_with_single_dot.map { |ext| [ext, an_object_responding_to(:call)] }] | ||
when 4..Float::INFINITY | ||
{ content_type => { engine.default_mime_type => an_object_responding_to(:call) } } | ||
end | ||
|
||
context "when specified target is not registered" do | ||
let(:target) { :foo } | ||
it { is_expected.to eq false } | ||
end | ||
|
||
context "when specified target is already registered" do | ||
let(:target) { :erb } | ||
it { is_expected.to eq true } | ||
expect(&described_method).to change(&sprokcets_extensions).to(hash_including(diff)) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Gakubuchi::MimeType do | ||
describe "#new" do | ||
subject { -> { described_class.new(content_type) } } | ||
|
||
context "when an invalid `content_type` is specified" do | ||
let(:content_type) { "foo" } | ||
it { is_expected.to raise_error(Gakubuchi::Error::InvalidMimeType, /#{content_type}/) } | ||
end | ||
|
||
context "when a valid `content_type` is specified" do | ||
let(:content_type) { "application/csv+ruby" } | ||
it { is_expected.not_to raise_error } | ||
end | ||
end | ||
|
||
describe "#content_type" do | ||
subject { described_class.new(content_type).content_type } | ||
let(:content_type) { "application/csv+ruby" } | ||
|
||
it { is_expected.to eq content_type } | ||
end | ||
|
||
describe "#extensions" do | ||
subject { described_class.new("application/csv+ruby", options).extensions } | ||
|
||
context "when `extensions` isn't specified" do | ||
let(:options) { { extensions: nil } } | ||
|
||
it { is_expected.to be_an_instance_of(Array) } | ||
it { is_expected.to be_empty } | ||
end | ||
|
||
context "when `extensions` includes duplicated values" do | ||
let(:options) { { extensions: %w(.rcsv .csv.ruby .rcsv) } } | ||
|
||
it { is_expected.to be_an_instance_of(Array) } | ||
it { is_expected.to contain_exactly(*options[:extensions].uniq) } | ||
end | ||
|
||
context "when `extensions` includes non-string values" do | ||
let(:options) { { extensions: %i(.rcsv .csv.ruby) } } | ||
|
||
it { is_expected.to be_an_instance_of(Array) } | ||
it { is_expected.to contain_exactly(*options[:extensions].map(&:to_s)) } | ||
end | ||
end | ||
end |
Oops, something went wrong.