simple-lazy 1.0.0
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 +7 -0
- data/.gitignore +1 -0
- data/Gemfile +2 -0
- data/LICENSE +2 -0
- data/README.md +29 -0
- data/Rakefile +3 -0
- data/bin/console +8 -0
- data/lib/simple/lazy.rb +51 -0
- data/simple-lazy.gemspec +13 -0
- data/test/simple/lazy_test.rb +40 -0
- metadata +52 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1f1bf156abca48a25effef7bf5ec2d6bab5e61cdbf6ebeb33844fdf06be05539
|
4
|
+
data.tar.gz: 7f1982b3a37fb5b2a2c0352c7dae1c3744f9d30209bd518739066767753530ec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9571b50538dad79b871db6dc08026db758d732047f203358a1e6175305c1e689f54add0ee800657f9e9639fd5a6327cee9d6703252e3f9d75128a8db8efd8ddf
|
7
|
+
data.tar.gz: be241db829ca6c92efab15753a7c9259dbb6c013db24e7820f73ae84f210a7d44b01e955559e29a6b2141c0f0f34b8a1cf1a4c62b5b067b0d9fedd4160ba116a
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gemfile.lock
|
data/Gemfile
ADDED
data/LICENSE
ADDED
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Simple::Lazy
|
2
|
+
|
3
|
+
lazily load objects
|
4
|
+
|
5
|
+
## installation
|
6
|
+
|
7
|
+
$ gem install simple-lazy
|
8
|
+
|
9
|
+
**or**
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'simple-lazy'
|
13
|
+
```
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
user = Simple::Lazy.new(1) { |id| User.find(id) } # #<Lazy @value=1 cached?=false>
|
21
|
+
user.value # => 1
|
22
|
+
user.cached? # => false
|
23
|
+
user.username # => "elonmusk"
|
24
|
+
user.tweets.count # => 7419
|
25
|
+
user.value # => 1
|
26
|
+
user.cached? # => true
|
27
|
+
user.inspect # => #<Lazy @value=1 cached?=false>
|
28
|
+
user.to_s # => "@elonmusk"
|
29
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/lib/simple/lazy.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
module Simple
|
2
|
+
class Lazy
|
3
|
+
VERSION = "1.0.0"
|
4
|
+
|
5
|
+
attr_reader :value
|
6
|
+
|
7
|
+
def initialize(value, &block)
|
8
|
+
@block = block
|
9
|
+
@value = value
|
10
|
+
@cached = nil
|
11
|
+
@is_cached = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def hash
|
15
|
+
value.hash
|
16
|
+
end
|
17
|
+
|
18
|
+
def eql?(other)
|
19
|
+
other.is_a?(Simple::Lazy) ? value == other.value : value == other
|
20
|
+
end
|
21
|
+
|
22
|
+
def <=>(other)
|
23
|
+
other.is_a?(Simple::Lazy) ? value <=> other.value : value <=> other
|
24
|
+
end
|
25
|
+
|
26
|
+
def method_missing(method, *args, &block)
|
27
|
+
cached.public_send(method, *args, &block)
|
28
|
+
end
|
29
|
+
|
30
|
+
def cached
|
31
|
+
unless @cached
|
32
|
+
@cached = @block.call(value)
|
33
|
+
@is_cached = true
|
34
|
+
end
|
35
|
+
|
36
|
+
@cached
|
37
|
+
end
|
38
|
+
|
39
|
+
def cached?
|
40
|
+
@is_cached
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_s
|
44
|
+
cached.to_s
|
45
|
+
end
|
46
|
+
|
47
|
+
def inspect
|
48
|
+
"#<Simple::Lazy @value=#{@value.inspect} cached?=#{cached?.inspect}>"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/simple-lazy.gemspec
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require_relative "lib/simple/lazy"
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "simple-lazy"
|
5
|
+
spec.version = Simple::Lazy::VERSION
|
6
|
+
spec.authors = ["localhostdotdev"]
|
7
|
+
spec.email = ["localhostdotdev@protonmail.com"]
|
8
|
+
spec.summary = "lazy loading, e.g. Simple::Lazy.new(1) { |id| User.find(id) }"
|
9
|
+
spec.homepage = "https://github.com/simple-updates/simple-lazy"
|
10
|
+
spec.license = "MIT"
|
11
|
+
spec.files = `git ls-files`.split("\n")
|
12
|
+
spec.require_paths = ["lib"]
|
13
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "minitest/autorun"
|
2
|
+
|
3
|
+
class TestSimpleLazy < Minitest::Test
|
4
|
+
def test_1
|
5
|
+
assert_equal(1, Simple::Lazy.new(1) { }.value)
|
6
|
+
assert_equal(false, Simple::Lazy.new(1) { }.cached?)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_2
|
10
|
+
user = Simple::Lazy.new(1) { |id| Struct.new(:name).new("a#{id}") }
|
11
|
+
|
12
|
+
assert_equal(1, user.value)
|
13
|
+
assert_equal(false, user.cached?)
|
14
|
+
assert_equal("a1", user.name)
|
15
|
+
assert_equal(true, user.cached?)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_3
|
19
|
+
error = nil
|
20
|
+
|
21
|
+
begin
|
22
|
+
lazy = Simple::Lazy.new(1) { |id| raise 'oops' }
|
23
|
+
lazy.inspect
|
24
|
+
lazy.value
|
25
|
+
lazy.cached?
|
26
|
+
lazy == lazy
|
27
|
+
[lazy, lazy].sort
|
28
|
+
lazy.hash
|
29
|
+
rescue => e
|
30
|
+
error = e
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_nil(error)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_4
|
37
|
+
lazy = Simple::Lazy.new(1) { |id| "oh" }
|
38
|
+
assert_equal("oh", lazy.to_s)
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple-lazy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- localhostdotdev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-24 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- localhostdotdev@protonmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- Gemfile
|
22
|
+
- LICENSE
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- bin/console
|
26
|
+
- lib/simple/lazy.rb
|
27
|
+
- simple-lazy.gemspec
|
28
|
+
- test/simple/lazy_test.rb
|
29
|
+
homepage: https://github.com/simple-updates/simple-lazy
|
30
|
+
licenses:
|
31
|
+
- MIT
|
32
|
+
metadata: {}
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubygems_version: 3.0.3
|
49
|
+
signing_key:
|
50
|
+
specification_version: 4
|
51
|
+
summary: lazy loading, e.g. Simple::Lazy.new(1) { |id| User.find(id) }
|
52
|
+
test_files: []
|