mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add untyped ValueProxy base class
This commit is contained in:
parent
0af4c2f54f
commit
623033623a
2 changed files with 13 additions and 2 deletions
|
@ -1,8 +1,10 @@
|
||||||
|
require "./value_proxy"
|
||||||
|
|
||||||
module Spectator
|
module Spectator
|
||||||
# Lazy initialization of a value.
|
# Lazy initialization of a value.
|
||||||
# Constructs a value only once by calling a `Proc`.
|
# Constructs a value only once by calling a `Proc`.
|
||||||
# The value is then stored and reused - the `Proc` is only called once.
|
# The value is then stored and reused - the `Proc` is only called once.
|
||||||
struct Lazy(T)
|
class LazyValueProxy(T) < ValueProxy
|
||||||
@value_or_block : Proc(T) | T
|
@value_or_block : Proc(T) | T
|
||||||
|
|
||||||
# Creates a lazy instance.
|
# Creates a lazy instance.
|
||||||
|
@ -17,7 +19,7 @@ module Spectator
|
||||||
# Retrieves the lazy initialized value.
|
# Retrieves the lazy initialized value.
|
||||||
# The first call to this method will create the value.
|
# The first call to this method will create the value.
|
||||||
# Subsequent calls will return the same value.
|
# Subsequent calls will return the same value.
|
||||||
def value : T
|
def value
|
||||||
if value = @value_or_block.as?(T)
|
if value = @value_or_block.as?(T)
|
||||||
return value
|
return value
|
||||||
else
|
else
|
9
src/spectator/value_proxy.cr
Normal file
9
src/spectator/value_proxy.cr
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
module Spectator
|
||||||
|
# Base class for proxying test values to examples.
|
||||||
|
# This abstraction is required for inferring types.
|
||||||
|
# The `DSL#let` macro makes heavy use of this.
|
||||||
|
protected abstract class ValueProxy
|
||||||
|
# Retrieves the underlying value.
|
||||||
|
abstract def value
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue