lazy_init 0.1.0 → 0.1.1
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 +4 -4
- data/.yardopts +7 -0
- data/CHANGELOG.md +10 -0
- data/README.md +26 -26
- data/lib/lazy_init/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c3c12be25d5a45b0b5d70589b47c4335e8e610b809ee80647e0230c7778c6a5
|
4
|
+
data.tar.gz: 50dcdd14ca0228e4c202cd886cb17c622c2b677e0cb041f8f39f36eb14bc08cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7cfa817d7c404168dd8237410edd85453d71334b16b31dba4d63b2fecf5374080595c4db983798bddca2883704b8dc07639a861d8113fff4f7a24875bfb5340
|
7
|
+
data.tar.gz: 4c8ff04962ac7b59f6ecb9749faa6993814749fa9d146fc2eb482ffb9059ab03facc84430ceef04c07afc3484c5c041e833b8429a0ab523c6c352eef86659148
|
data/.yardopts
ADDED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@ Thread-safe lazy initialization patterns for Ruby with automatic dependency reso
|
|
4
4
|
|
5
5
|
[](https://www.ruby-lang.org/)
|
6
6
|
[](https://badge.fury.io/rb/lazy_init)
|
7
|
-
[](https://rubygems.org/gems/lazy_init)
|
8
8
|
|
9
9
|
## Table of Contents
|
10
10
|
|
@@ -164,20 +164,20 @@ lazy_attr_reader(name, **options, &block)
|
|
164
164
|
Defines a thread-safe lazy-initialized attribute.
|
165
165
|
#### Parameters:
|
166
166
|
|
167
|
-
- name (Symbol/String): Attribute name
|
168
|
-
- timeout (Numeric, optional): Timeout in seconds for computation
|
169
|
-
- depends_on (Array<Symbol>/Symbol, optional): Dependencies to resolve first
|
170
|
-
- block (Proc): Computation block
|
167
|
+
- `name` (Symbol/String): Attribute name
|
168
|
+
- `timeout` (Numeric, optional): Timeout in seconds for computation
|
169
|
+
- `depends_on` (Array<Symbol>/Symbol, optional): Dependencies to resolve first
|
170
|
+
- `block` (Proc): Computation block
|
171
171
|
|
172
172
|
#### Generated Methods:
|
173
173
|
|
174
|
-
-
|
175
|
-
-
|
176
|
-
- reset_#{name}
|
174
|
+
- `#{name}`: Returns computed value
|
175
|
+
- `#{name}_computed?`: Returns true if value has been computed
|
176
|
+
- `reset_#{name}!`: Resets to uncomputed state
|
177
177
|
|
178
178
|
#### Examples:
|
179
179
|
```ruby
|
180
|
-
|
180
|
+
class ServiceManager
|
181
181
|
extend LazyInit
|
182
182
|
|
183
183
|
# Simple lazy attribute
|
@@ -210,12 +210,12 @@ lazy_class_variable(name, **options, &block)
|
|
210
210
|
Defines a thread-safe lazy-initialized class variable shared across all instances.
|
211
211
|
#### Parameters:
|
212
212
|
|
213
|
-
- Same as lazy_attr_reader
|
213
|
+
- Same as `lazy_attr_reader`
|
214
214
|
|
215
215
|
#### Generated Methods:
|
216
216
|
|
217
|
-
- Class-level: ClassName.#{name}
|
218
|
-
- Instance-level:
|
217
|
+
- Class-level: `ClassName.#{name}`, `ClassName.#{name}\_computed?`, `ClassName.reset\_#{name}!`
|
218
|
+
- Instance-level: `#{name}`, `#{name}\_computed?`, `reset\_#{name}!` (delegates to class)
|
219
219
|
|
220
220
|
#### Example:
|
221
221
|
```ruby
|
@@ -244,7 +244,7 @@ class DataProcessor
|
|
244
244
|
include LazyInit # Note: include, not extend
|
245
245
|
end
|
246
246
|
```
|
247
|
-
|
247
|
+
`lazy(&block)`
|
248
248
|
|
249
249
|
Creates a standalone lazy value container.
|
250
250
|
```ruby
|
@@ -255,13 +255,13 @@ end
|
|
255
255
|
```
|
256
256
|
|
257
257
|
|
258
|
-
|
258
|
+
`lazy_once(**options, &block)`
|
259
259
|
|
260
260
|
Method-scoped lazy initialization with automatic cache key generation.
|
261
261
|
#### Parameters:
|
262
262
|
|
263
|
-
- max_entries (Integer): Maximum cache entries before LRU eviction
|
264
|
-
- ttl (Numeric): Time-to-live in seconds for cache entries
|
263
|
+
- `max_entries` (Integer): Maximum cache entries before LRU eviction
|
264
|
+
- `ttl` (Numeric): Time-to-live in seconds for cache entries
|
265
265
|
|
266
266
|
#### Example:
|
267
267
|
``` ruby
|
@@ -276,11 +276,11 @@ class DataAnalyzer
|
|
276
276
|
end
|
277
277
|
```
|
278
278
|
|
279
|
-
|
279
|
+
`clear_lazy_once_values!`
|
280
280
|
|
281
281
|
Clears all cached lazy_once values for the instance.
|
282
282
|
|
283
|
-
|
283
|
+
`lazy_once_statistics`
|
284
284
|
|
285
285
|
Returns cache statistics for debugging and monitoring.
|
286
286
|
|
@@ -308,15 +308,15 @@ end
|
|
308
308
|
|
309
309
|
#### Configuration Options:
|
310
310
|
|
311
|
-
-
|
312
|
-
-
|
313
|
-
-
|
311
|
+
- `default_timeout`: Default timeout for all lazy attributes (default: nil)
|
312
|
+
- `max_lazy_once_entries`: Maximum entries in lazy_once cache (default: 1000)
|
313
|
+
- `lazy_once_ttl`: Default TTL for lazy_once entries (default: nil)
|
314
314
|
|
315
315
|
## Advanced Usage
|
316
316
|
### Dependency Resolution
|
317
317
|
#### LazyInit automatically resolves dependencies in the correct order:
|
318
318
|
```ruby
|
319
|
-
|
319
|
+
class ComplexService
|
320
320
|
extend LazyInit
|
321
321
|
|
322
322
|
lazy_attr_reader :config do
|
@@ -534,7 +534,7 @@ Realistic benchmark results (x86_64-darwin19, Ruby 3.0.2):
|
|
534
534
|
- Initial computation: ~identical (LazyInit setup overhead negligible)
|
535
535
|
- Cached access: 3.5x slower than manual ||=
|
536
536
|
-100,000 calls: Manual 13ms, LazyInit 45ms
|
537
|
-
- In practice: For expensive operations (5-50ms),
|
537
|
+
- In practice: For expensive operations (5-50ms), 0.0004ms per call overhead is negligible.
|
538
538
|
- Trade-off: 3.5x cached access cost for 100% thread safety
|
539
539
|
|
540
540
|
[Full details can be found here](https://github.com/N3BCKN/lazy_init/blob/main/benchmarks/benchmark_performance.rb)
|
@@ -735,7 +735,7 @@ A: Minimal - about 1 mutex + 3 instance variables per lazy attribute.
|
|
735
735
|
|
736
736
|
Q: Can I use lazy_attr_reader with private methods?
|
737
737
|
|
738
|
-
A: Yes,
|
738
|
+
A: Yes, generated methods respect the same visibility as where they're defined.
|
739
739
|
|
740
740
|
Q: How do I debug dependency resolution issues?
|
741
741
|
|
@@ -746,7 +746,7 @@ Q: Does this work with inheritance?
|
|
746
746
|
A: Yes, lazy attributes are inherited and can be overridden in subclasses.
|
747
747
|
## Contributing
|
748
748
|
|
749
|
-
1. Fork
|
749
|
+
1. Fork repository
|
750
750
|
2. Create your feature branch (git checkout -b my-new-feature)
|
751
751
|
3. Write tests for your changes
|
752
752
|
4. Ensure all tests pass (bundle exec rspec)
|
@@ -762,4 +762,4 @@ bundle install
|
|
762
762
|
bundle exec rspec # Run tests
|
763
763
|
```
|
764
764
|
## License
|
765
|
-
|
765
|
+
Gem is available as open source under the terms of the MIT License.
|
data/lib/lazy_init/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_init
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Konstanty Koszewski
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-07-
|
11
|
+
date: 2025-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -90,6 +90,7 @@ extra_rdoc_files: []
|
|
90
90
|
files:
|
91
91
|
- ".gitignore"
|
92
92
|
- ".rspec"
|
93
|
+
- ".yardopts"
|
93
94
|
- CHANGELOG.md
|
94
95
|
- GEMFILE
|
95
96
|
- LICENSE
|