Class: Hike::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/hike/index.rb

Overview

‘Index` 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 `Index` object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, paths, extensions) ⇒ Index

‘Index.new` is an internal method. Instead of constructing it directly, create a `Trail` and call `Trail#index`.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hike/index.rb', line 16

def initialize(root, paths, extensions)
  @root = root

  # Freeze is used here so an error is throw if a mutator method
  # is called on the array. Mutating `@paths` or `@extensions`
  # would have unpredictable results.
  @paths      = paths.dup.freeze
  @extensions = extensions.dup.freeze
  @pathnames  = paths.map { |path| Pathname.new(path) }

  @stats    = {}
  @entries  = {}
  @patterns = {}
end

Instance Attribute Details

#extensionsObject (readonly)

‘Index#extensions` is an immutable `Extensions` collection.



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

def extensions
  @extensions
end

#pathsObject (readonly)

‘Index#paths` is an immutable `Paths` collection.



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

def paths
  @paths
end

Instance Method Details

#entries(path) ⇒ Object

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



70
71
72
73
74
75
# File 'lib/hike/index.rb', line 70

def entries(path)
  key = path.to_s
  @entries[key] ||= Pathname.new(path).entries.reject { |entry| entry.to_s =~ /^\.\.?$/ }
rescue Errno::ENOENT
  @entries[key] = []
end

#find(*logical_paths, &block) ⇒ Object

The real implementation of ‘find`. `Trail#find` generates a one time index and delegates here.

See ‘Trail#find` for usage.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hike/index.rb', line 45

def find(*logical_paths, &block)
  if block_given?
    options = extract_options!(logical_paths)
    base_path = Pathname.new(options[: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
  else
    find(*logical_paths) do |path|
      return path
    end
  end
end

#indexObject

‘Index#index` returns `self` to be compatable with the `Trail` interface.



37
38
39
# File 'lib/hike/index.rb', line 37

def index
  self
end

#rootObject

‘Index#root` returns root path as a `String`. This attribute is immutable.



32
33
34
# File 'lib/hike/index.rb', line 32

def root
  @root.to_s
end

#stat(path) ⇒ Object

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



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hike/index.rb', line 79

def stat(path)
  key = path.to_s
  if @stats.key?(key)
    @stats[key]
  else
    begin
      @stats[key] = File.stat(path)
    rescue Errno::ENOENT
      @stats[key] = nil
    end
  end
end