dis 1.1.21 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b9926ad8c82fb6ed96ea76e41e3f93f7c4dc2c5e8acec7917e6131cb474e0404
4
- data.tar.gz: 27521093c0575935ce489991f8250dac55a2dc86384ac207639fe03fe25ad2e0
3
+ metadata.gz: cf00e607df1b1aa62ffa3e025c22ac42e3363d14fb0a06cb6b8f41d3b7757db5
4
+ data.tar.gz: 2cfe7f5a47d87bdd5e6c59e7f98446bec2918a27c8e3476c123ef1164842d437
5
5
  SHA512:
6
- metadata.gz: fcd0fa193bf99030dee1a1254f91c7bdad6237a6b899120a3a14569aa6d766a5629dc46bfb4c73ba8f29e8f67c1244bfdaf17f4d2d1438d9b5c96660d3dcd5c4
7
- data.tar.gz: 2cb2dd1c9fca6bd489248980ab2d251d0d9fab85cbe5b98334dd52ccc6042a2faf6a5dccc1584a339b5899f4fb5157cc873ac235cebd16f26f2a8d45a5a728ab
6
+ metadata.gz: e2428a8aa2299b730e1db9702fbec9e725cae6a38bfd3e04bce0ac1ba41484f45840f52cdfe0f998dfe5ffa3c5c5a5fa31bc01c380a1aa8a307392e4a4c9a6a4
7
+ data.tar.gz: d1e9bfbc53cdd753fa0b52730b4c6ac88ec00ca1ba3dd519086fdea58f117947f30d0eb5bef297d952a8fb8deab6d642ce939d5a045d19f70f0586c578b3111e
data/lib/dis/layer.rb CHANGED
@@ -136,6 +136,21 @@ module Dis
136
136
  end
137
137
  end
138
138
 
139
+ # Returns the absolute file path for a locally stored file, or nil
140
+ # if the provider is not local or the file does not exist.
141
+ #
142
+ # layer.file_path("documents", key)
143
+ def file_path(type, key)
144
+ return unless connection.respond_to?(:local_root)
145
+ return unless exists?(type, key)
146
+
147
+ File.join(
148
+ connection.local_root,
149
+ directory_component(type, key),
150
+ key_component(type, key)
151
+ )
152
+ end
153
+
139
154
  # Deletes a file from the store.
140
155
  #
141
156
  # layer.delete("documents", key)
@@ -14,11 +14,13 @@ module Dis
14
14
 
15
15
  # Returns true if two Data objects represent the same data.
16
16
  def ==(other)
17
- return false unless other.respond_to?(:read)
18
-
19
- # TODO: This can be made faster by
20
- # comparing hashes for stored objects.
21
- other.read == read
17
+ if !raw? && other.is_a?(self.class) && !other.changed?
18
+ content_hash == other.content_hash
19
+ elsif other.respond_to?(:read)
20
+ other.read == read
21
+ else
22
+ false
23
+ end
22
24
  end
23
25
 
24
26
  # Returns true if data exists either in memory or in storage.
@@ -68,6 +70,22 @@ module Dis
68
70
  Dis::Storage.store(storage_type, raw)
69
71
  end
70
72
 
73
+ # Clears cached data and tempfiles, allowing them to be garbage
74
+ # collected. Subsequent calls to read or tempfile will re-fetch.
75
+ def reset_read_cache!
76
+ @read = nil
77
+ return unless @tempfile
78
+
79
+ @tempfile.close!
80
+ @tempfile = nil
81
+ end
82
+
83
+ # Returns the file path to the data. Prefers a local storage path
84
+ # to avoid unnecessary copies, falls back to a tempfile.
85
+ def file_path
86
+ local_path || tempfile.path
87
+ end
88
+
71
89
  # Writes the data to a temporary file.
72
90
  def tempfile
73
91
  unless @tempfile
@@ -78,6 +96,12 @@ module Dis
78
96
  @tempfile
79
97
  end
80
98
 
99
+ protected
100
+
101
+ def content_hash
102
+ @record[@record.class.dis_attributes[:content_hash]]
103
+ end
104
+
81
105
  private
82
106
 
83
107
  def closest
@@ -88,10 +112,6 @@ module Dis
88
112
  end
89
113
  end
90
114
 
91
- def content_hash
92
- @record[@record.class.dis_attributes[:content_hash]]
93
- end
94
-
95
115
  def raw?
96
116
  raw ? true : false
97
117
  end
@@ -129,6 +149,12 @@ module Dis
129
149
  Dis::Storage.get(storage_type, content_hash)
130
150
  end
131
151
 
152
+ def local_path
153
+ return if raw?
154
+
155
+ Dis::Storage.file_path(storage_type, content_hash)
156
+ end
157
+
132
158
  attr_reader :raw
133
159
  end
134
160
  end
data/lib/dis/model.rb CHANGED
@@ -131,6 +131,11 @@ module Dis
131
131
  dis_set :filename, file.original_filename
132
132
  end
133
133
 
134
+ # Returns a file path to the data, preferring local storage paths.
135
+ def data_file_path
136
+ dis_data.file_path
137
+ end
138
+
134
139
  # Returns the data as a temporary file.
135
140
  delegate :tempfile, to: :dis_data
136
141
 
data/lib/dis/storage.rb CHANGED
@@ -113,6 +113,19 @@ module Dis
113
113
  result
114
114
  end
115
115
 
116
+ # Returns the absolute file path from the first layer that has a
117
+ # local copy, or nil if no layer stores files locally.
118
+ #
119
+ # Dis::Storage.file_path("things", key)
120
+ def file_path(type, key)
121
+ require_layers!
122
+ layers.each do |layer|
123
+ path = layer.file_path(type, key)
124
+ return path if path
125
+ end
126
+ nil
127
+ end
128
+
116
129
  # Deletes a file from all layers. Kicks off a
117
130
  # <tt>Dis::Jobs::Delete</tt> job if any delayed layers are defined.
118
131
  # Returns true if the file existed in any immediate layers,
data/lib/dis/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dis
4
- VERSION = "1.1.21"
4
+ VERSION = "1.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dis
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.21
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Inge Jørgensen