Class: Guard::Haml2erb

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

Instance Method Summary collapse

Constructor Details

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

Initialize a Guard.

Parameters:

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

    the Guard file watchers

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

    the custom Guard options



10
11
12
# File 'lib/guard/haml2erb.rb', line 10

def initialize(watchers = [], options = {})
  super
end

Instance Method Details

#compile_haml2erb(file) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/guard/haml2erb.rb', line 64

def compile_haml2erb(file)
  template = File.read(file)
  output = ::Haml2Erb.convert(template)
rescue StandardError => error
  ::Guard::UI.error "haml2erb error: " + error.message
  throw :task_has_failed
end

#get_output(file) ⇒ String

Get the file path to output the erb based on the file being built. The output path is relative to where guard is being run.

Parameters:

  • file (String)

    path to file being built

Returns:

  • (String)

    path to file where output should be written



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/guard/haml2erb.rb', line 20

def get_output(file)
  file_dir = File.dirname(file)
  file_name = File.basename(file).split('.')[0..-2].join('.')

  file_name = "#{file_name}.erb" if file_name.match(/\.erb/).nil?

  file_dir = file_dir.gsub(Regexp.new("#{@options[:input]}(\/){0,1}"), '') if @options[:input]
  file_dir = File.join(@options[:output], file_dir) if @options[:output]

  if file_dir == ''
    file_name
  else
    File.join(file_dir, file_name)
  end
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



40
41
42
# File 'lib/guard/haml2erb.rb', line 40

def run_all
  run_on_changes(Watcher.match_files(self, Dir.glob(File.join('**', '*.*'))))
end

#run_on_changes(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



47
48
49
50
51
52
53
54
55
56
# File 'lib/guard/haml2erb.rb', line 47

def run_on_changes(paths)
  paths.each do |file|
    output_file = get_output(file)
    FileUtils.mkdir_p File.dirname(output_file)
    File.open(output_file, 'w') { |f| f.write(compile_haml2erb(file)) }
    ::Guard::UI.info "# compiled haml in '#{file}' to erb in '#{output_file}'"
    ::Guard::Notifier.notify("# compiled haml to erb in #{file}", :title => "Guard::Haml2erb", :image => :success) if @options[:notifications]
  end
  notify paths
end

#run_on_removals(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



61
62
# File 'lib/guard/haml2erb.rb', line 61

def run_on_removals(paths)
end