Class: Guard::Treetop

Inherits:
Guard
  • Object
show all
Includes:
TreetopVersion
Defined in:
lib/guard/treetop.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :all_on_start => false
}

Constants included from TreetopVersion

Guard::TreetopVersion::VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Treetop.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/guard/treetop.rb', line 16

def initialize(watchers=[], options={})
  watchers ||= []
  defaults = DEFAULT_OPTIONS.clone

  if options[:input]
    input = options.delete(:input)
    defaults.merge!({:output => input})
    watchers << ::Guard::Watcher.new(%r{#{input}/(.+)(\.treetop|\.tt)})
  end

  super(watchers, defaults.merge!(options))

  @compiler = ::Treetop::Compiler::GrammarCompiler.new
end

Instance Attribute Details

#compilerObject (readonly)

Returns the value of attribute compiler.



14
15
16
# File 'lib/guard/treetop.rb', line 14

def compiler
  @compiler
end

Instance Method Details

#run_allObject



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

def run_all
  run_on_changes(Watcher.match_files(self, Dir.glob('**/*.{tt,treetop}')))
end

#run_on_changes(paths) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/guard/treetop.rb', line 39

def run_on_changes(paths)
  tuples = paths.map do |path|
    basename = File.basename(path).gsub(/\.(tt|treetop)\z/, '.rb')
    dirname = options[:output] || File.dirname(path)
    output = File.join(dirname, basename)
    [path, output]
  end
  tuples.each { |e| compiler.compile(*e) }
rescue StandardError
  throw :task_has_failed
end

#startObject



31
32
33
# File 'lib/guard/treetop.rb', line 31

def start
  run_all if options[:all_on_start]
end