Class: Guard::EJS
Instance Method Summary collapse
-
#initialize(watchers = [], options = {}) ⇒ EJS
constructor
A new instance of EJS.
-
#run_all ⇒ Object
Compile all templates.
- #run_on_additions(paths) ⇒ Object
-
#run_on_changes(paths) ⇒ Object
Run when the guardfile changes.
-
#run_on_modifications(paths) ⇒ Object
Compile each template at the passed in paths.
- #run_on_removals(paths) ⇒ Object
- #start ⇒ Object
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 = [], = {}) super = { run_on_start: true, namespace: 'app', input: 'public/templates', output: 'public/compiled/compiled.js' }.update() end |
Instance Method Details
#run_all ⇒ Object
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([:output]) File.open([: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 |
#start ⇒ Object
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 [:run_on_start] end |