Module: BFS

Defined in:
lib/bfs.rb,
lib/bfs/blob.rb,
lib/bfs/bucket.rb,
lib/bfs/errors.rb,
lib/bfs/helpers.rb,
lib/bfs/bucket/fs.rb,
lib/bfs/bucket/in_mem.rb,
lib/bfs/bucket/abstract.rb

Defined Under Namespace

Modules: Bucket Classes: Blob, FileInfo, FileNotFound, Writer

Class Method Summary collapse

Class Method Details

.defer(obj, method) ⇒ Object



83
84
85
86
# File 'lib/bfs.rb', line 83

def self.defer(obj, method)
  owner = Process.pid
  ObjectSpace.define_finalizer(obj, ->(*) { obj.send(method) if Process.pid == owner })
end

.norm_mode(mode) ⇒ Object



78
79
80
81
# File 'lib/bfs.rb', line 78

def self.norm_mode(mode)
  mode = mode.to_i(8) if mode.is_a?(String)
  mode & 0o000777
end

.norm_path(path) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/bfs.rb', line 70

def self.norm_path(path)
  path = path.to_s.dup
  path.gsub!(File::SEPARATOR, '/')
  path.sub!(%r{^/+}, '')
  path.sub!(%r{/+$}, '')
  path
end

.register(*schemes, &resolver) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/bfs.rb', line 38

def self.register(*schemes, &resolver)
  @registry ||= {}
  schemes.each do |scheme|
    scheme = scheme.to_s
    raise(ArgumentError, "scheme #{scheme} is already registered") if @registry.key?(scheme)

    @registry[scheme] = resolver
  end
end

.resolve(url, &block) ⇒ Object

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bfs.rb', line 58

def self.resolve(url, &block)
  url = url.is_a?(::URI) ? url.dup : URI.parse(url)
  rsl = @registry[url.scheme]
  raise ArgumentError, "Unable to resolve #{url}, scheme #{url.scheme} is not registered" unless rsl

  opts = {}
  CGI.parse(url.query.to_s).each do |key, values|
    opts[key.to_sym] = values.first
  end
  rsl.call(url, opts, block)
end

.unregister(*schemes) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/bfs.rb', line 48

def self.unregister(*schemes)
  @registry ||= {}
  schemes.each do |scheme|
    scheme = scheme.to_s
    raise(ArgumentError, "scheme #{scheme} is not registered") unless @registry.key?(scheme)

    @registry.delete(scheme)
  end
end