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,
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>
Instance Method Summary collapse
- #mutable_attributes ⇒ Object
-
#rel(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.
- #yardoc ⇒ Kamaze::Project::Tools::Yardoc protected
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>
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 |
#patterns ⇒ 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_attributes ⇒ Object
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
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 |
#setup ⇒ Object (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
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
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, ) do |mod, add, rem| if trigger?(mod + add + rem) yardoc.run end end listener.start sleep if wait self end |
#yardoc ⇒ Kamaze::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 |