dis 1.0.2 → 1.0.3

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
  SHA1:
3
- metadata.gz: 8e3efb0078d31c5dad69adb12179ac307e07e44b
4
- data.tar.gz: 568d166000b658f28b4456d1210117164b7d66d2
3
+ metadata.gz: ca1ddbb8f8cbce08f246eb2ae764fc1f330cee7b
4
+ data.tar.gz: 0faed0087f940954495eb77e994e78bc07b4f9b1
5
5
  SHA512:
6
- metadata.gz: 184a32e5b464fcc2a4f8598bf4773a2dd7bfc4ec950ce8624764a72ba64d67cdc0bec3f0c4cb430dc3b224cac141db56cc1f40951d8462782c53f83fc88a5024
7
- data.tar.gz: 4f12596ecd11fbb0fa52af015021b90607d9fda21c2f6aada34057c6ea8b3e6f6222f29afde2d2e3825c88366b45a88bf2c45a75dbb5391d6713fc060285c962
6
+ metadata.gz: f4a242e9bf376396a498def8cd1cd457bb5e35b680781cc337faef9d57e238c851f1ddead2417fa6221da4c89130b5672990565d026b3b7f328e47cfcc8ef1a3
7
+ data.tar.gz: f811ffaf3e73c76e92df930ad03debd766da58d524d0bc2e929c6ba771a3e59b1167c385a2eab4469fd538a32d95be2edfa1f35c896b60b95bfe3a812235b95f
@@ -0,0 +1,98 @@
1
+ # encoding: utf-8
2
+
3
+ module Dis
4
+ # = Dis SparseLayer
5
+ #
6
+ # A special case of Dis::Layer intended to be sparsely populated.
7
+ # Can be configured with a size limit, objects that have been transferred
8
+ # to other layers will be evicted based on a Least Recently Used basis.
9
+ #
10
+ # ==== Example
11
+ #
12
+ # Dis::SparseLayer.new(
13
+ # Fog::Storage.new({
14
+ # provider: 'Local',
15
+ # local_root: Rails.root.join('db', 'dis')
16
+ # }),
17
+ # path: Rails.env,
18
+ # limit: 10.gigabytes
19
+ # )
20
+ class SparseLayer < Layer
21
+ def initialize(connection, options={})
22
+ super
23
+ @limit = options[:limit]
24
+ end
25
+
26
+ def limit?
27
+ @limit ? true : false
28
+ end
29
+
30
+ def store(type, hash, file)
31
+ super.tap do
32
+ update_timestamp(type, hash)
33
+ end
34
+ end
35
+
36
+ def get(type, hash)
37
+ super.tap do |result|
38
+ update_timestamp(type, hash) if result
39
+ end
40
+ end
41
+
42
+ def delete(type, hash)
43
+ super.tap do
44
+ delete_timestamp(type, hash)
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def delete_timestamp(type, path)
51
+ return false unless timestamp_exists?(type, path)
52
+ get_timestamp(type, path).destroy
53
+ end
54
+
55
+ def get_timestamp(type, hash)
56
+ if dir = directory(type, hash)
57
+ dir.files.get(timestamp_path(type, hash))
58
+ end
59
+ end
60
+
61
+ def outdated_timestamp?(timestamp)
62
+ !timestamp || timestamp < 5.minutes.ago
63
+ end
64
+
65
+ def read_timestamp(type, hash)
66
+ if timestamp = get_timestamp(type, hash)
67
+ DateTime.parse(timestamp.body)
68
+ end
69
+ end
70
+
71
+ def timestamp_exists?(type, hash)
72
+ if (directory(type, hash) &&
73
+ directory(type, hash).files.head(timestamp_path(type, hash)))
74
+ true
75
+ else
76
+ false
77
+ end
78
+ end
79
+
80
+ def timestamp_path(type, hash)
81
+ key_component(type, hash) + ".timestamp"
82
+ end
83
+
84
+ def update_timestamp(type, hash)
85
+ if outdated_timestamp?(read_timestamp(type, hash))
86
+ write_timestamp(type, hash)
87
+ end
88
+ end
89
+
90
+ def write_timestamp(type, hash)
91
+ directory!(type, hash).files.create(
92
+ key: timestamp_path(type, hash),
93
+ body: DateTime.now.to_s,
94
+ public: public?
95
+ )
96
+ end
97
+ end
98
+ end
data/lib/dis/storage.rb CHANGED
@@ -17,6 +17,21 @@ module Dis
17
17
  # one writeable, non-delayed layer must exist.
18
18
  class Storage
19
19
  class << self
20
+ # Returns a hex digest for a given binary. Accepts files, strings
21
+ # and Fog models.
22
+ def file_digest(file, &block)
23
+ hash = case file
24
+ when Fog::Model
25
+ digest.hexdigest(file.body)
26
+ when String
27
+ digest.hexdigest(file)
28
+ else
29
+ digest.file(file.path).hexdigest
30
+ end
31
+ yield hash if block_given?
32
+ hash
33
+ end
34
+
20
35
  # Exposes the layer set, which is an instance of
21
36
  # <tt>Dis::Layers</tt>.
22
37
  def layers
@@ -113,7 +128,7 @@ module Dis
113
128
  private
114
129
 
115
130
  def store_immediately!(type, file)
116
- hash_file(file) do |hash|
131
+ file_digest(file) do |hash|
117
132
  layers.immediate.writeable.each do |layer|
118
133
  layer.store(type, hash, file)
119
134
  end
@@ -135,19 +150,6 @@ module Dis
135
150
  def digest
136
151
  Digest::SHA1
137
152
  end
138
-
139
- def hash_file(file, &block)
140
- hash = case file
141
- when Fog::Model
142
- digest.hexdigest(file.body)
143
- when String
144
- digest.hexdigest(file)
145
- else
146
- digest.file(file.path).hexdigest
147
- end
148
- yield hash if block_given?
149
- hash
150
- end
151
153
  end
152
154
  end
153
155
  end
data/lib/dis/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Dis
4
- VERSION = "1.0.2"
4
+ VERSION = "1.0.3"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dis
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Inge Jørgensen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-28 00:00:00.000000000 Z
11
+ date: 2015-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -86,6 +86,7 @@ files:
86
86
  - lib/dis/model.rb
87
87
  - lib/dis/model/class_methods.rb
88
88
  - lib/dis/model/data.rb
89
+ - lib/dis/sparse_layer.rb
89
90
  - lib/dis/storage.rb
90
91
  - lib/dis/validations.rb
91
92
  - lib/dis/validations/data_presence.rb
@@ -113,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
114
  version: '0'
114
115
  requirements: []
115
116
  rubyforge_project:
116
- rubygems_version: 2.4.1
117
+ rubygems_version: 2.4.5
117
118
  signing_key:
118
119
  specification_version: 4
119
120
  summary: A file store for your Rails app