Class: Masamune::CachedFilesystem::PathCache

Inherits:
Object
  • Object
show all
Defined in:
lib/masamune/cached_filesystem.rb

Instance Method Summary collapse

Constructor Details

#initialize(filesystem) ⇒ PathCache

Returns a new instance of PathCache.



94
95
96
97
# File 'lib/masamune/cached_filesystem.rb', line 94

def initialize(filesystem)
  @filesystem = filesystem
  @cache = {}
end

Instance Method Details

#any?(path) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
121
# File 'lib/masamune/cached_filesystem.rb', line 117

def any?(path)
  elem = get(path)
  return false unless elem
  elem.any? { |entry| entry.name.start_with?(path) }
end

#get(path) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/masamune/cached_filesystem.rb', line 108

def get(path)
  return unless path
  paths = path_split(path)
  value = paths.inject(@cache) { |acc, elem| acc.is_a?(Hash) ? acc.fetch(elem, {}) : acc }
  emit(value)
rescue KeyError
  EMPTY_SET
end

#put(path, entry) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/masamune/cached_filesystem.rb', line 99

def put(path, entry)
  return unless path
  return if @filesystem.root_path?(path)
  put(File.join(@filesystem.dirname(path), '.'), OpenStruct.new(name: @filesystem.dirname(path)))
  paths = path_split(path)
  elems = paths.reverse.inject(entry) { |acc, elem| { elem => acc } }
  @cache.deep_merge!(elems)
end