Skip to content

Registry

Jamie English edited this page Jul 21, 2017 · 4 revisions

Speculation allows you to store your specs in a global registry. The following demonstrates adding a spec named :"MyApp/id" to the registry:

S.def :"MyApp/id", S.or(:i => Integer, :s => String)

Now we've registered our spec we're free to use it anywhere in our application:

S.valid?(:"MyApp/id", "123") # => true
S.valid?(:"MyApp/id", 123)   # => true
S.valid?(:"MyApp/id", 123.0) # => false
S.valid?(:"MyApp/id", nil)   # => false

We can also refer to this spec by name when defining other specs:

S.def :"MyApp/maybe_id", S.nilable(:"MyApp/id")
S.def :"MyApp/ids", S.coll_of(:"MyApp/id")

If you're wondering "why the weird symbols?", see Namespaced Symbols for an explanation.

Clone this wiki locally