class Kamaze::Project::Tools::Yardoc::Watcher

Provide a watcher built on top of Yardoc

Attributes

options[RW]

@type [Hash]

paths[W]

@type [Array<String>]

patterns[W]

@type [Array<String>]

yardoc[RW]

@return [Kamaze::Project::Tools::Yardoc]

Public Instance Methods

mutable_attributes() click to toggle source
# File lib/kamaze/project/tools/yardoc/watcher.rb, line 55
def mutable_attributes
  [:yardoc, :paths, :options, :patterns]
end
paths() click to toggle source

@return [Array<String>]

# 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
patterns() click to toggle source

@return [Array<String>]

# File lib/kamaze/project/tools/yardoc/watcher.rb, line 44
def patterns
  @patterns.map { |pattern| pattern.gsub(%r{^./+}, '') }
end
watch(wait = false) click to toggle source

Watch for changes

Non-blocking, unless “wait“ (bool)

@param [Boolean] wait @return [self]

# 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

Protected Instance Methods

relative(*paths) click to toggle source

Transform paths to relative paths

@param [String|Pathname|Array<String>] paths

# 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
setup() click to toggle source
# 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) click to toggle source

Denote paths trigger (require) action

@param [String|Pathname|Array<String>] paths @return [Boolean]

# 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