Class: Guard::EJS

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/ejs.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of EJS.



7
8
9
10
11
12
13
14
15
# File 'lib/guard/ejs.rb', line 7

def initialize(watchers = [], options = {})
  super
  @options = {
    run_on_start: true,
    namespace: 'app',
    input: 'public/templates',
    output: 'public/compiled/compiled.js'
  }.update(options)
end

Instance Method Details

#run_allObject

Compile all templates.



25
26
27
28
29
30
31
32
# File 'lib/guard/ejs.rb', line 25

def run_all
  # Get all files.
  files = Dir.glob("#{@options[:input]}/**/*").select do |path|
    not File.directory? path
  end
  
  run_on_modifications files
end

#run_on_additions(paths) ⇒ Object



39
40
41
# File 'lib/guard/ejs.rb', line 39

def run_on_additions(paths)
  run_on_modifications paths
end

#run_on_changes(paths) ⇒ Object

Run when the guardfile changes.



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

def run_on_changes(paths)
  puts 'guardfile changed'
end

#run_on_modifications(paths) ⇒ Object

Compile each template at the passed in paths.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/guard/ejs.rb', line 44

def run_on_modifications(paths)
  hash = {}
  
  puts "paths: #{paths.inspect}"
  paths.each do |path|
    file = File.read path
    compiled = ::EJS.compile file
    hash[path] = compiled
  end
  
  # Just overwrite the whole thing for now.
  FileUtils.mkdir_p File.dirname(@options[:output])
  
  File.open(@options[:output], 'w+') do |file|
    file.write "window.#{@options[:namespace]} = window.#{@options[:namespace]} || {}\n"
    file.write "window.#{@options[:namespace]}.templates = {"
    
    hash.each do |name, template|
      file.write "\"#{name}\": #{template},"
    end
    
    file.write "}"
  end
end

#run_on_removals(paths) ⇒ Object



69
70
71
# File 'lib/guard/ejs.rb', line 69

def run_on_removals(paths)
  puts 'run on removals'
end

#startObject



17
18
19
20
21
22
# File 'lib/guard/ejs.rb', line 17

def start
  UI.info 'Guard::EJS is now watching at somewhere'
  
  # Run all if the option is true.
  run_all() if @options[:run_on_start]
end