lazy_object 0.0.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9fa7eb3857a682996ac6ffe73f5e8cd71f5a4d4779226328931bd9db0c36da25
4
- data.tar.gz: 97954d2a1890e404532550032e3eb6ae2b11b9e4c9aad48a05948b11e409c1a4
3
+ metadata.gz: da5663885eb9cd9651c97f3433ffe9b0b69f2f305bbf8d2d4c2a3fa29364cfbb
4
+ data.tar.gz: 5dcd597961dfd4e68160f831e2730099ffd7e9969414d6eebb6aa95470bfa91a
5
5
  SHA512:
6
- metadata.gz: 434a3bdd6b382e2a1ec48cd365dda8c97fc4ea2fa214348e4d2815e5e93e5715c5a9656384e63f899c21c65b343ffde320016b9cb07d564ed61f5d684ebcc446
7
- data.tar.gz: b7c2f19501981b4eea60409548856241d27fce4292dd9ec31448a144eb5e8ce1110f4fbf1b8bf6b42156b181d189ad4f25d180dca7d2d84969082ccd18017a92
6
+ metadata.gz: 6fab73e859d5c5a19dc4588e519864762e16a5548b8454d0b758240f482493136d3527b9e3f04af4914f127a2bf23de582164801221dfe7f16ee8ec673a11a41
7
+ data.tar.gz: 57f3aa04c035ba86bb4431864a33e706af8fa225c13b564cbb60fe4e564107b5af8c838ff6448ead2ba0d10c3af27069d0e2dae1125a6805844aa58948f63478
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.4
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 = "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"
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.3'
13
+ '0.2.0'
14
14
  end
15
15
 
16
16
  def initialize(&callable)
17
17
  @__callable__ = callable
18
18
  end
19
19
 
20
- def ==(item)
21
- __target_object__ == item
20
+ def ==(other)
21
+ __target_object__ == other
22
22
  end
23
23
 
24
- def !=(item)
25
- __target_object__ != item
24
+ def !=(other)
25
+ __target_object__ != other
26
26
  end
27
27
 
28
28
  def !
@@ -31,11 +31,15 @@ class LazyObject < BasicObject
31
31
 
32
32
  # Cached target object.
33
33
  def __target_object__
34
- @__target_object__ ||= @__callable__.call
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.
38
- def method_missing(method_name, *args, &block)
39
- __target_object__.send(method_name, *args, &block)
42
+ def method_missing(method_name, ...)
43
+ __target_object__.send(method_name, ...)
40
44
  end
41
45
  end
@@ -8,26 +8,89 @@ 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 = TargetObject.new
27
+ targ = TargetObject.new
16
28
  targ.value = :foo
17
29
  targ
18
30
  end
19
31
  }
20
32
 
21
- it 'should call the init block and forward methods to the target object' do
22
- expect( lazy_object.value ).to eq(:foo)
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( lazy_object.value ).to eq(:bar)
36
+ expect(lazy_object.value).to eq(:bar)
37
+
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 } }
25
56
 
26
- expect( lazy_object.method_with_yield(:baz) { |foo| "--#{foo}--" } ).to eq('--baz--')
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
72
+ end
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
27
90
  end
28
91
 
29
92
  context "equality operators" do
30
- it 'should return correct value when comparing' do
93
+ it "should return correct value when comparing" do
31
94
  one = LazyObject.new { 1 }
32
95
  expect(one).to eq(1)
33
96
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require "lazy_object"
2
+
1
3
  # This file was generated by the `rspec --init` command. Conventionally, all
2
4
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
5
  # The generated `.rspec` file contains `--require spec_helper` which will cause this
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.0.3
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: 2019-06-19 00:00:00.000000000 Z
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
@@ -85,16 +85,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - ">="
87
87
  - !ruby/object:Gem::Version
88
- version: '0'
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
- rubyforge_project:
96
- rubygems_version: 2.7.9
97
- signing_key:
95
+ rubygems_version: 3.2.33
96
+ signing_key:
98
97
  specification_version: 4
99
98
  summary: Lazily initialized object wrapper.
100
99
  test_files: