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

Inherits:
BaseTool show all
Defined in:
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>)


49
50
51
52
53
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 49

def paths
  @paths.map(&:to_s).tap do |paths|
    return paths.include?('.') ? ['.'] : paths
  end
end

#patternsArray<String>

Returns:

  • (Array<String>)


44
45
46
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 44

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

#yardocKamaze::Project::Tools::Yardoc



16
17
18
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 16

def yardoc
  @yardoc
end

Instance Method Details

#mutable_attributesObject



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

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

#relative(*paths) ⇒ Object (protected)

Transform paths to relative paths

Parameters:

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


64
65
66
67
68
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 64

def relative(*paths)
  (paths.is_a?(Array) ? paths : [paths]).map do |path|
    path.to_s.gsub(%r{^#{Dir.pwd}/+}, '')
  end
end

#setupObject (protected)



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 88

def setup
  @yardoc ||= Kamaze::Project.instance.tools.fetch('yardoc')
  @paths ||= yardoc.paths
  @patterns ||= yardoc.patterns
  # @formatter:off
  @options = {
    only: /\.(rb|md)$/,
    ignore: yardoc.excluded.map { |pattern| /#{pattern}/ }
  }.merge(@options.to_h)
  # @formatter:on
end

#trigger?(*paths) ⇒ Boolean (protected)

Denote paths trigger (require) action

Parameters:

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

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 74

def trigger?(*paths)
  # @formatter:off
  paths.map(&:to_s)
       .map { |path| relative(path)[0] }
       .each do |path|
    patterns.each do |pattern|
      return true if File.fnmatch(pattern, path, File::FNM_PATHNAME)
    end
    # @formatter:on
  end

  false
end

#watch(wait = false) ⇒ self

Watch for changes

Non-blocking, unless wait (bool)

Parameters:

  • wait (Boolean) (defaults to: false)

Returns:

  • (self)


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

def watch(wait = false)
  self.tap do
    Listen.to(*paths, options) do |mod, add, rem|
      yardoc.run if trigger?(*mod.concat(add).concat(rem))
    end.tap(&:start)

    sleep if wait
  end
end