Permalink
Browse files

Regenerate full template on change

  • Loading branch information...
1 parent be1e4bc commit d8e4b004f0e8806bfbf57f961d7dd51d540b3c4d @lowentropy lowentropy committed Feb 18, 2011
Showing with 28 additions and 41 deletions.
  1. +17 −27 lib/guard/mustachejs.rb
  2. +11 −14 spec/guard/mustache_spec.rb
View
@@ -12,57 +12,46 @@ def initialize(watchers = [], options = {})
end
def start
- write_template read_template
+ write_template([]) unless File.exists?(options[:output])
end
def run_on_change(paths)
- write_template modify_template(read_template, paths)
+ run_all
end
def run_all
- run_on_change(Watcher.match_files(self, Dir.glob(File.join('**', '*'))))
+ paths = Watcher.match_files(self, Dir.glob(File.join('**', '*'))).uniq
+ write_template paths
end
private
- def read_template
- declaration = "var #{options[:variable]} = {};"
- template = File.read(options[:output]) if File.exists?(options[:output])
- write_once(template || "", declaration)
- end
-
- def write_once(template, line)
- template.tap { template << line << "\n" unless template.include? line }
- end
-
- def write_template(template)
- file = File.open(options[:output], 'w')
- file.puts template
- file.close
- end
-
- def modify_template(template, paths)
+ def write_template(paths)
+ template = "var #{options[:variable]} = {};\n"
paths.each do |path|
key, raw = convert_to_js path
- key = key.split '/'
key.unshift options[:variable].clone
write_key_value template, key, raw
end
+ file = File.open(options[:output], 'w')
+ file.puts template
+ file.close
notify paths
- template
end
def write_key_value(template, key, value)
- prefix = key.shift
+ lhs = key.shift
until key.empty?
part = key.shift
- prefix << '.' << part
- write_once template, "#{prefix} = #{key.any? ? '{}' : value};"
+ lhs << '.' << part
+ rhs = key.any? ? '{}' : value
+ line = "#{lhs} = #{rhs};\n"
+ template << line unless template.include? line
end
end
def notify(paths)
- message = "Added to #{options[:output]}: #{paths.join(', ')}"
+ message = "#{options[:output]}:\n\n#{paths.join("\n")}"
::Guard::Notifier.notify message, :title => "MustacheJS"
end
@@ -73,7 +62,8 @@ def convert_to_js(path)
raise "No capture groups in guard pattern for mustache"
end
js = File.read(path).inspect
- return base_name.gsub(/[-.]/,'_'), js
+ key = base_name.gsub(/[-.]/,'_').split('/')
+ return key, js
end
end
raise "No matcher matched '#{path}'"
@@ -23,12 +23,11 @@
end
end
- describe '#run_all' do
+ describe '#run_on_change' do
subject { Guard::MustacheJsGuard.new [watcher] }
- before { Dir.stub(:glob).and_return ['x/a.html', 'x/b.js', 'y/c.html'] }
it 'runs the run_on_change with all watched files' do
- subject.should_receive(:run_on_change).with(['x/a.html'])
- subject.run_all
+ subject.should_receive(:run_all)
+ subject.run_on_change [:foo, :bar, :baz]
end
end
@@ -46,8 +45,6 @@
let(:declaration) { "var mustache_templates = {};" }
it 'modifies the first line' do
assert_file_present output_path
- assert_reads output_path, declaration
- assert_writes output_path, declaration
subject.start
end
end
@@ -59,7 +56,7 @@
let(:paths) { ['app/mustache/x/y/foo.html', 'app/mustache/x/z/bar-baz.js'] }
let(:template) { '{{#user}}<p>Hi, {{name}}</p>{{/user}}' }
let(:output_path) { "public/javascripts/mustache-templates.js" }
- let(:message) { "Added to #{output_path}: #{paths.join(', ')}" }
+ let(:message) { "#{output_path}:\n\n#{paths.join("\n")}" }
let(:output) { <<-OUTPUT }
var mustache_templates = {};
mustache_templates.x = {};
@@ -69,15 +66,15 @@
mustache_templates.x.z.bar_baz_js = "#{template}";
OUTPUT
+ before { Dir.stub!(:glob).and_return(paths) }
subject { Guard::MustacheJsGuard.new watchers }
-
+
it 'should write the correct template' do
- assert_file_absent output_path
- assert_reads(paths[0], template)
- assert_reads(paths[1], template)
- assert_writes(output_path, output)
- assert_notifies(message)
- subject.run_on_change(paths)
+ assert_reads paths[0], template
+ assert_reads paths[1], template
+ assert_writes output_path, output
+ assert_notifies message
+ subject.run_on_change paths
end
end

0 comments on commit d8e4b00

Please sign in to comment.