Class: Opal::Hike::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/opal/hike.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.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/opal/hike.rb', line 41

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`, `@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 collection of extensions.



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

def extensions
  @extensions
end

#pathsObject (readonly)

Index#paths is an immutable collection of Pathnames.



34
35
36
# File 'lib/opal/hike.rb', line 34

def paths
  @paths
end

Instance Method Details

#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.



86
87
88
89
90
91
92
93
94
95
# File 'lib/opal/hike.rb', line 86

def entries(path)
  @entries[path.to_s] ||= begin
    pathname = Pathname.new(path)
    if pathname.directory?
      pathname.entries.reject { |entry| entry.to_s =~ /^\.|~$|^\#.*\#$/ }.sort
    else
      []
    end
  end
end

#find(logical_path) ⇒ Object

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

See Trail#find for usage.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/opal/hike.rb', line 70

def find(logical_path)
  base_path = Pathname.new(@root)
  logical_path = Pathname.new(logical_path.sub(/^\//, ''))

  if logical_path.to_s =~ %r{^\.\.?/}
    find_in_base_path(logical_path, base_path) { |path| return path }
  else
    find_in_paths(logical_path) { |path| return path }
  end

  nil
end

#indexObject

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



62
63
64
# File 'lib/opal/hike.rb', line 62

def index
  self
end

#rootObject

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



57
58
59
# File 'lib/opal/hike.rb', line 57

def root
  @root.to_s
end

#stat(path) ⇒ Object

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



99
100
101
102
103
104
105
106
107
108
# File 'lib/opal/hike.rb', line 99

def stat(path)
  key = path.to_s
  if @stats.key?(key)
    @stats[key]
  elsif File.exist?(path)
    @stats[key] = File.stat(path)
  else
    @stats[key] = nil
  end
end