class Guard::Jst

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method
# File lib/guard/jst.rb, line 7
def initialize(options = {})
  super
  @options = { extnames: %w[jst ejs jst.ejs] }.merge options

  @output = Pathname.new @options[:output]
  @input  = Pathname.new @options[:input]
end

Public Instance Methods

run_all() click to toggle source

Compile all templates.

# File lib/guard/jst.rb, line 22
def run_all
  paths = Dir.glob("#{@options[:input]}/**/*#{extnames}").select { |path| not File.directory? path }
  run_on_modifications paths
end
run_on_additions(paths) click to toggle source
# File lib/guard/jst.rb, line 32
def run_on_additions(paths)
  run_on_modifications paths
end
run_on_changes(paths) click to toggle source

Run when the guardfile changes.

# File lib/guard/jst.rb, line 28
def run_on_changes(paths)
  run_on_modifications paths
end
run_on_modifications(paths = []) click to toggle source

Compile each template at the passed in paths.

# File lib/guard/jst.rb, line 37
    def run_on_modifications(paths = [])
      paths
        .select { |path| @options[:extnames].include? path.scan(/\.(.+)/).flatten[0] }
        .map { |path| Pathname path }
        .uniq.each do |path|
          relative_path = path.relative_path_from @input

          UI.info "[JST] COMPILE: #{path}"

          File.open(@output.join(relative_path).tap { |target| target.dirname.mkpath }.to_s.sub(/\..+/, '.js'), 'w+') do |file|
            file.write <<-JS
(function() {
  this.JST || (this.JST = {});
  this.JST["#{relative_path.to_s.sub(/\..+/, '')}"] = #{EJS.compile path.read}
}).call(this)
          JS
          end
        end
    end
run_on_removals(paths) click to toggle source
# File lib/guard/jst.rb, line 57
def run_on_removals(paths)
  paths.each do |path|
    UI.info "[JST] REMOVE: #{path}"
    File.delete @output.join(Pathname.new(path).relative_path_from(@input)).to_s.sub(/\..+/, '.js')
  end
end
start() click to toggle source
# File lib/guard/jst.rb, line 15
def start
  UI.info 'Guard::Jst is now watching for changes.'
  # Run all if the option is true.
  run_all if @options[:run_on_start]
end

Private Instance Methods

extnames() click to toggle source
# File lib/guard/jst.rb, line 66
def extnames
  "{#{@options[:extnames].map { |ext| ".#{ext}" }.join(',')}}"
end