Class: Guard::MtHaml

Inherits:
Plugin
  • Object
show all
Defined in:
lib/guard/mthaml.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MtHaml

Initializer



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/guard/mthaml.rb', line 12

def initialize(options = {})
    options = {
        input: "views/src",
        output: "views",
        environment: "php",
        extension: nil,
        notifications: true,
        compress_output: false,
        static_files: false,
        run_at_start: true
    }.merge(options)

    # Define extension if environment is nil
    if options[:extension].nil?
        options[:extension] = if options[:static_files] then "html" else options[:environment] end
    end

    super(options)

    if options[:input]
        watchers << ::Guard::Watcher.new(%r{^#{options[:input]}/(.+\.haml)$})
    end
end

Instance Method Details

#reloadObject

On Guard reload



53
54
55
# File 'lib/guard/mthaml.rb', line 53

def reload
    run_all
end

#run_allObject

Run all



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

def run_all
    run_on_changes Watcher.match_files(self, Dir.glob(File.join("**", "*.*")).reject { |f| f[%r{(\.php)$}] })
end

#run_on_changes(paths) ⇒ Object

Run on changes to watched files

Parameters:

  • paths (Array)

    Paths of changed files



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/guard/mthaml.rb', line 70

def run_on_changes(paths)
    paths.each do |file|

        file = Pathname.new(file)
        file_dir = file.dirname.realpath
        input_dir = Pathname.new(options[:input]).realpath
        input_file = file.realpath

        # Simple check to see if we need to create any directories in the output
        if file_dir == input_dir
            # File looks like it's in the base directory
            output_dir = Pathname.new(options[:output]).realpath
        else
            # Looks like we need to create a directory or two
            output_dir = Pathname.new(options[:output]).realpath + file_dir.to_s.gsub(input_dir.to_s, "")[1..-1]
        end

        # Make directories if they don't already exist
        make_directory(output_dir)

        # Initiate compiler
        compile_haml(input_file, output_dir)
    end
end

#run_on_removals(paths) ⇒ Object

Called when a watched file is removed

Parameters:

  • paths (Array)

    Paths of changed files



101
102
# File 'lib/guard/mthaml.rb', line 101

def run_on_removals(paths)
end

#startObject

Run at start



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

def start
    run_all if options[:run_at_start]
end

#stopObject

Stop running



46
47
48
# File 'lib/guard/mthaml.rb', line 46

def stop
    true
end