Class: Hike::CachedTrail
- Inherits:
-
Object
- Object
- Hike::CachedTrail
- 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
-
#aliases ⇒ Object
readonly
‘CachedTrail#aliases` is an immutable `Hash` mapping an extension to an `Array` of aliases.
-
#extensions ⇒ Object
readonly
‘CachedTrail#extensions` is an immutable `Extensions` collection.
-
#paths ⇒ Object
readonly
‘CachedTrail#paths` is an immutable `Paths` collection.
Instance Method Summary collapse
-
#cached ⇒ Object
(also: #index)
‘CachedTrail#cached` returns `self` to be compatable with the `Trail` interface.
-
#entries(path) ⇒ Object
A cached version of ‘Dir.entries` that filters out `.` files and `~` swap files.
-
#find(*logical_paths) ⇒ Object
The real implementation of ‘find`.
-
#find_all(*logical_paths, &block) ⇒ Object
The real implementation of ‘find_all`.
-
#initialize(root, paths, extensions, aliases) ⇒ CachedTrail
constructor
‘CachedTrail.new` is an internal method.
-
#root ⇒ Object
‘CachedTrail#root` returns root path as a `String`.
-
#stat(path) ⇒ Object
A cached version of ‘File.stat`.
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`.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/hike/cached_trail.rb', line 22 def initialize(root, paths, extensions, aliases) @root = root # 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 @aliases = aliases.inject({}) { |h, (k, a)| h[k] = a.dup.freeze; h }.freeze @pathnames = paths.map { |path| Pathname.new(path) } @stats = {} @entries = {} @patterns = {} end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
‘CachedTrail#aliases` is an immutable `Hash` mapping an extension to an `Array` of aliases.
18 19 20 |
# File 'lib/hike/cached_trail.rb', line 18 def aliases @aliases end |
#extensions ⇒ Object (readonly)
‘CachedTrail#extensions` is an immutable `Extensions` collection.
14 15 16 |
# File 'lib/hike/cached_trail.rb', line 14 def extensions @extensions end |
#paths ⇒ Object (readonly)
‘CachedTrail#paths` is an immutable `Paths` collection.
11 12 13 |
# File 'lib/hike/cached_trail.rb', line 11 def paths @paths end |
Instance Method Details
#cached ⇒ Object Also known as: index
‘CachedTrail#cached` returns `self` to be compatable with the `Trail` interface.
46 47 48 |
# File 'lib/hike/cached_trail.rb', line 46 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.
87 88 89 |
# File 'lib/hike/cached_trail.rb', line 87 def entries(path) @entries[path.to_s] ||= super 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.
57 58 59 |
# File 'lib/hike/cached_trail.rb', line 57 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.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/hike/cached_trail.rb', line 65 def find_all(*logical_paths, &block) return to_enum(__method__, *logical_paths) unless block_given? = (logical_paths) base_path = Pathname.new([:base_path] || @root) logical_paths.each do |logical_path| logical_path = Pathname.new(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 |
#root ⇒ Object
‘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.to_s end |
#stat(path) ⇒ Object
A cached version of ‘File.stat`. Returns nil if the file does not exist.
93 94 95 96 97 98 99 100 |
# File 'lib/hike/cached_trail.rb', line 93 def stat(path) key = path.to_s if @stats.key?(key) @stats[key] else @stats[key] = super end end |