lazy_object 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lazy_object.rb +6 -2
- data/spec/lazy_object_spec.rb +18 -0
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da5663885eb9cd9651c97f3433ffe9b0b69f2f305bbf8d2d4c2a3fa29364cfbb
|
4
|
+
data.tar.gz: 5dcd597961dfd4e68160f831e2730099ffd7e9969414d6eebb6aa95470bfa91a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6fab73e859d5c5a19dc4588e519864762e16a5548b8454d0b758240f482493136d3527b9e3f04af4914f127a2bf23de582164801221dfe7f16ee8ec673a11a41
|
7
|
+
data.tar.gz: 57f3aa04c035ba86bb4431864a33e706af8fa225c13b564cbb60fe4e564107b5af8c838ff6448ead2ba0d10c3af27069d0e2dae1125a6805844aa58948f63478
|
data/lib/lazy_object.rb
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
# lazy.get_expensive_results(foo, bar) # Initializes VeryExpensiveObject and calls 'get_expensive_results' on it, passing in foo and bar
|
11
11
|
class LazyObject < BasicObject
|
12
12
|
def self.version
|
13
|
-
'0.
|
13
|
+
'0.2.0'
|
14
14
|
end
|
15
15
|
|
16
16
|
def initialize(&callable)
|
@@ -31,7 +31,11 @@ class LazyObject < BasicObject
|
|
31
31
|
|
32
32
|
# Cached target object.
|
33
33
|
def __target_object__
|
34
|
-
@__target_object__
|
34
|
+
if defined?(@__target_object__)
|
35
|
+
@__target_object__
|
36
|
+
else
|
37
|
+
@__target_object__ = @__callable__.call
|
38
|
+
end
|
35
39
|
end
|
36
40
|
|
37
41
|
# Forwards all method calls to the target object.
|
data/spec/lazy_object_spec.rb
CHANGED
@@ -71,6 +71,24 @@ RSpec.describe LazyObject do
|
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
+
context "when target object evaluates to a falsy value" do
|
75
|
+
let(:lazy_object) { LazyObject.new { TargetObject.new } }
|
76
|
+
|
77
|
+
it "evaluates the block exactly once for false" do
|
78
|
+
TargetObject.should_receive(:new).once.and_return(false)
|
79
|
+
3.times do
|
80
|
+
expect(lazy_object).to eq(false)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
it "evaluates the block exactly once for nil" do
|
85
|
+
TargetObject.should_receive(:new).once.and_return(nil)
|
86
|
+
3.times do
|
87
|
+
expect(lazy_object).to eq(nil)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
74
92
|
context "equality operators" do
|
75
93
|
it "should return correct value when comparing" do
|
76
94
|
one = LazyObject.new { 1 }
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arthur Shagall
|
8
8
|
- Sergey Potapov
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-08-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -77,7 +77,7 @@ homepage: ''
|
|
77
77
|
licenses:
|
78
78
|
- MIT
|
79
79
|
metadata: {}
|
80
|
-
post_install_message:
|
80
|
+
post_install_message:
|
81
81
|
rdoc_options: []
|
82
82
|
require_paths:
|
83
83
|
- lib
|
@@ -92,8 +92,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
|
-
rubygems_version: 3.2.
|
96
|
-
signing_key:
|
95
|
+
rubygems_version: 3.2.33
|
96
|
+
signing_key:
|
97
97
|
specification_version: 4
|
98
98
|
summary: Lazily initialized object wrapper.
|
99
99
|
test_files:
|