Class: Glimmer::Gladiator::Dir
- Inherits:
-
Object
- Object
- Glimmer::Gladiator::Dir
- Includes:
- Glimmer, DataBinding::ObservableModel
- Defined in:
- lib/models/glimmer/gladiator/dir.rb
Constant Summary collapse
- IGNORE_PATHS =
['.gladiator', '.gladiator-scratchpad', '.git', 'coverage', 'packages', 'node_modules', 'tmp', 'vendor', 'pkg', 'dist', 'log', 'test/reports']
- REGEXP_FILTER =
/[:_\/.-]/
Instance Attribute Summary collapse
- #all_children ⇒ Object
-
#children ⇒ Object
Returns the value of attribute children.
-
#display_path ⇒ Object
Returns the value of attribute display_path.
-
#filter ⇒ Object
Returns the value of attribute filter.
-
#filtered_path ⇒ Object
Returns the value of attribute filtered_path.
-
#filtered_path_options ⇒ Object
Returns the value of attribute filtered_path_options.
-
#ignore_paths ⇒ Object
Returns the value of attribute ignore_paths.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#path ⇒ Object
Returns the value of attribute path.
-
#selected_child ⇒ Object
Returns the value of attribute selected_child.
Instance Method Summary collapse
- #all_children_files ⇒ Object
- #close ⇒ Object
- #collect_all_children ⇒ Object
- #depth_first_search_file(dir, file_path) ⇒ Object
- #eql?(other) ⇒ Boolean
- #filtered ⇒ Object
- #find_child_file(child_path) ⇒ Object
- #generate_display_path ⇒ Object
- #hash ⇒ Object
-
#initialize(path, project_dir = nil) ⇒ Dir
constructor
A new instance of Dir.
- #is_local_dir ⇒ Object
- #pause_refresh ⇒ Object
- #project_dir ⇒ Object
- #refresh(async: true, force: false) ⇒ Object
- #remove! ⇒ Object
- #resume_refresh ⇒ Object
- #retrieve_children ⇒ Object
- #selected_child_path ⇒ Object
- #selected_child_path=(selected_path) ⇒ Object
- #selected_child_path_history ⇒ Object
- #stop_filewatcher ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(path, project_dir = nil) ⇒ Dir
Returns a new instance of Dir.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 37 def initialize(path, project_dir = nil) @project_dir = project_dir if is_local_dir @filewatcher = Filewatcher.new(path) @filewatcher_thread = Thread.new(@filewatcher) do |fw| begin fw.watch do |filename, event| # TODO do fine grained processing of events for enhanced performance (e.g. dir refresh vs file change) # TODO do fine grained file change only without a refresh delay for enhanced performance begin if !@refresh_in_progress && !filename.include?('new_file') && !ignore_paths.any? { |ignore_path| filename.include?(ignore_path) } && (event != :updated || find_child_file(filename).nil?) Thread.new { refresh } end rescue => e puts e. end end rescue => e puts e. end end end self.path = ::File.(path) @name = ::File.basename(::File.(path)) @ignore_paths = IGNORE_PATHS self. = [] end |
Instance Attribute Details
#all_children ⇒ Object
192 193 194 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 192 def all_children @all_children ||= collect_all_children end |
#children ⇒ Object
Returns the value of attribute children.
33 34 35 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 33 def children @children end |
#display_path ⇒ Object
Returns the value of attribute display_path.
33 34 35 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 33 def display_path @display_path end |
#filter ⇒ Object
Returns the value of attribute filter.
33 34 35 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 33 def filter @filter end |
#filtered_path ⇒ Object
Returns the value of attribute filtered_path.
33 34 35 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 33 def filtered_path @filtered_path end |
#filtered_path_options ⇒ Object
Returns the value of attribute filtered_path_options.
33 34 35 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 33 def @filtered_path_options end |
#ignore_paths ⇒ Object
Returns the value of attribute ignore_paths.
33 34 35 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 33 def ignore_paths @ignore_paths end |
#name ⇒ Object
Returns the value of attribute name.
34 35 36 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 34 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
34 35 36 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 34 def parent @parent end |
#path ⇒ Object
Returns the value of attribute path.
34 35 36 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 34 def path @path end |
#selected_child ⇒ Object
Returns the value of attribute selected_child.
33 34 35 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 33 def selected_child @selected_child end |
Instance Method Details
#all_children_files ⇒ Object
204 205 206 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 204 def all_children_files all_children.select {|child| child.is_a?(Gladiator::File) } end |
#close ⇒ Object
133 134 135 136 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 133 def close all_children_files.each(&:close) stop_filewatcher end |
#collect_all_children ⇒ Object
196 197 198 199 200 201 202 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 196 def collect_all_children @all_children = children.reduce([]) do |output, child| addition = [child] addition += child.collect_all_children if child.is_a?(Dir) output + addition end end |
#depth_first_search_file(dir, file_path) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 117 def depth_first_search_file(dir, file_path) dir.children.each do |child| if child.is_a?(File) return child if child.path.include?(file_path.to_s) else result = depth_first_search_file(child, file_path) return result unless result.nil? end end nil end |
#eql?(other) ⇒ Boolean
256 257 258 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 256 def eql?(other) self.path.eql?(other&.path) end |
#filtered ⇒ Object
182 183 184 185 186 187 188 189 190 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 182 def filtered return if filter.nil? children_files = !@last_filter.to_s.empty? && filter.downcase.start_with?(@last_filter.downcase) ? @last_filtered : all_children_files children_files.select do |child| child_path = child.path.to_s.sub(project_dir.path, '') child_path.downcase.include?(filter.downcase) || child_path.downcase.gsub(REGEXP_FILTER, '').include?(filter.downcase.gsub(REGEXP_FILTER, '')) end.sort_by {|c| c.path.to_s.downcase} end |
#find_child_file(child_path) ⇒ Object
113 114 115 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 113 def find_child_file(child_path) depth_first_search_file(self, child_path) end |
#generate_display_path ⇒ Object
78 79 80 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 78 def generate_display_path is_local_dir ? path : @display_path = @path.sub(project_dir.path, '').sub(/^\//, '') end |
#hash ⇒ Object
260 261 262 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 260 def hash self.path.hash end |
#is_local_dir ⇒ Object
65 66 67 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 65 def is_local_dir @project_dir.nil? end |
#pause_refresh ⇒ Object
146 147 148 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 146 def pause_refresh @refresh_paused = true end |
#project_dir ⇒ Object
69 70 71 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 69 def project_dir @project_dir || self end |
#refresh(async: true, force: false) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 154 def refresh(async: true, force: false) return if @refresh_paused && !force @refresh_in_progress = true retrieve_children collect_all_children refresh_operation = lambda do notify_observers(:children) notify_observers(:all_children) end if async async_exec(&refresh_operation) else sync_exec(&refresh_operation) end @refresh_in_progress = false end |
#remove! ⇒ Object
248 249 250 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 248 def remove! FileUtils.rm_rf(path) end |
#resume_refresh ⇒ Object
150 151 152 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 150 def resume_refresh @refresh_paused = false end |
#retrieve_children ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 94 def retrieve_children @children = ::Dir.glob(::File.join(@path, '*')).reject do |p| # TODO make sure to configure ignore_paths in a preferences dialog if project_dir == self project_dir.ignore_paths.any? do |ignore_path| p.include?(ignore_path) end end end.map do |p| ::File.file?(p) ? File.new(p, project_dir) : Dir.new(p, project_dir) end.sort_by do |c| c.path.to_s.downcase end.sort_by do |c| c.class.name end.each do |child| child.retrieve_children if child.is_a?(Dir) end end |
#selected_child_path ⇒ Object
237 238 239 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 237 def selected_child_path @selected_child&.path end |
#selected_child_path=(selected_path) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 208 def selected_child_path=(selected_path) return (project_dir.selected_child = nil) if selected_path.nil? # scratchpad scenario if selected_path.empty? # Scratchpad @selected_child&.write_dirty_content @scratchpad = (self.selected_child = File.new('', project_dir)) if @scratchpad.nil? || @scratchpad.closed? return @scratchpad end full_selected_path = selected_path.include?(project_dir.path) ? selected_path : ::File.join(project_dir.path, selected_path) return if ::Dir.exist?(full_selected_path) || (selected_child && selected_child.path == full_selected_path) selected_path = full_selected_path if ::File.file?(selected_path) @selected_child&.write_dirty_content new_child = find_child_file(selected_path) begin unless new_child.dirty_content.nil? selected_child_path_history << new_child.path if new_child && !selected_child_path_history.include?(new_child.path) self.selected_child = new_child self.selected_child.start_filewatcher end rescue # no op end else refresh end end |
#selected_child_path_history ⇒ Object
129 130 131 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 129 def selected_child_path_history @selected_child_path_history ||= [] end |
#stop_filewatcher ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 138 def stop_filewatcher @filewatcher&.stop @filewatcher_thread&.kill @filewatcher_thread = nil @filewatcher&.finalize @filewatcher = nil end |
#to_s ⇒ Object
252 253 254 |
# File 'lib/models/glimmer/gladiator/dir.rb', line 252 def to_s path end |