cache_box 0.0.1.pre.preview2 → 0.0.1.pre.preview3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ffc75a2d7008957a8c53dabf66772390d5eca8e964f6ab92f35fe57c063d5e52
4
- data.tar.gz: 28c08be79e3a089f14897a42805a3a3b0b12f44a49c8fda8bc81884763aacc72
3
+ metadata.gz: 111176139c5bc21c2938a934ceda7d643ad1dedeed2055c7bf53782d7eabd599
4
+ data.tar.gz: ebcb405c0f2de33d53ae26d758a6f80c049793fe0dda8db1c482ea6660ddb455
5
5
  SHA512:
6
- metadata.gz: 948602c9c4a3aac655c6a2c13a8284d92f9da2246a7b901133c050adb2119ceadb4936fe750cb27ab5d5afe383326e2e02cf623fb5c3d5145acb6a9dce289aa2
7
- data.tar.gz: c8f3529d877814c4007d31699dabbd4fc375714b52184f36ec6660f3c126463f90afbe522c5e7eee42f62c0495ea9483753f02de66f533297dd855ec0b20f88e
6
+ metadata.gz: 51b2fd10d3484697d46ea07ec50d5a5ff6304f3abc22ed77546bc3702410519b38c0c47e549d183f1c74e860a170715fdef4c127b1cf9da2aaf665f0d88a7ef5
7
+ data.tar.gz: 67ee7653c76f8bcc9b20af7c461d9bafac6408852a6a1279eb033ff22eafbfbd7fc33dfff421a6131a5c15967e29e7604c7007d6461687a0134b1dda07081f98
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'cache_box'
5
- spec.version = '0.0.1-preview2'
5
+ spec.version = '0.0.1-preview3'
6
6
  spec.authors = ['Codruț Constantin Gușoi']
7
7
  spec.email = ['codrut.gusoi+git-commit@gmail.com']
8
8
 
@@ -20,7 +20,8 @@ Gem::Specification.new do |spec|
20
20
  ]
21
21
  spec.require_paths = ['lib']
22
22
 
23
- spec.add_development_dependency 'minitest', '~> 5.0'
24
- spec.add_development_dependency 'rake', '~> 12.0'
25
- spec.add_development_dependency 'rubocop', '~> 0.88'
23
+ spec.add_development_dependency 'minitest', '~> 5.14'
24
+ spec.add_development_dependency 'pry-byebug', '~> 3.9'
25
+ spec.add_development_dependency 'rake', '~> 13.0'
26
+ spec.add_development_dependency 'rubocop', '~> 0.88'
26
27
  end
@@ -4,68 +4,129 @@ require 'fileutils'
4
4
  require_relative 'cache_box_chain'
5
5
 
6
6
  class CacheBox
7
+ # Input:
8
+ #
9
+ # namespace = String | Symbol # Default: :namespace
10
+ #
11
+ # Output: N/A
7
12
  def initialize(namespace = :namespace)
8
13
  validate!(namespace, 'namespace')
9
14
 
10
15
  @namespace = namespace
11
- @state = {}
16
+ @state = { complete: {}, result: {}, storage: {} }
12
17
  @root = File.join(Dir.pwd, '.cache')
13
18
  @directory = File.join(@root, @namespace.to_s)
14
19
  end
15
20
 
21
+ # Input:
22
+ #
23
+ # namespace = String | Symbol # Default: :namespace
24
+ #
25
+ # Output: self
16
26
  def reset!(namespace = :namespace)
17
27
  initialize(namespace)
18
28
 
19
29
  self
20
30
  end
21
31
 
32
+ # Input:
33
+ #
34
+ # name = String | Symbol # Default: :name
35
+ # args = Array[...Object]
36
+ # &block = Proc(*args)
37
+ #
38
+ # Output: Object # Anything the &block returns.
22
39
  def with(name = :name, *args)
23
40
  validate!(name, 'name')
24
41
 
25
- file = File.join(@directory, name.to_s)
26
- result = find(name, file)
42
+ name = name.to_s
43
+ file = File.join(@directory, name)
44
+ result = find!(name, file)
27
45
  return result unless result.nil?
28
46
 
29
- store(yield(*args), name, file)
47
+ result = yield(*args)
48
+
49
+ @state[:complete][name] = true
50
+ @state[:result][name] = result
51
+ @state[:storage].delete(name)
52
+ store!(name, file)
53
+
54
+ result
30
55
  end
31
56
 
57
+ # Input:
58
+ #
59
+ # name = String | Symbol # Default: :name
60
+ # args = Array[...Object]
61
+ # &block = Proc(Hash{...Object => Object}, *args)
62
+ #
63
+ # Output: Object # Anything the &block returns.
32
64
  def with_many(name = :name, *args)
33
65
  validate!(name, 'name')
34
66
 
35
- file = File.join(@directory, name.to_s)
36
- result = find(name, file)
67
+ name = name.to_s
68
+ file = File.join(@directory, name)
69
+ result = find!(name, file)
37
70
  return result unless result.nil?
38
71
 
39
- storage = {}
72
+ storage = @state[:storage][name] || {}
40
73
  begin
41
- yield(storage, *args)
74
+ result = yield(storage, *args)
75
+
76
+ @state[:complete][name] = true
77
+ @state[:result][name] = result
78
+ @state[:storage].delete(name)
79
+ store!(name, file)
80
+
81
+ result
42
82
  ensure
43
- store(storage, name, file)
83
+ unless @state[:complete][name]
84
+ @state[:storage][name] = storage
85
+ end
86
+
87
+ store!(name, file)
44
88
  end
45
- storage
46
89
  end
47
90
 
91
+ # Input:
92
+ #
93
+ # name = String | Symbol # Default: :name
94
+ #
95
+ # Output: true | false
48
96
  def has?(name = :name)
49
97
  validate!(name, 'name')
50
98
 
51
- file = File.join(@directory, name.to_s)
99
+ name = name.to_s
100
+ return true if @state[:result].key?(name)
101
+
102
+ file = File.join(@directory, name)
52
103
  load!(name, file)
53
104
 
54
- @state.key?(name)
105
+ @state[:result].key?(name)
55
106
  end
56
107
 
108
+ # Output: self
57
109
  def expire!
58
- @state = {}
110
+ @state = { complete: {}, result: {}, storage: {} }
59
111
  FileUtils.remove_entry_secure(@directory, true)
60
112
 
61
113
  self
62
114
  end
63
115
 
116
+ # Input:
117
+ #
118
+ # name = String | Symbol # Default: :name
119
+ #
120
+ # Output: self
64
121
  def expire(name = :name)
65
122
  validate!(name, 'name')
66
123
 
67
- @state.delete(name)
68
- file = File.join(@directory, name.to_s)
124
+ name = name.to_s
125
+ @state[:complete].delete(name)
126
+ @state[:result].delete(name)
127
+ @state[:storage].delete(name)
128
+
129
+ file = File.join(@directory, name)
69
130
  FileUtils.remove_entry_secure(file, true)
70
131
 
71
132
  self
@@ -73,6 +134,12 @@ class CacheBox
73
134
 
74
135
  private
75
136
 
137
+ # Input:
138
+ #
139
+ # arg = String | Symbol
140
+ # name = String
141
+ #
142
+ # Output: N/A
76
143
  def validate!(arg, name)
77
144
  return if arg.is_a?(Symbol) || arg.is_a?(String)
78
145
 
@@ -82,28 +149,50 @@ class CacheBox
82
149
  raise(ArgumentError, "#{name} must be a Symbol or String, got #{klass}: #{value}")
83
150
  end
84
151
 
152
+ # Input:
153
+ #
154
+ # name = String
155
+ # file = String # Path
156
+ #
157
+ # Output: N/A
85
158
  def load!(name, file)
86
- return unless @state[name].nil? && File.exist?(file)
159
+ if File.exist?(file)
160
+ data = Marshal.load(File.read(file))
87
161
 
88
- content = File.read(file)
89
- @state[name] = Marshal.load(content)
162
+ @state[:complete][name] = data[:complete]
163
+ @state[:result][name] = data[:result]
164
+ @state[:store][name] = data[:store] unless data[:complete]
165
+ end
90
166
  end
91
167
 
92
- def find(name, file)
168
+ # Input:
169
+ #
170
+ # name = String
171
+ # file = String # Path
172
+ #
173
+ # Output: Object # Anything
174
+ def find!(name, file)
93
175
  load!(name, file)
94
176
 
95
- @state[name]
177
+ @state[:result][name]
96
178
  end
97
179
 
98
- def store(value, name, file)
99
- @state[name] = value
180
+ # Input:
181
+ #
182
+ # file = String # Path
183
+ #
184
+ # Output: N/A
185
+ def store!(name, file)
186
+ data = {
187
+ complete: @state[:complete][name],
188
+ result: @state[:result][name],
189
+ }
190
+ data[:store] = @state[:store][:name] unless @state[:complete][name]
100
191
 
101
- content = Marshal.dump(value)
192
+ content = Marshal.dump(data)
102
193
 
103
194
  directory = File.dirname(file)
104
195
  FileUtils.mkdir_p(directory) unless Dir.exist?(directory)
105
196
  File.write(file, content)
106
-
107
- value
108
197
  end
109
198
  end
@@ -2,11 +2,21 @@
2
2
 
3
3
  class CacheBox
4
4
  class Chain
5
+ # Input:
6
+ #
7
+ # namespace = String | Symbol # Default: :namespace
8
+ #
9
+ # Output: N/A
5
10
  def initialize(namespace = :namespace)
6
11
  @cache = ::CacheBox.new(namespace)
7
12
  @chain = []
8
13
  end
9
14
 
15
+ # Input:
16
+ #
17
+ # namespace = String | Symbol # Default: :namespace
18
+ #
19
+ # Output: self
10
20
  def reset!(namespace = :namespace)
11
21
  @cache.reset!(namespace)
12
22
  @chain = []
@@ -14,6 +24,13 @@ class CacheBox
14
24
  self
15
25
  end
16
26
 
27
+ # Input:
28
+ #
29
+ # name = String | Symbol
30
+ # args = Array[...Object]
31
+ # &block = Proc(*args)
32
+ #
33
+ # Output: self
17
34
  def chain(name, *args, &block)
18
35
  validate_chain!(name, &block)
19
36
 
@@ -24,6 +41,13 @@ class CacheBox
24
41
  self
25
42
  end
26
43
 
44
+ # Input:
45
+ #
46
+ # name = String | Symbol
47
+ # args = Array[...Object]
48
+ # &block = Proc(Hash{...Object => Object}, *args)
49
+ #
50
+ # Output: self
27
51
  def chain_many(name, *args, &block)
28
52
  validate_chain!(name, &block)
29
53
 
@@ -34,6 +58,11 @@ class CacheBox
34
58
  self
35
59
  end
36
60
 
61
+ # Input:
62
+ #
63
+ # all = nil | :all # Default: nil
64
+ #
65
+ # Output: Object # Anything the last block in the chain returns.
37
66
  def run!(all = nil)
38
67
  validate_run!(all)
39
68
 
@@ -44,12 +73,18 @@ class CacheBox
44
73
  end
45
74
  end
46
75
 
76
+ # Output: self
47
77
  def expire!
48
78
  @cache.expire!
49
79
 
50
80
  self
51
81
  end
52
82
 
83
+ # Input:
84
+ #
85
+ # names = ...(String | Symbol)
86
+ #
87
+ # Output: self
53
88
  def expire(*names)
54
89
  names.each do |name|
55
90
  @cache.expire(name)
@@ -60,6 +95,12 @@ class CacheBox
60
95
 
61
96
  private
62
97
 
98
+ # Input:
99
+ #
100
+ # name = String | Symbol
101
+ # &block = Proc
102
+ #
103
+ # Output: N/A
63
104
  def validate_chain!(name, &block)
64
105
  unless name.is_a?(Symbol) || name.is_a?(String)
65
106
  klass = name.class
@@ -79,6 +120,11 @@ class CacheBox
79
120
  )
80
121
  end
81
122
 
123
+ # Input:
124
+ #
125
+ # arg = nil | :all
126
+ #
127
+ # Output: N/A
82
128
  def validate_run!(arg)
83
129
  return if arg.nil? || arg == :all
84
130
 
@@ -92,6 +138,7 @@ class CacheBox
92
138
  )
93
139
  end
94
140
 
141
+ # Output: Object # Anything the last callable returns.
95
142
  def run_all
96
143
  result = nil
97
144
  @chain.each do |_name, args, callable|
@@ -105,6 +152,7 @@ class CacheBox
105
152
  result
106
153
  end
107
154
 
155
+ # Output: Object # Anything the last executed callable returns.
108
156
  def run_chain
109
157
  work = []
110
158
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache_box
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.pre.preview2
4
+ version: 0.0.1.pre.preview3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Codruț Constantin Gușoi
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-18 00:00:00.000000000 Z
11
+ date: 2020-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -16,28 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '5.0'
19
+ version: '5.14'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '5.0'
26
+ version: '5.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.9'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '12.0'
47
+ version: '13.0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: '12.0'
54
+ version: '13.0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rubocop
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -52,7 +66,7 @@ dependencies:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0.88'
55
- description:
69
+ description:
56
70
  email:
57
71
  - codrut.gusoi+git-commit@gmail.com
58
72
  executables: []
@@ -67,7 +81,7 @@ homepage: https://gitlab.com/sdwolfz/cache_box_rb
67
81
  licenses:
68
82
  - BSD 3-clause
69
83
  metadata: {}
70
- post_install_message:
84
+ post_install_message:
71
85
  rdoc_options: []
72
86
  require_paths:
73
87
  - lib
@@ -82,8 +96,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
96
  - !ruby/object:Gem::Version
83
97
  version: 1.3.1
84
98
  requirements: []
85
- rubygems_version: 3.1.3
86
- signing_key:
99
+ rubygems_version: 3.1.2
100
+ signing_key:
87
101
  specification_version: 4
88
102
  summary: A simple, fast, and easy to use file backed cache.
89
103
  test_files: []