Permalink
Browse files

Refactoring

  • Loading branch information...
rymai committed Oct 6, 2013
1 parent 83371f1 commit 99c106c872871e67c3879da4817362c14c8c1248
Showing with 60 additions and 45 deletions.
  1. +1 −1 Guardfile
  2. +29 −24 lib/guard/haml.rb
  3. +1 −1 lib/guard/haml/templates/Guardfile
  4. +29 −19 spec/guard/haml_spec.rb
View
@@ -1,6 +1,6 @@
guard :rspec do
watch(%r{^spec/.+_spec\.rb$})
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end
View
@@ -22,7 +22,7 @@ def initialize(opts = {})
end
def start
- run_all if @options[:run_at_start]
+ run_all if options[:run_at_start]
end
def stop
@@ -39,31 +39,34 @@ def run_all
def run_on_changes(paths)
paths.each do |file|
- output_files = get_output(file)
+ output_paths = _output_paths(file)
compiled_haml = compile_haml(file)
- output_files.each do |output_file|
+
+ output_paths.each do |output_file|
FileUtils.mkdir_p File.dirname(output_file)
File.open(output_file, 'w') { |f| f.write(compiled_haml) }
end
+
message = "Successfully compiled haml to html!\n"
- message += "# #{file} -> #{output_files.join(', ')}".gsub("#{::Bundler.root.to_s}/", '')
+ message += "# #{file} -> #{output_paths.join(', ')}".gsub("#{::Bundler.root.to_s}/", '')
::Guard::UI.info message
- Notifier.notify( true, message ) if @options[:notifications]
+ Notifier.notify(true, message) if options[:notifications]
end
- notify paths
+
+ _notify_other_guard_plugins(paths)
end
private
- def compile_haml file
+ def compile_haml(file)
begin
content = File.new(file).read
- engine = ::Haml::Engine.new(content, (@options[:haml_options] || {}))
+ engine = ::Haml::Engine.new(content, (options[:haml_options] || {}))
engine.render
rescue StandardError => error
message = "HAML compilation failed!\nError: #{error.message}"
::Guard::UI.error message
- Notifier.notify( false, message ) if @options[:notifications]
+ Notifier.notify(false, message) if options[:notifications]
throw :task_has_failed
end
end
@@ -74,13 +77,14 @@ def compile_haml file
# @param file [String, Array<String>] path to file being built
# @return [Array<String>] path(s) to file where output should be written
#
- def get_output(file)
+ def _output_paths(file)
input_file_dir = File.dirname(file)
- file_name = get_file_name(file)
- file_name = "#{file_name}.html" if append_file_ext_to_output_path?(file_name)
- input_file_dir = input_file_dir.gsub(Regexp.new("#{@options[:input]}(\/){0,1}"), '') if @options[:input]
- if @options[:output]
- Array(@options[:output]).map do |output_dir|
+ file_name = _output_filename(file)
+ file_name = "#{file_name}.html" if _append_html_ext_to_output_path?(file_name)
+ input_file_dir = input_file_dir.gsub(Regexp.new("#{options[:input]}(\/){0,1}"), '') if options[:input]
+
+ if options[:output]
+ Array(options[:output]).map do |output_dir|
File.join(output_dir, input_file_dir, file_name)
end
else
@@ -104,31 +108,32 @@ def get_output(file)
# @param file String path to file
# @return String file name including extension
#
- def get_file_name(file)
+ def _output_filename(file)
sub_strings = File.basename(file).split('.')
base_name, extensions = sub_strings.first, sub_strings[1..-1]
if extensions.last == 'haml'
extensions.pop
if extensions.empty?
- [base_name, @options[:default_ext]].join('.')
+ [base_name, options[:default_ext]].join('.')
else
[base_name, extensions].flatten.join('.')
end
else
- [base_name, extensions, @options[:default_ext]].flatten.join('.')
+ [base_name, extensions, options[:default_ext]].flatten.join('.')
end
end
- def append_file_ext_to_output_path?(file_name)
- return unless @options[:auto_append_file_ext]
- file_name.match("\.html?").nil?
+ def _append_html_ext_to_output_path?(filename)
+ return unless options[:auto_append_file_ext]
+
+ filename.match("\.html?").nil?
end
- def notify(changed_files)
- ::Guard.guards.reject{ |guard| guard == self }.each do |guard|
+ def _notify_other_guard_plugins(changed_files)
+ ::Guard.guards.reject { |guard| guard == self }.each do |guard|
paths = Watcher.match_files(guard, changed_files)
- guard.run_on_changes paths unless paths.empty?
+ guard.run_on_changes(paths) unless paths.empty?
end
end
end
@@ -6,6 +6,6 @@
# notifications: true send notifictions to Growl/libnotify/Notifu
# haml_options: { ugly: true } pass options to the Haml engine
-guard 'haml' do
+guard :haml do
watch(/^.+(\.html\.haml)$/)
end
View
@@ -16,12 +16,21 @@
specify { subject.options[:notifications].should be_true }
end
- context "when recieves options hash" do
+ context "when receives options hash" do
it 'should merge it to @options instance variable' do
subject_with_options.options[:notifications].should be_false
subject_with_options.options[:run_at_start].should be_true
end
end
+
+ context 'with no watchers and the :input option' do
+ let(:plugin) { described_class.new(input: 'markup') }
+
+ it 'generates watchers automatically' do
+ plugin.watchers.should have(1).item
+ plugin.watchers[0].pattern.should eq %r{^markup/(.+(\.html)?\.haml)$}
+ end
+ end
end
describe '#start' do
@@ -70,20 +79,20 @@
end
end
- describe '#get_output' do
+ describe '#_output_paths' do
context 'by default' do
it 'should return test/index.html.haml as [test/index.html]' do
- subject.send(:get_output, 'test/index.html.haml').
+ subject.send(:_output_paths, 'test/index.html.haml').
should eq(['test/index.html'])
end
it 'should return test/index.htm.haml as [test/index.htm]' do
- subject.send(:get_output, 'test/index.htm.haml').
+ subject.send(:_output_paths, 'test/index.htm.haml').
should eq(['test/index.htm'])
end
it 'should return test/index.haml as [test/index.html]' do
- subject.send(:get_output, 'test/index.haml').
+ subject.send(:_output_paths, 'test/index.haml').
should eq(['test/index.html'])
end
end
@@ -94,7 +103,7 @@
end
it 'should return test/index.html.haml as [demo/output/test/index.html.haml]' do
- subject.send(:get_output, 'test/index.html.haml').
+ subject.send(:_output_paths, 'test/index.html.haml').
should eq(['demo/output/test/index.html'])
end
end
@@ -105,7 +114,7 @@
end
it 'should return test/index.html.haml as [demo1/output/test/index.html.haml, demo2/output/test/index.html.haml]' do
- subject.send(:get_output, 'test/index.html.haml').
+ subject.send(:_output_paths, 'test/index.html.haml').
should eq(['demo1/output/test/index.html', 'demo2/output/test/index.html'])
end
end
@@ -116,12 +125,12 @@
end
it 'should return test/index.haml as test/index.txt' do
- subject.send(:get_output, 'test/index.haml').
+ subject.send(:_output_paths, 'test/index.haml').
should eq(['test/index.txt'])
end
it 'should return test/index.php.haml as test/index.php due to the second extension' do
- subject.send(:get_output, 'test/index.php.haml').
+ subject.send(:_output_paths, 'test/index.php.haml').
should eq(['test/index.php'])
end
end
@@ -134,7 +143,7 @@
end
it 'should return test/ignore/index.html.haml as [index.html]' do
- subject.send(:get_output, 'test/ignore/index.html.haml').
+ subject.send(:_output_paths, 'test/ignore/index.html.haml').
should eq(['index.html'])
end
@@ -144,53 +153,54 @@
end
it 'should return test/ignore/abc/index.html.haml as [demo/output/abc/index.html]' do
- subject.send(:get_output, 'test/ignore/abc/index.html.haml').
+ subject.send(:_output_paths, 'test/ignore/abc/index.html.haml').
should eq(['demo/output/abc/index.html'])
end
end
end
context 'when the input file contains a second extension"' do
it 'should return test/index.php.haml as [test/index.php]' do
- subject.send(:get_output, 'test/index.php.haml').
+ subject.send(:_output_paths, 'test/index.php.haml').
should eq(['test/index.php'])
end
end
end
- describe '#get_file_name' do
+ describe '#_output_filename' do
context 'by default (if a ".haml" extension has been defined)' do
it 'should return the file name with the default extension ".html"' do
- subject.send(:get_file_name, 'test/index.haml').
+ subject.send(:_output_filename, 'test/index.haml').
should eq('index.html')
end
end
context 'if no extension has been defined at all' do
it 'should return the file name with the default extension ".html"' do
- subject.send(:get_file_name, 'test/index').
+ subject.send(:_output_filename, 'test/index').
should eq('index.html')
end
end
context 'if an extension other than ".haml" has been defined' do
it 'should return the file name with the default extension ".html"' do
- subject.send(:get_file_name, 'test/index.foo').
+ subject.send(:_output_filename, 'test/index.foo').
should eq('index.foo.html')
end
end
context 'if multiple extensions including ".haml" have been defined' do
it 'should return the file name with the extension second to last' do
- subject.send(:get_file_name, 'test/index.foo.haml').
+ subject.send(:_output_filename, 'test/index.foo.haml').
should eq('index.foo')
end
end
end
describe '#run_on_changes' do
it 'should notify other guards upon completion' do
- subject.should_receive(:notify).with([])
+ subject.should_receive(:_notify_other_guard_plugins).with([])
+
subject.run_on_changes([])
end
@@ -218,7 +228,7 @@
context 'with two outputs' do
before do
- subject_notifiable.stub(:get_output).and_return(["#{@fixture_path}/test.html", "#{@fixture_path}/test2.html"])
+ subject_notifiable.stub(:_output_paths).and_return(["#{@fixture_path}/test.html", "#{@fixture_path}/test2.html"])
end
after do

0 comments on commit 99c106c

Please sign in to comment.