Class: ROM::Files::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rom/files/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = Pathname.pwd) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • path (Pathname, #to_s) (defaults to: Pathname.pwd)


13
14
15
16
# File 'lib/rom/files/connection.rb', line 13

def initialize(path = Pathname.pwd)
  @path = Pathname(path).expand_path
  @data = Concurrent::Hash.new
end

Instance Attribute Details

#dataConcurrent::Hash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Dataset registry

Returns:

  • (Concurrent::Hash)


26
27
28
# File 'lib/rom/files/connection.rb', line 26

def data
  @data
end

#pathPathname (readonly)

Returns:



19
20
21
# File 'lib/rom/files/connection.rb', line 19

def path
  @path
end

Instance Method Details

#[]Dataset

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



39
# File 'lib/rom/files/connection.rb', line 39

def_instance_delegators :data, :[], :size

#binread(id) ⇒ String

Parameters:

Returns:

  • (String)


96
97
98
# File 'lib/rom/files/connection.rb', line 96

def binread(id)
  path_for(id).binread
end

#binwrite(id, contents) ⇒ String

Parameters:

  • id (Pathname, #to_s)
  • contents (String, #to_s)

Returns:

  • (String)


103
104
105
# File 'lib/rom/files/connection.rb', line 103

def binwrite(id, contents)
  path_for(id).binwrite(contents)
end

#build_dataset(options) ⇒ Object



52
53
54
# File 'lib/rom/files/connection.rb', line 52

def build_dataset(options)
  Dataset.new([], options.merge(connection: self))
end

#create_dataset(name) ⇒ Dataset

Parameters:

  • name (String, Symbol)

Returns:



43
44
45
46
47
48
49
50
# File 'lib/rom/files/connection.rb', line 43

def create_dataset(name)
  mime_type = MIME::Types[name].first
  @data[name] = if mime_type
                  build_dataset(mime_type: mime_type)
                else
                  build_dataset(inside_paths: [name])
                end
end

#delete(id) ⇒ Object

Parameters:



108
109
110
# File 'lib/rom/files/connection.rb', line 108

def delete(id)
  path_for(id).delete
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/rom/files/connection.rb', line 57

def key?(name)
  MIME::Types[name].any? || path_for(name).exist?
end

#read(id) ⇒ String

Parameters:

Returns:

  • (String)


83
84
85
# File 'lib/rom/files/connection.rb', line 83

def read(id)
  path_for(id).read
end

#search(patterns, path: self.path, exclude_patterns: EMPTY_ARRAY, sorting: nil, directories: false) ⇒ Array<Pathname>

Parameters:

  • patterns (Array<String>)
  • path (Pathname) (defaults to: self.path)
  • exclude_patterns (Array<String>) (defaults to: EMPTY_ARRAY)
  • sorting (#to_proc, nil) (defaults to: nil)
  • directories (Boolean) (defaults to: false)

Returns:



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rom/files/connection.rb', line 67

def search(patterns, path: self.path, exclude_patterns: EMPTY_ARRAY, sorting: nil, directories: false)
  files = patterns.inject([]) do |result, pattern|
    result + Pathname.glob(path_for(pattern, path: path)).map { |found| found.relative_path_from(path) }
  end
  files = files.reject(&:directory?) unless directories
  files = files.reject do |match|
    exclude_patterns.any? do |pattern|
      match.fnmatch(pattern, File::FNM_EXTGLOB)
    end
  end
  files = files.sort_by(&sorting) if sorting
  files
end

#sizeInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return registered datasets count

Returns:

  • (Integer)


39
# File 'lib/rom/files/connection.rb', line 39

def_instance_delegators :data, :[], :size

#write(id, contents) ⇒ String

Parameters:

  • id (Pathname, #to_s)
  • contents (String, #to_s)

Returns:

  • (String)


90
91
92
# File 'lib/rom/files/connection.rb', line 90

def write(id, contents)
  path_for(id).write(contents.to_s)
end