-
Notifications
You must be signed in to change notification settings - Fork 146
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 #147 from guard/e2-js_erb_config
Allow customizing livereload.js with ERB
- Loading branch information
Showing
8 changed files
with
157 additions
and
11 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,23 @@ | ||
require 'erb' | ||
require 'tempfile' | ||
|
||
require 'guard/compat/plugin' | ||
|
||
module Guard | ||
class LiveReload < Plugin | ||
class Snippet | ||
attr_reader :path | ||
attr_reader :options | ||
|
||
def initialize(template, options) | ||
@options = options # for ERB context | ||
tmpfile = Tempfile.new('livereload.js') | ||
source = IO.read(template) | ||
data = ERB.new(source).result(binding) | ||
tmpfile.write(data) | ||
tmpfile.close | ||
@path = tmpfile.path | ||
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
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,45 @@ | ||
RSpec.describe Guard::LiveReload::Snippet do | ||
let(:options) { { foo: :bar } } | ||
|
||
let(:template) { '/foo/livereload.js.erb' } | ||
let(:contents) { '// <%= options[:foo] %>' } | ||
|
||
subject { described_class.new(template, options) } | ||
|
||
let(:tmpfile) { instance_double(Tempfile) } | ||
|
||
before do | ||
allow(File).to receive(:expand_path) do |*args| | ||
fail "stub called for File.expand_path(#{args.map(&:inspect) * ','})" | ||
end | ||
|
||
allow(IO).to receive(:read) do |*args| | ||
fail "stub called for IO.read(#{args.map(&:inspect) * ','})" | ||
end | ||
|
||
allow(IO).to receive(:read).with(template).and_return(contents) | ||
|
||
allow(Tempfile).to receive(:new).and_return(tmpfile) | ||
allow(tmpfile).to receive(:path).and_return('/tmp/livereload-123') | ||
allow(tmpfile).to receive(:write) | ||
allow(tmpfile).to receive(:close) | ||
end | ||
|
||
describe '#initialize' do | ||
it 'evaluates the js snippet file' do | ||
expect(tmpfile).to receive(:write).with('// bar') | ||
subject | ||
end | ||
|
||
it 'closes the tmpfile' do | ||
expect(tmpfile).to receive(:close) | ||
subject | ||
end | ||
end | ||
|
||
describe '#path' do | ||
it 'is set to a tmpfile with the ERB result' do | ||
expect(subject.path).to eq '/tmp/livereload-123' | ||
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