Add untyped ValueProxy base class

This commit is contained in:
Michael Miller 2018-08-25 18:36:32 -06:00
parent 0af4c2f54f
commit 623033623a
2 changed files with 13 additions and 2 deletions

View File

@ -1,8 +1,10 @@
require "./value_proxy"
module Spectator
# Lazy initialization of a value.
# Constructs a value only once by calling a `Proc`.
# 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
# Creates a lazy instance.
@ -17,7 +19,7 @@ module Spectator
# Retrieves the lazy initialized value.
# The first call to this method will create the value.
# Subsequent calls will return the same value.
def value : T
def value
if value = @value_or_block.as?(T)
return value
else

View 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