88# residing in the `HOMEBREW_CACHE`.
99#
1010class CacheStoreDatabase
11+ extend T ::Generic
12+
13+ # Sorbet type members are mutable by design and cannot be frozen.
14+ Key = type_member # rubocop:disable Style/MutableConstant
15+ # Sorbet type members are mutable by design and cannot be frozen.
16+ Value = type_member # rubocop:disable Style/MutableConstant
17+
1118 # Yields the cache store database.
1219 # Closes the database after use if it has been loaded.
13- #
14- # @param [Symbol] type
15- # @yield [CacheStoreDatabase] self
1620 sig {
1721 type_parameters ( :U )
1822 . params (
1923 type : Symbol ,
20- _blk : T . proc . params ( arg0 : CacheStoreDatabase ) . returns ( T . type_parameter ( :U ) ) ,
24+ _blk : T . proc . params ( arg0 : CacheStoreDatabase [ T . anything , T . anything ] ) . returns ( T . type_parameter ( :U ) ) ,
2125 )
2226 . returns ( T . type_parameter ( :U ) )
2327 }
@@ -47,32 +51,29 @@ def self.use(type, &_blk)
4751 end
4852
4953 # Creates a CacheStoreDatabase.
50- #
51- # @param [Symbol] type
52- # @return [nil]
5354 sig { params ( type : Symbol ) . void }
5455 def initialize ( type )
5556 @type = type
5657 @dirty = T . let ( false , T . nilable ( T ::Boolean ) )
5758 end
5859
5960 # Sets a value in the underlying database (and creates it if necessary).
60- sig { params ( key : T . anything , value : T . anything ) . void }
61+ sig { params ( key : Key , value : Value ) . void }
6162 def set ( key , value )
6263 dirty!
6364 db [ key ] = value
6465 end
6566
6667 # Gets a value from the underlying database (if it already exists).
67- sig { params ( key : T . anything ) . returns ( T . untyped ) }
68+ sig { params ( key : Key ) . returns ( T . nilable ( Value ) ) }
6869 def get ( key )
6970 return unless created?
7071
7172 db [ key ]
7273 end
7374
7475 # Deletes a value from the underlying database (if it already exists).
75- sig { params ( key : T . anything ) . void }
76+ sig { params ( key : Key ) . void }
7677 def delete ( key )
7778 return unless created?
7879
@@ -99,16 +100,12 @@ def write_if_dirty!
99100 end
100101
101102 # Returns `true` if the cache file has been created for the given `@type`.
102- #
103- # @return [Boolean]
104103 sig { returns ( T ::Boolean ) }
105104 def created?
106105 cache_path . exist?
107106 end
108107
109108 # Returns the modification time of the cache file (if it already exists).
110- #
111- # @return [Time]
112109 sig { returns ( T . nilable ( Time ) ) }
113110 def mtime
114111 return unless created?
@@ -117,25 +114,23 @@ def mtime
117114 end
118115
119116 # Performs a `select` on the underlying database.
120- #
121- # @return [ Hash]
122- sig { params ( block : T . proc . params ( arg0 : T . untyped , arg1 : T . untyped ) . returns ( BasicObject ) ) . returns ( T :: Hash [ T . untyped , T . untyped ] ) }
117+ sig {
118+ overridable . params ( block : T . proc . params ( arg0 : Key , arg1 : Value ) . returns ( BasicObject ) ) . returns ( T :: Hash [ Key , Value ] )
119+ }
123120 def select ( &block )
124121 db . select ( &block )
125122 end
126123
127124 # Returns `true` if the cache is empty.
128- #
129- # @return [Boolean]
130125 sig { returns ( T ::Boolean ) }
131126 def empty?
132127 db . empty?
133128 end
134129
135130 # Performs a `each_key` on the underlying database.
136- #
137- # @return [ Hash]
138- sig { params ( block : T . proc . params ( arg0 : T . untyped ) . returns ( BasicObject ) ) . returns ( T :: Hash [ T . untyped , T . untyped ] ) }
131+ sig {
132+ params ( block : T . proc . params ( arg0 : Key ) . returns ( BasicObject ) ) . returns ( T :: Hash [ Key , Value ] )
133+ }
139134 def each_key ( &block )
140135 db . each_key ( &block )
141136 end
@@ -145,11 +140,9 @@ def each_key(&block)
145140 # Lazily loaded database in read/write mode. If this method is called, a
146141 # database file will be created in the `HOMEBREW_CACHE` with a name
147142 # corresponding to the `@type` instance variable.
148- #
149- # @return [Hash] db
150- sig { returns ( T ::Hash [ T . untyped , T . untyped ] ) }
143+ sig { returns ( T ::Hash [ Key , Value ] ) }
151144 def db
152- @db ||= T . let ( { } , T . nilable ( T ::Hash [ T . untyped , T . untyped ] ) )
145+ @db ||= T . let ( { } , T . nilable ( T ::Hash [ Key , Value ] ) )
153146 return @db if !@db . empty? || !created?
154147
155148 begin
@@ -163,8 +156,6 @@ def db
163156
164157 # The path where the database resides in the `HOMEBREW_CACHE` for the given
165158 # `@type`.
166- #
167- # @return [Pathname]
168159 sig { returns ( Pathname ) }
169160 def cache_path
170161 HOMEBREW_CACHE /"#{ @type } .json"
@@ -177,8 +168,6 @@ def dirty!
177168 end
178169
179170 # Returns `true` if the cache needs to be written to disk.
180- #
181- # @return [Boolean]
182171 sig { returns ( T ::Boolean ) }
183172 def dirty?
184173 !!@dirty
@@ -190,20 +179,23 @@ def dirty?
190179# storage mechanism.
191180#
192181class CacheStore # rubocop:todo Style/OneClassPerFile
182+ extend T ::Generic
193183 extend T ::Helpers
194184
195185 abstract!
196186
197- # @param [CacheStoreDatabase] database
198- # @return [nil]
199- sig { params ( database : CacheStoreDatabase ) . void }
187+ # Sorbet type members are mutable by design and cannot be frozen.
188+ Key = type_member # rubocop:disable Style/MutableConstant
189+ # Sorbet type members are mutable by design and cannot be frozen.
190+ Value = type_member # rubocop:disable Style/MutableConstant
191+
192+ sig { params ( database : CacheStoreDatabase [ Key , Value ] ) . void }
200193 def initialize ( database )
201- @database = database
194+ @database = T . let ( database , CacheStoreDatabase [ Key , Value ] )
202195 end
203196
204197 protected
205198
206- # @return [CacheStoreDatabase]
207- sig { returns ( CacheStoreDatabase ) }
199+ sig { returns ( CacheStoreDatabase [ Key , Value ] ) }
208200 attr_reader :database
209201end
0 commit comments