cache_box 0.0.1.pre.preview1 → 0.0.1.pre.preview6

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: 19b591535ab5ba124dca095013f08a3be9b348e0138a1738ec6c3088f388e8b9
4
- data.tar.gz: 33183202f61274a5f3cfbae070bb83a8f2749b99596daeb98a43f173da8e5ea7
3
+ metadata.gz: ab194a6029ec0a9e3a9ac5cb8cdac124f7a06ab1d4066d50665bbdf9548a727a
4
+ data.tar.gz: 06e3109c3d4f9437bd70768d21c69cc3252cc316f60a68072c5d1d8edb7fa4b6
5
5
  SHA512:
6
- metadata.gz: e14a3c9b73792b6debc38676ce17f28ecfc81981e96476c7d6038ef4a1b9cd9af48513fc4569b9155edb5aa1c685e0ed2ad7844be2ff4e98ae905dce131fdff0
7
- data.tar.gz: fd250b2db538d59d215ab656def286afca52d09894d1ea667484a1927441bd1d50638c5a1f0c187a89fb92cb40f7863c5b018fba8ae47867eec15df65edb8233
6
+ metadata.gz: 2229714e8ce3c5d92eb5a34cfdf047d6f0e315a56db856ea9c05bebacc22ca2c0881689d822df58c01f608cafd00d323b2e64f7decce15695ca671a1383fd1c1
7
+ data.tar.gz: 83b5d7dac66492c48ea35c5ec82a70c249260d6e0f1961322ed9016ddafa1d0a7fecd6a3323db42cf7f9f2e29d6bced25d50d0117a76ac9af1215d17bb1e8639
@@ -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-preview1'
5
+ spec.version = '0.0.1-preview6'
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
- def initialize(namespace = :default_namespace)
7
+ # Input:
8
+ #
9
+ # namespace = String | Symbol # Default: :namespace
10
+ #
11
+ # Output: N/A
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
 
16
- def reset!(namespace = :default_namespace)
21
+ # Input:
22
+ #
23
+ # namespace = String | Symbol # Default: :namespace
24
+ #
25
+ # Output: self
26
+ def reset!(namespace = :namespace)
17
27
  initialize(namespace)
18
28
 
19
29
  self
20
30
  end
21
31
 
22
- def with(name = :default_name, *args)
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.
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
 
32
- def with_many(name = :default_name, *args)
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.
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
 
48
- def has?(name = :default_name)
91
+ # Input:
92
+ #
93
+ # name = String | Symbol # Default: :name
94
+ #
95
+ # Output: true | false
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
 
64
- def expire(name = :default_name)
116
+ # Input:
117
+ #
118
+ # name = String | Symbol # Default: :name
119
+ #
120
+ # Output: self
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,36 +134,64 @@ 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
- return if arg.is_a?(Symbol)
144
+ return if arg.is_a?(Symbol) || arg.is_a?(String)
78
145
 
79
146
  klass = arg.class
80
147
  value = arg.inspect
81
148
 
82
- raise(ArgumentError, "#{name} must be a Symbol, got #{klass}: #{value}")
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] if data.key?(:complete)
163
+ @state[:result][name] = data[:result] if data.key?(:result)
164
+ @state[:storage][name] = data[:storage] if data.key?(:storage)
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
100
-
101
- content = Marshal.dump(value)
102
-
103
- FileUtils.mkdir_p(@directory) unless Dir.exist?(@directory)
180
+ # Input:
181
+ #
182
+ # file = String # Path
183
+ #
184
+ # Output: N/A
185
+ def store!(name, file)
186
+ data = {}
187
+ data[:complete] = @state[:complete][name] if @state[:complete][name]
188
+ data[:result] = @state[:result][name] if @state[:result][name]
189
+ data[:storage] = @state[:storage][name] if @state[:storage][name] && !@state[:complete][name]
190
+
191
+ content = Marshal.dump(data)
192
+
193
+ directory = File.dirname(file)
194
+ FileUtils.mkdir_p(directory) unless Dir.exist?(directory)
104
195
  File.write(file, content)
105
-
106
- value
107
196
  end
108
197
  end
@@ -2,18 +2,35 @@
2
2
 
3
3
  class CacheBox
4
4
  class Chain
5
- def initialize(namespace = :default_namespace)
5
+ # Input:
6
+ #
7
+ # namespace = String | Symbol # Default: :namespace
8
+ #
9
+ # Output: N/A
10
+ def initialize(namespace = :namespace)
6
11
  @cache = ::CacheBox.new(namespace)
7
12
  @chain = []
8
13
  end
9
14
 
10
- def reset!(namespace = :default_namespace)
15
+ # Input:
16
+ #
17
+ # namespace = String | Symbol # Default: :namespace
18
+ #
19
+ # Output: self
20
+ def reset!(namespace = :namespace)
11
21
  @cache.reset!(namespace)
12
22
  @chain = []
13
23
 
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,12 +95,21 @@ 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
- unless name.is_a?(Symbol)
105
+ unless name.is_a?(Symbol) || name.is_a?(String)
65
106
  klass = name.class
66
107
  value = name.inspect
67
108
 
68
- raise(ArgumentError, "name must be a Symbol, got #{klass}: #{value}")
109
+ raise(
110
+ ArgumentError,
111
+ "name must be a Symbol or a String, got #{klass}: #{value}"
112
+ )
69
113
  end
70
114
 
71
115
  return unless block.nil?
@@ -76,6 +120,11 @@ class CacheBox
76
120
  )
77
121
  end
78
122
 
123
+ # Input:
124
+ #
125
+ # arg = nil | :all
126
+ #
127
+ # Output: N/A
79
128
  def validate_run!(arg)
80
129
  return if arg.nil? || arg == :all
81
130
 
@@ -89,6 +138,7 @@ class CacheBox
89
138
  )
90
139
  end
91
140
 
141
+ # Output: Object # Anything the last callable returns.
92
142
  def run_all
93
143
  result = nil
94
144
  @chain.each do |_name, args, callable|
@@ -102,6 +152,7 @@ class CacheBox
102
152
  result
103
153
  end
104
154
 
155
+ # Output: Object # Anything the last executed callable returns.
105
156
  def run_chain
106
157
  work = []
107
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.preview1
4
+ version: 0.0.1.pre.preview6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Codruț Constantin Gușoi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-17 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