Class: Guard::Templates

Inherits:
Guard
  • Object
show all
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

Constructor Details

#initialize(watchers = [], options = {}) ⇒ Templates

Initialize a Guard.

Parameters:

  • watchers (Array<Guard::Watcher>) (defaults to: [])

    the Guard file watchers

  • options (Hash) (defaults to: {})

    the custom Guard options



15
16
17
18
19
20
# File 'lib/guard/templates.rb', line 15

def initialize(watchers = [], options = {})
  @watchers = watchers
  @options = DEFAULTS.clone.merge(options)
  @single_file_mode = @options[:output].match(/\.js$/)
  super(watchers, @options)
end

Instance Method Details

#reloadObject

Called when ‘reload|r|z + enter` is pressed. This method should be mainly used for “reload” (really!) actions like reloading passenger/spork/bundler/…

Raises:

  • (:task_has_failed)

    when reload has failed



36
37
# File 'lib/guard/templates.rb', line 36

def reload
end

#run_allObject

Called when just ‘enter` is pressed This method should be principally used for long action like running all specs/tests/…

Raises:

  • (:task_has_failed)

    when run_all has failed



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.

Parameters:

  • paths (Array<String>)

    the changes files or paths

Raises:

  • (:task_has_failed)

    when run_on_change has failed



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.

Parameters:

  • paths (Array<String>)

    the deleted files or paths

Raises:

  • (:task_has_failed)

    when run_on_change has failed



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

#startObject

Call once when Guard starts. Please override initialize method to init stuff.

Raises:

  • (:task_has_failed)

    when start has failed



24
25
26
# File 'lib/guard/templates.rb', line 24

def start
  run_all
end

#stopObject

Called when ‘stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).

Raises:

  • (:task_has_failed)

    when stop has failed



30
31
# File 'lib/guard/templates.rb', line 30

def stop
end