Class: ActiveSupport::EventedFileUpdateChecker::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/evented_file_update_checker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, dirs) ⇒ Core

Returns a new instance of Core.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/evented_file_update_checker.rb', line 71

def initialize(files, dirs)
  @files = files.map { |file| Pathname(file).expand_path }.to_set

  @dirs = dirs.each_with_object({}) do |(dir, exts), hash|
    hash[Pathname(dir).expand_path] = Array(exts).map { |ext| ext.to_s.sub(/\A\.?/, ".") }.to_set
  end

  @common_path = common_path(@dirs.keys)

  @dtw = directories_to_watch
  @missing = []

  @updated = Concurrent::AtomicBoolean.new(false)
  @mutex = Mutex.new

  start
  @after_fork = ActiveSupport::ForkTracker.after_fork { start }
end

Instance Attribute Details

#updatedObject (readonly)

Returns the value of attribute updated.



69
70
71
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/evented_file_update_checker.rb', line 69

def updated
  @updated
end

Instance Method Details

#changed(modified, added, removed) ⇒ Object



129
130
131
132
133
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/evented_file_update_checker.rb', line 129

def changed(modified, added, removed)
  unless @updated.true?
    @updated.make_true if (modified + added + removed).any? { |f| watching?(f) }
  end
end

#common_path(paths) ⇒ Object



163
164
165
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/evented_file_update_checker.rb', line 163

def common_path(paths)
  paths.map { |path| path.ascend.to_a }.reduce(&:&)&.first
end

#directories_to_watchObject



157
158
159
160
161
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/evented_file_update_checker.rb', line 157

def directories_to_watch
  dtw = @dirs.keys | @files.map(&:dirname)
  accounted_for = dtw.to_set + Gem.path.map { |path| Pathname(path) }
  dtw.reject { |dir| dir.ascend.drop(1).any? { |parent| accounted_for.include?(parent) } }
end

#finalizerObject



90
91
92
93
94
95
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/evented_file_update_checker.rb', line 90

def finalizer
  proc do
    stop
    ActiveSupport::ForkTracker.unregister(@after_fork)
  end
end

#normalize_dirs!Object



123
124
125
126
127
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/evented_file_update_checker.rb', line 123

def normalize_dirs!
  @dirs.transform_keys! do |dir|
    dir.exist? ? dir.realpath : dir
  end
end

#restartObject



114
115
116
117
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/evented_file_update_checker.rb', line 114

def restart
  stop
  start
end

#restart?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/evented_file_update_checker.rb', line 119

def restart?
  @missing.any?(&:exist?)
end

#startObject



103
104
105
106
107
108
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/evented_file_update_checker.rb', line 103

def start
  normalize_dirs!
  @dtw, @missing = [*@dtw, *@missing].partition(&:exist?)
  @listener = @dtw.any? ? Listen.to(*@dtw, &method(:changed)) : nil
  @listener&.start
end

#stopObject



110
111
112
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/evented_file_update_checker.rb', line 110

def stop
  @listener&.stop
end

#thread_safelyObject



97
98
99
100
101
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/evented_file_update_checker.rb', line 97

def thread_safely
  @mutex.synchronize do
    yield self
  end
end

#watching?(file) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/evented_file_update_checker.rb', line 135

def watching?(file)
  file = Pathname(file)

  if @files.member?(file)
    true
  elsif file.directory?
    false
  else
    ext = file.extname

    file.dirname.ascend do |dir|
      matching = @dirs[dir]

      if matching && (matching.empty? || matching.include?(ext))
        break true
      elsif dir == @common_path || dir.root?
        break false
      end
    end
  end
end