Add LazyObject class.
This commit is contained in:
parent
b5548f9548
commit
428bc9c7a3
14
Library/Homebrew/lazy_object.rb
Normal file
14
Library/Homebrew/lazy_object.rb
Normal file
@ -0,0 +1,14 @@
|
||||
class LazyObject < Delegator
|
||||
def initialize(&callable)
|
||||
super(callable)
|
||||
end
|
||||
|
||||
def __getobj__
|
||||
return @__delegate__ if defined?(@__delegate__)
|
||||
@__delegate__ = @__callable__.call
|
||||
end
|
||||
|
||||
def __setobj__(callable)
|
||||
@__callable__ = callable
|
||||
end
|
||||
end
|
||||
35
Library/Homebrew/test/lazy_object_spec.rb
Normal file
35
Library/Homebrew/test/lazy_object_spec.rb
Normal file
@ -0,0 +1,35 @@
|
||||
require "lazy_object"
|
||||
|
||||
describe LazyObject do
|
||||
describe "#initialize" do
|
||||
it "does not evaluate the block" do
|
||||
expect { |block|
|
||||
described_class.new(&block)
|
||||
}.not_to yield_control
|
||||
end
|
||||
end
|
||||
|
||||
describe "when receiving a message" do
|
||||
it "evaluates the block" do
|
||||
expect(described_class.new { 42 }.to_s).to eq "42"
|
||||
end
|
||||
end
|
||||
|
||||
describe "#!" do
|
||||
it "delegates to the underlying object" do
|
||||
expect(!(described_class.new { false })).to be true
|
||||
end
|
||||
end
|
||||
|
||||
describe "#!=" do
|
||||
it "delegates to the underlying object" do
|
||||
expect(described_class.new { 42 }).not_to eq 13
|
||||
end
|
||||
end
|
||||
|
||||
describe "#==" do
|
||||
it "delegates to the underlying object" do
|
||||
expect(described_class.new { 42 }).to eq 42
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user