Class: Hike::CachedTrail

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/hike/cached_trail.rb

Overview

CachedTrail is an internal cached variant of Trail. It assumes the file system does not change between find calls. All stat and entries calls are cached for the lifetime of the CachedTrail object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, paths, extensions, aliases) ⇒ CachedTrail

CachedTrail.new is an internal method. Instead of constructing it directly, create a Trail and call Trail#CachedTrail.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hike/cached_trail.rb', line 20

def initialize(root, paths, extensions, aliases)
  @root = root.to_s

  # Freeze is used here so an error is throw if a mutator method
  # is called on the array. Mutating `@paths`, `@extensions`, or
  # `@aliases` would have unpredictable results.
  @paths      = paths.dup.freeze
  @extensions = extensions.dup.freeze

  # Create a reverse mapping from extension to possible aliases.
  @aliases = aliases.dup.freeze
  @reverse_aliases = @aliases.inject({}) { |h, (k, a)|
    (h[a] ||= []) << k; h
  }

  @stats    = Hash.new { |h, k| h[k] = FileUtils.stat(k) }
  @entries  = Hash.new { |h, k| h[k] = FileUtils.entries(k) }
  @patterns = Hash.new { |h, k| h[k] = pattern_for(k) }
end

Instance Attribute Details

#aliasesObject (readonly)

CachedTrail#aliases is an immutable Hash mapping an extension to an Array of aliases.



16
17
18
# File 'lib/hike/cached_trail.rb', line 16

def aliases
  @aliases
end

#extensionsObject (readonly)

CachedTrail#extensions is an immutable Extensions collection.



12
13
14
# File 'lib/hike/cached_trail.rb', line 12

def extensions
  @extensions
end

#pathsObject (readonly)

CachedTrail#paths is an immutable Paths collection.



9
10
11
# File 'lib/hike/cached_trail.rb', line 9

def paths
  @paths
end

#rootObject (readonly)

CachedTrail#root returns root path as a String. This attribute is immutable.



41
42
43
# File 'lib/hike/cached_trail.rb', line 41

def root
  @root
end

Instance Method Details

#cachedObject Also known as: index

CachedTrail#cached returns self to be compatable with the Trail interface.



44
45
46
# File 'lib/hike/cached_trail.rb', line 44

def cached
  self
end

#entries(path) ⇒ Object

A cached version of Dir.entries that filters out . files and ~ swap files. Returns an empty Array if the directory does not exist.



85
86
87
# File 'lib/hike/cached_trail.rb', line 85

def entries(path)
  @entries[path]
end

#find(*logical_paths) ⇒ Object

The real implementation of find. Trail#find generates a one time cache and delegates here.

See Trail#find for usage.



55
56
57
# File 'lib/hike/cached_trail.rb', line 55

def find(*logical_paths)
  find_all(*logical_paths).first
end

#find_all(*logical_paths, &block) ⇒ Object

The real implementation of find_all. Trail#find_all generates a one time index and delegates here.

See Trail#find_all for usage.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hike/cached_trail.rb', line 63

def find_all(*logical_paths, &block)
  return to_enum(__method__, *logical_paths) unless block_given?

  options = extract_options!(logical_paths)
  base_path = (options[:base_path] || root).to_s

  logical_paths.each do |logical_path|
    logical_path = logical_path.sub(/^\//, '')

    if relative?(logical_path)
      find_in_base_path(logical_path, base_path, &block)
    else
      find_in_paths(logical_path, &block)
    end
  end

  nil
end

#stat(path) ⇒ Object

A cached version of File.stat. Returns nil if the file does not exist.



91
92
93
# File 'lib/hike/cached_trail.rb', line 91

def stat(path)
  @stats[path]
end