Class: Guard::Templates
- Defined in:
- lib/guard/compilers.rb,
lib/guard/templates.rb
Defined Under Namespace
Modules: Compilers
Constant Summary collapse
- DEFAULTS =
{ :namespace => 'this' }
Instance Method Summary collapse
-
#initialize(watchers = [], options = {}) ⇒ Templates
constructor
Initialize a Guard.
-
#reload ⇒ Object
Called when ‘reload|r|z + enter` is pressed.
-
#run_all ⇒ Object
Called when just ‘enter` is pressed This method should be principally used for long action like running all specs/tests/…
-
#run_on_change(paths) ⇒ Object
Called on file(s) modifications that the Guard watches.
-
#run_on_deletion(paths) ⇒ Object
Called on file(s) deletions that the Guard watches.
-
#start ⇒ Object
Call once when Guard starts.
-
#stop ⇒ Object
Called when ‘stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).
Constructor Details
#initialize(watchers = [], options = {}) ⇒ Templates
Initialize a Guard.
15 16 17 18 19 20 |
# File 'lib/guard/templates.rb', line 15 def initialize(watchers = [], = {}) @watchers = watchers @options = DEFAULTS.clone.merge() @single_file_mode = @options[:output].match(/\.js$/) super(watchers, @options) end |
Instance Method Details
#reload ⇒ Object
Called when ‘reload|r|z + enter` is pressed. This method should be mainly used for “reload” (really!) actions like reloading passenger/spork/bundler/…
36 37 |
# File 'lib/guard/templates.rb', line 36 def reload end |
#run_all ⇒ Object
Called when just ‘enter` is pressed This method should be principally used for long action like running all specs/tests/…
42 43 44 |
# File 'lib/guard/templates.rb', line 42 def run_all run_on_change(Watcher.match_files(self, Dir.glob('**/*'))) end |
#run_on_change(paths) ⇒ Object
Called on file(s) modifications that the Guard watches.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/guard/templates.rb', line 49 def run_on_change(paths) templates = {} paths.each do |path| @watchers.each do |watcher| if target = get_target(path, watcher) contents = File.read(path) if @single_file_mode templates[target] = contents else dir = Pathname.new(target[:path]).dirname.to_s FileUtils.mkdir_p(dir) if !File.exist?(dir) File.open(target[:path], 'w') do |f| f.write("#{@options[:namespace]}['#{target[:name]}'] = #{compile(contents, target)}") end end end end end if @single_file_mode File.open(@options[:output], 'w') do |f| js = templates.map{|target,content| "#{target[:name].dump}: #{compile(content, target)}" }.join(",\n") f.write "#{@options[:namespace]}.templates = {#{js}}" end end end |
#run_on_deletion(paths) ⇒ Object
Called on file(s) deletions that the Guard watches.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/guard/templates.rb', line 78 def run_on_deletion(paths) matched = false paths.each do |path| @watchers.each do |watcher| if target = get_target(path, watcher) matched = true FileUtils.rm target[:path] unless @single_file_mode end end end # this is slow, but works. would be better to do a eval + series of deletes, # but how to re-serialize the js object? run_all if @single_file_mode && matched end |
#start ⇒ Object
Call once when Guard starts. Please override initialize method to init stuff.
24 25 26 |
# File 'lib/guard/templates.rb', line 24 def start run_all end |
#stop ⇒ Object
Called when ‘stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).
30 31 |
# File 'lib/guard/templates.rb', line 30 def stop end |