lazy_object 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/lazy_object.gemspec +9 -8
- data/lib/lazy_object.rb +7 -7
- data/spec/lazy_object_spec.rb +51 -6
- data/spec/spec_helper.rb +2 -0
- metadata +4 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b3f3a27effb74769735f2bda4d82dc2c4c0a1d83d7ec045fa5271fa6159d73d
|
4
|
+
data.tar.gz: cc398a8b47a1a0dad641f49ce396e16a7004b4f2c08d87c9d6fb85d14ed2ba1e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71ca10f6211c502a834a49b3bab1d599bf9c253174f3b33d83f33f566204e4e638c89fed4148b04eafef3223d52d389005bdc186f96fccc861d24eb61a728f32
|
7
|
+
data.tar.gz: '0299ef027da70df518d9cdb2ab4d7bae52d806b78ea4dd6edb7158513a291e62ce1c3ac6084f0991254d43dbfd1f97b7240eb435333d9f809ffc7f3775cc07b5'
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
3.0
|
data/lazy_object.gemspec
CHANGED
@@ -4,14 +4,15 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'lazy_object'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name
|
8
|
-
spec.version
|
9
|
-
spec.authors
|
10
|
-
spec.email
|
11
|
-
spec.summary
|
12
|
-
spec.description
|
13
|
-
spec.homepage
|
14
|
-
spec.license
|
7
|
+
spec.name = "lazy_object"
|
8
|
+
spec.version = LazyObject.version
|
9
|
+
spec.authors = ["Arthur Shagall", "Sergey Potapov"]
|
10
|
+
spec.email = ["arthur.shagall@gmail.com"]
|
11
|
+
spec.summary = %q{Lazily initialized object wrapper.}
|
12
|
+
spec.description = %q{It's an object wrapper that forwards all calls to the reference object. This object is not created until the first method dispatch.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 3.0.0"
|
15
16
|
|
16
17
|
spec.files = `git ls-files -z`.split("\x0")
|
17
18
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
data/lib/lazy_object.rb
CHANGED
@@ -10,19 +10,19 @@
|
|
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.0
|
13
|
+
'0.1.0'
|
14
14
|
end
|
15
15
|
|
16
16
|
def initialize(&callable)
|
17
17
|
@__callable__ = callable
|
18
18
|
end
|
19
19
|
|
20
|
-
def ==(
|
21
|
-
__target_object__ ==
|
20
|
+
def ==(other)
|
21
|
+
__target_object__ == other
|
22
22
|
end
|
23
23
|
|
24
|
-
def !=(
|
25
|
-
__target_object__ !=
|
24
|
+
def !=(other)
|
25
|
+
__target_object__ != other
|
26
26
|
end
|
27
27
|
|
28
28
|
def !
|
@@ -35,7 +35,7 @@ class LazyObject < BasicObject
|
|
35
35
|
end
|
36
36
|
|
37
37
|
# Forwards all method calls to the target object.
|
38
|
-
def method_missing(method_name,
|
39
|
-
__target_object__.send(method_name,
|
38
|
+
def method_missing(method_name, ...)
|
39
|
+
__target_object__.send(method_name, ...)
|
40
40
|
end
|
41
41
|
end
|
data/spec/lazy_object_spec.rb
CHANGED
@@ -8,26 +8,71 @@ RSpec.describe LazyObject do
|
|
8
8
|
def method_with_yield(foo)
|
9
9
|
yield foo
|
10
10
|
end
|
11
|
+
|
12
|
+
def method_with_kwargs(**kwargs)
|
13
|
+
kwargs
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_with_kwargs_and_block(**kwargs, &block)
|
17
|
+
block.call **kwargs
|
18
|
+
end
|
19
|
+
|
20
|
+
def method_with_kwargs_and_yield(**kwargs)
|
21
|
+
yield **kwargs
|
22
|
+
end
|
11
23
|
end
|
12
24
|
|
13
25
|
let(:lazy_object) {
|
14
26
|
LazyObject.new do
|
15
|
-
targ
|
27
|
+
targ = TargetObject.new
|
16
28
|
targ.value = :foo
|
17
29
|
targ
|
18
30
|
end
|
19
31
|
}
|
20
32
|
|
21
|
-
it
|
22
|
-
expect(
|
33
|
+
it "should call the init block and forward methods to the target object" do
|
34
|
+
expect(lazy_object.value).to eq(:foo)
|
23
35
|
lazy_object.value = :bar
|
24
|
-
expect(
|
36
|
+
expect(lazy_object.value).to eq(:bar)
|
25
37
|
|
26
|
-
expect(
|
38
|
+
expect(
|
39
|
+
lazy_object.method_with_yield(:baz) { |foo| "--#{foo}--" }
|
40
|
+
).to eq("--baz--")
|
41
|
+
end
|
42
|
+
|
43
|
+
context "when target object methods use keyword arguments" do
|
44
|
+
subject(:target_method) { lazy_object.method_with_kwargs(foo: "bar") }
|
45
|
+
|
46
|
+
let(:lazy_object) { LazyObject.new { TargetObject.new } }
|
47
|
+
|
48
|
+
it "should not raise an exception and properly delegate to the target object" do
|
49
|
+
expect { target_method }.not_to raise_exception
|
50
|
+
expect(target_method).to eq({ foo: "bar" })
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "when target object uses kwargs and a block" do
|
55
|
+
let(:lazy_object) { LazyObject.new { TargetObject.new } }
|
56
|
+
|
57
|
+
it "will delegate the call to the block" do
|
58
|
+
result = lazy_object.method_with_kwargs_and_block(foo: "bar") { |foo:| { bar: foo.reverse } }
|
59
|
+
|
60
|
+
expect(result).to eq({ bar: "rab" })
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "when target object uses kwargs and an implicit block" do
|
65
|
+
let(:lazy_object) { LazyObject.new { TargetObject.new } }
|
66
|
+
|
67
|
+
it "will delegate the call to the block" do
|
68
|
+
result = lazy_object.method_with_kwargs_and_yield(foo: "bar") { |foo:| { bar: foo.reverse } }
|
69
|
+
|
70
|
+
expect(result).to eq({ bar: "rab" })
|
71
|
+
end
|
27
72
|
end
|
28
73
|
|
29
74
|
context "equality operators" do
|
30
|
-
it
|
75
|
+
it "should return correct value when comparing" do
|
31
76
|
one = LazyObject.new { 1 }
|
32
77
|
expect(one).to eq(1)
|
33
78
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arthur Shagall
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-12-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -85,15 +85,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
85
|
requirements:
|
86
86
|
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
|
-
version:
|
88
|
+
version: 3.0.0
|
89
89
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
90
|
requirements:
|
91
91
|
- - ">="
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: '0'
|
94
94
|
requirements: []
|
95
|
-
|
96
|
-
rubygems_version: 2.7.9
|
95
|
+
rubygems_version: 3.2.3
|
97
96
|
signing_key:
|
98
97
|
specification_version: 4
|
99
98
|
summary: Lazily initialized object wrapper.
|