Class: Kamaze::Project::Tools::Yardoc::Watcher

Inherits:
BaseTool show all
Defined in:
lib/kamaze/project/tools/yardoc/watcher.rb,
lib/kamaze/project/tools/yardoc/watcher.rb

Overview

Provide a watcher built on top of Yardoc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Kamaze::Project::Tools::BaseTool

Instance Attribute Details

#observer_peersHash|nil (readonly, protected) Originally defined in module Concern::Observable

Returns:

  • (Hash|nil)

#optionsObject



22
23
24
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 22

def options
  @options
end

#pathsArray<String>

Returns:

  • (Array<String>)


52
53
54
55
56
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 52

def paths
  paths = @paths.map(&:to_s)

  paths.include?('.') ? ['.'] : paths
end

#patternsArray<String>

Returns:

  • (Array<String>)


47
48
49
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 47

def patterns
  @patterns.map { |pattern| pattern.gsub(%r{^./+}, '') }
end

Instance Method Details

#mutable_attributesObject



58
59
60
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 58

def mutable_attributes
  [:paths, :options, :patterns]
end

#rel(paths) ⇒ Object (protected)

Transform paths to relative paths

Parameters:

  • paths (String|Pathname|Array<String>)


67
68
69
70
71
72
73
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 67

def rel(paths)
  paths = [paths] unless paths.is_a?(Array)

  paths.map do |path|
    path.to_s.gsub(%r{^#{Dir.pwd}/+}, '')
  end
end

#setupObject (protected)



93
94
95
96
97
98
99
100
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 93

def setup
  @paths ||= yardoc.paths
  @patterns ||= yardoc.patterns
  @options = {
    only:   /\.(rb|md)$/,
    ignore: yardoc.excluded.map { |pattern| /#{pattern}/ }
  }.merge(@options.to_h)
end

#trigger?(paths) ⇒ Boolean (protected)

Denote paths trigger (require) action

Parameters:

  • paths (String|Pathname|Array<String>)

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 79

def trigger?(paths)
  paths = [paths] unless paths.is_a?(Array)

  paths.map(&:to_s)
       .map { |path| rel(path)[0] }
       .each do |path|
    patterns.each do |pattern|
      return true if File.fnmatch(pattern, path, File::FNM_PATHNAME)
    end
  end

  false
end

#watch(wait = false) ⇒ self

Watch for changes

Non-blocking, unless wait

Parameters:

  • wait (Boolean) (defaults to: false)

Returns:

  • (self)


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 33

def watch(wait = false)
  listener = ::Listen.to(*paths, options) do |mod, add, rem|
    if trigger?(mod + add + rem)
      yardoc.run
    end
  end

  listener.start
  sleep if wait

  self
end

#yardocKamaze::Project::Tools::Yardoc (protected)



103
104
105
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 103

def yardoc
  Kamaze.project.tools.fetch('yardoc')
end