Class: Kamaze::Project::Tools::Yardoc::Watcher
- Inherits:
-
BaseTool
- Object
- Observable
- BaseTool
- Kamaze::Project::Tools::Yardoc::Watcher
- Defined in:
- lib/kamaze/project/tools/yardoc/watcher.rb
Overview
Provide a watcher built on top of Yardoc
Instance Attribute Summary collapse
- #observer_peers ⇒ Hash|nil included from Concern::Observable readonly protected
- #options ⇒ Object
- #paths ⇒ Array<String>
- #patterns ⇒ Array<String>
- #yardoc ⇒ Kamaze::Project::Tools::Yardoc
Instance Method Summary collapse
- #mutable_attributes ⇒ Object
-
#relative(*paths) ⇒ Object
protected
Transform paths to relative paths.
- #setup ⇒ Object protected
-
#trigger?(*paths) ⇒ Boolean
protected
Denote paths trigger (require) action.
-
#watch(wait = false) ⇒ self
Watch for changes.
Constructor Details
This class inherits a constructor from Kamaze::Project::Tools::BaseTool
Instance Attribute Details
#observer_peers ⇒ Hash|nil (readonly, protected) Originally defined in module Concern::Observable
#options ⇒ Object
22 23 24 |
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 22 def @options end |
#paths ⇒ 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 |
#patterns ⇒ Array<String>
44 45 46 |
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 44 def patterns @patterns.map { |pattern| pattern.gsub(%r{^./+}, '') } end |
#yardoc ⇒ Kamaze::Project::Tools::Yardoc
16 17 18 |
# File 'lib/kamaze/project/tools/yardoc/watcher.rb', line 16 def yardoc @yardoc end |
Instance Method Details
#mutable_attributes ⇒ Object
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
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 |
#setup ⇒ Object (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
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)
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, ) do |mod, add, rem| yardoc.run if trigger?(*mod.concat(add).concat(rem)) end.tap(&:start) sleep if wait end end |