Class: ROM::Files::Connection
- Inherits:
-
Object
- Object
- ROM::Files::Connection
- Extended by:
- Forwardable
- Defined in:
- lib/rom/files/connection.rb
Instance Attribute Summary collapse
-
#data ⇒ Concurrent::Hash
readonly
private
Dataset registry.
- #path ⇒ Pathname readonly
Instance Method Summary collapse
- #[] ⇒ Dataset private
- #binread(id) ⇒ String
- #binwrite(id, contents) ⇒ String
- #build_dataset(options) ⇒ Object
- #create_dataset(name) ⇒ Dataset
- #delete(id) ⇒ Object
-
#initialize(path = Pathname.pwd) ⇒ Connection
constructor
A new instance of Connection.
- #key?(name) ⇒ Boolean
- #read(id) ⇒ String
- #search(patterns, path: self.path, exclude_patterns: EMPTY_ARRAY, sorting: nil, directories: false) ⇒ Array<Pathname>
-
#size ⇒ Integer
private
Return registered datasets count.
- #write(id, contents) ⇒ String
Constructor Details
#initialize(path = Pathname.pwd) ⇒ Connection
Returns a new instance of Connection.
13 14 15 16 |
# File 'lib/rom/files/connection.rb', line 13 def initialize(path = Pathname.pwd) @path = Pathname(path). @data = Concurrent::Hash.new end |
Instance Attribute Details
#data ⇒ Concurrent::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
26 27 28 |
# File 'lib/rom/files/connection.rb', line 26 def data @data end |
#path ⇒ Pathname (readonly)
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.
39 |
# File 'lib/rom/files/connection.rb', line 39 def_instance_delegators :data, :[], :size |
#binread(id) ⇒ String
96 97 98 |
# File 'lib/rom/files/connection.rb', line 96 def binread(id) path_for(id).binread end |
#binwrite(id, contents) ⇒ 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() Dataset.new([], .merge(connection: self)) end |
#create_dataset(name) ⇒ Dataset
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
108 109 110 |
# File 'lib/rom/files/connection.rb', line 108 def delete(id) path_for(id).delete end |
#key?(name) ⇒ 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
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>
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 |
#size ⇒ Integer
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
39 |
# File 'lib/rom/files/connection.rb', line 39 def_instance_delegators :data, :[], :size |
#write(id, contents) ⇒ String
90 91 92 |
# File 'lib/rom/files/connection.rb', line 90 def write(id, contents) path_for(id).write(contents.to_s) end |