Class: Listen::DirectoryRecord

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ DirectoryRecord

Initializes a directory record.

Parameters:

  • [String] (Hash)

    a customizable set of options

Raises:

  • (ArgumentError)


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

#directoryObject (readonly)

Returns the value of attribute directory.



13
14
15
# File 'lib/listen/directory_record.rb', line 13

def directory
  @directory
end

#pathsObject (readonly)

Returns the value of attribute paths.



13
14
15
# File 'lib/listen/directory_record.rb', line 13

def paths
  @paths
end

#sha1_checksumsObject (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

#buildObject

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

Parameters:

  • directories (Array)

    the list of directories scan for changes

  • options (Hash) (defaults to: {})

Options Hash (options):

  • recursive (Boolean)

    scan all sub-directories recursively

  • relative_paths (Boolean)

    whether or not to use relative paths for changes

Returns:

  • (Hash<Array>)

    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, options = {})
  @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, options)
    detect_additions(directory, options)
  end
  @updated_at = Time.now.to_i
  @changes
end

#filter(*regexps) ⇒ Listen::Listener

Adds file filters to the listener.

Examples:

Filter some files

ignore /\.txt$/, /.*\.zip/

Parameters:

  • regexps (Array<Regexp>)

    a list of regexps file filters

Returns:



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.

Parameters:

  • path (String)

    the path to test.

Returns:

  • (Boolean)


88
89
90
# File 'lib/listen/directory_record.rb', line 88

def filtered?(path)
  @filters.empty? || @filters.any? { |filter| path =~ filter }
end

#filtersArray<String>

Returns the filters used in the record to know which paths should be stored.

Returns:

  • (Array<String>)

    the used filters



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.

Examples:

Ignore some paths

ignore ".git", ".svn"

Parameters:

  • paths (String, Array<String>)

    a path or a list of paths to ignore



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.

Parameters:

  • path (String)

    the path to test.

Returns:

  • (Boolean)


78
79
80
# File 'lib/listen/directory_record.rb', line 78

def ignored?(path)
  @ignored_paths.any? { |ignored_path| path =~ /#{ignored_path}$/ }
end

#ignored_pathsArray<String>

Returns the ignored paths in the record

Returns:

  • (Array<String>)

    the ignored paths



35
36
37
# File 'lib/listen/directory_record.rb', line 35

def ignored_paths
  @ignored_paths.to_a
end