Class: Listen::DirectoryRecord
- Inherits:
-
Object
- Object
- Listen::DirectoryRecord
- Defined in:
- lib/listen/directory_record.rb
Overview
The directory record stores information about a directory and keeps track of changes to the structure of its childs.
Constant Summary collapse
- DEFAULT_IGNORED_PATHS =
Default paths’ beginnings that doesn’t get stored in the record
%w[.bundle .git .DS_Store log tmp vendor]
Instance Attribute Summary collapse
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
-
#sha1_checksums ⇒ Object
readonly
Returns the value of attribute sha1_checksums.
Instance Method Summary collapse
-
#build ⇒ Object
Finds the paths that should be stored and adds them to the paths’ hash.
-
#fetch_changes(directories, options = {}) ⇒ Hash<Array>
Detects changes in the passed directories, updates the record with the new changes and returns the changes.
-
#filter(*regexps) ⇒ Listen::Listener
Adds file filters to the listener.
-
#filtered?(path) ⇒ Boolean
Returns whether a path should be filtered or not.
-
#filters ⇒ Array<String>
Returns the filters used in the record to know which paths should be stored.
-
#ignore(*paths) ⇒ Object
Adds ignored path to the record.
-
#ignored?(path) ⇒ Boolean
Returns whether a path should be ignored or not.
-
#ignored_paths ⇒ Array<String>
Returns the ignored paths in the record.
-
#initialize(directory) ⇒ DirectoryRecord
constructor
Initializes a directory record.
Constructor Details
#initialize(directory) ⇒ DirectoryRecord
Initializes a directory record.
22 23 24 25 26 27 28 29 |
# File 'lib/listen/directory_record.rb', line 22 def initialize(directory) @directory = directory raise ArgumentError, "The path '#{directory}' is not a directory!" unless File.directory?(@directory) @ignored_paths = Set.new(DEFAULT_IGNORED_PATHS) @filters = Set.new @sha1_checksums = Hash.new end |
Instance Attribute Details
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
13 14 15 |
# File 'lib/listen/directory_record.rb', line 13 def directory @directory end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
13 14 15 |
# File 'lib/listen/directory_record.rb', line 13 def paths @paths end |
#sha1_checksums ⇒ Object (readonly)
Returns the value of attribute sha1_checksums.
13 14 15 |
# File 'lib/listen/directory_record.rb', line 13 def sha1_checksums @sha1_checksums end |
Instance Method Details
#build ⇒ Object
Finds the paths that should be stored and adds them to the paths’ hash.
95 96 97 98 99 |
# File 'lib/listen/directory_record.rb', line 95 def build @paths = Hash.new { |h, k| h[k] = Hash.new } important_paths { |path| insert_path(path) } @updated_at = Time.now.to_i end |
#fetch_changes(directories, options = {}) ⇒ Hash<Array>
Detects changes in the passed directories, updates the record with the new changes and returns the changes
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/listen/directory_record.rb', line 111 def fetch_changes(directories, = {}) @changes = { :modified => [], :added => [], :removed => [] } directories = directories.sort_by { |el| el.length }.reverse # diff sub-dir first directories.each do |directory| next unless directory[@directory] # Path is or inside directory detect_modifications_and_removals(directory, ) detect_additions(directory, ) end @updated_at = Time.now.to_i @changes end |
#filter(*regexps) ⇒ Listen::Listener
Adds file filters to the listener.
68 69 70 |
# File 'lib/listen/directory_record.rb', line 68 def filter(*regexps) @filters.merge(regexps) end |
#filtered?(path) ⇒ Boolean
Returns whether a path should be filtered or not.
88 89 90 |
# File 'lib/listen/directory_record.rb', line 88 def filtered?(path) @filters.empty? || @filters.any? { |filter| path =~ filter } end |
#filters ⇒ Array<String>
Returns the filters used in the record to know which paths should be stored.
44 45 46 |
# File 'lib/listen/directory_record.rb', line 44 def filters @filters.to_a end |
#ignore(*paths) ⇒ Object
Adds ignored path to the record.
55 56 57 |
# File 'lib/listen/directory_record.rb', line 55 def ignore(*paths) @ignored_paths.merge(paths) end |
#ignored?(path) ⇒ Boolean
Returns whether a path should be ignored or not.
78 79 80 |
# File 'lib/listen/directory_record.rb', line 78 def ignored?(path) @ignored_paths.any? { |ignored_path| path =~ /#{ignored_path}$/ } end |
#ignored_paths ⇒ Array<String>
Returns the ignored paths in the record
35 36 37 |
# File 'lib/listen/directory_record.rb', line 35 def ignored_paths @ignored_paths.to_a end |