Skip to content

Commit 43c047f

Browse files
committed
In docs, define types module before (re)opening it
1 parent db2a99c commit 43c047f

13 files changed

+59
-23
lines changed

docsite/source/array-with-member.html.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ name: dry-types
77
The built-in array type supports defining the member's type:
88

99
``` ruby
10+
Types = Dry.Types()
11+
1012
PostStatuses = Types::Array.of(Types::Coercible::String)
1113

1214
PostStatuses[[:foo, :bar]] # ["foo", "bar"]

docsite/source/built-in-types.html.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Built-in types are grouped under 6 categories:
1515

1616
### Categories
1717

18-
Assuming you included `Dry::Types` ([see instructions](docs::getting-started)) in a module called `Types`:
18+
Assuming you've defined your own `Types` module ([see instructions](docs::getting-started)):
1919

2020
* Nominal types:
2121
- `Types::Nominal::Any`

docsite/source/constraints.html.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ All types support the constraints API, but not all constraints are suitable for
1111
Under the hood it uses [`dry-logic`](/gems/dry-logic) and all of its predicates are supported.
1212

1313
``` ruby
14+
Types = Dry.Types()
15+
1416
string = Types::String.constrained(min_size: 3)
1517

1618
string['foo']

docsite/source/custom-types.html.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ There are a bunch of helpers for building your own types based on existing class
1111
`Types.Instance` builds a type that checks if a value has the given class.
1212

1313
```ruby
14+
Types = Dry.Types()
15+
1416
range_type = Types.Instance(Range)
1517
range_type[1..2] # => 1..2
1618
```
@@ -20,6 +22,8 @@ range_type[1..2] # => 1..2
2022
`Types.Value` builds a type that checks a value for equality (using `==`).
2123

2224
```ruby
25+
Types = Dry.Types()
26+
2327
valid = Types.Value('valid')
2428
valid['valid'] # => 'valid'
2529
valid['invalid']
@@ -31,6 +35,8 @@ valid['invalid']
3135
`Types.Constant` builds a type that checks a value for identity (using `equal?`).
3236

3337
```ruby
38+
Types = Dry.Types()
39+
3440
valid = Types.Constant(:valid)
3541
valid[:valid] # => :valid
3642
valid[:invalid]
@@ -42,6 +48,8 @@ valid[:invalid]
4248
`Types.Constructor` builds a new constructor type for the given class. By default uses the `new` method as a constructor.
4349

4450
```ruby
51+
Types = Dry.Types()
52+
4553
user_type = Types.Constructor(User)
4654

4755
# It is equivalent to User.new(name: 'John')
@@ -59,6 +67,8 @@ user_type = Types.Constructor(User) { |values| User.new(values) }
5967
`Types.Nominal` wraps the given class with a simple definition without any behavior attached.
6068

6169
```ruby
70+
Types = Dry.Types()
71+
6272
int = Types.Nominal(Integer)
6373
int[1] # => 1
6474

@@ -71,6 +81,8 @@ int['one'] # => 'one'
7181
`Types.Hash` builds a new hash schema.
7282

7383
```ruby
84+
Types = Dry.Types()
85+
7486
# In the full form
7587
Types::Hash.schema(name: Types::String, age: Types::Coercible::Integer)
7688

@@ -83,6 +95,8 @@ Types.Hash(name: Types::String, age: Types::Coercible::Integer)
8395
`Types.Array` is a shortcut for `Types::Array.of`
8496

8597
```ruby
98+
Types = Dry.Types()
99+
86100
ListOfStrings = Types.Array(Types::String)
87101
```
88102

@@ -91,6 +105,8 @@ ListOfStrings = Types.Array(Types::String)
91105
`Types.Interface` builds a type that checks a value responds to given methods.
92106

93107
```ruby
108+
Types = Dry.Types()
109+
94110
Callable = Types.Interface(:call)
95111
Contact = Types.Interface(:name, :phone)
96-
```
112+
```

docsite/source/default-values.html.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ name: dry-types
77
A type with a default value will return the configured value when the input is not defined:
88

99
``` ruby
10+
Types = Dry.Types()
11+
1012
PostStatus = Types::String.default('draft')
1113

1214
PostStatus[] # "draft"

docsite/source/enum.html.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ In many cases you may want to define an enum. For example, in a blog application
1010
require 'dry-types'
1111
require 'dry-struct'
1212

13-
module Types
14-
include Dry.Types()
15-
end
13+
Types = Dry.Types()
1614

1715
class Post < Dry::Struct
1816
Statuses = Types::String.enum('draft', 'published', 'archived')
@@ -42,10 +40,10 @@ Note that if you want to define an enum type with a default, you must call `.def
4240

4341
```ruby
4442
# this is the correct usage:
45-
Dry::Types::String.default('red').enum('blue', 'green', 'red')
43+
Types::String.default('red').enum('blue', 'green', 'red')
4644

4745
# this will raise an error:
48-
Dry::Types::String.enum('blue', 'green', 'red').default('red')
46+
Types::String.enum('blue', 'green', 'red').default('red')
4947
```
5048

5149
### Mappings

docsite/source/extensions/maybe.html.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ The [dry-monads gem](/gems/dry-monads/) provides approach to handling optional v
1313
require 'dry-types'
1414

1515
Dry::Types.load_extensions(:maybe)
16-
module Types
17-
include Dry.Types()
18-
end
16+
17+
Types = Dry.Types()
1918
```
2019

2120
2. Append `.maybe` to a _Type_ to return a _Monad_ object

docsite/source/getting-started.html.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ name: dry-types
66

77
### Using `Dry::Types` in Your Application
88

9-
1. Make `Dry::Types` available to the application by creating a namespace that includes `Dry::Types`:
9+
1. Make the base types available to your application by defining your own module built from `Dry.Types()`:
1010

1111
```ruby
12-
module Types
13-
include Dry.Types()
14-
end
12+
Types = Dry.Types()
1513
```
16-
17-
2. Reload the environment, & type `Types::Coercible::String` in the ruby console to confirm it worked:
14+
15+
2. Reload the environment, & enter `Types::Coercible::String` in your ruby console to confirm it worked:
1816

1917
``` ruby
2018
Types::Coercible::String
@@ -31,15 +29,16 @@ name: dry-types
3129
end
3230
```
3331
34-
2. Define [Custom Types](docs::custom-types) in the `Types` module, then pass the name & type to `attribute`:
32+
2. Define [Custom Types](docs::custom-types) in your types module, then pass the name & type to `attribute`:
3533
3634
```ruby
35+
Types = Dry.Types()
36+
3737
module Types
38-
include Dry.Types()
39-
4038
Email = String.constrained(format: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i)
4139
Age = Integer.constrained(gt: 18)
4240
end
41+
4342
class User < Dry::Struct
4443
attribute :name, Types::String
4544
attribute :email, Types::Email

docsite/source/hash-schemas.html.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ name: dry-types
77
It is possible to define a type for a hash with a known set of keys and corresponding value types. Let's say you want to describe a hash containing the name and the age of a user:
88

99
```ruby
10+
Types = Dry.Types()
11+
1012
# using simple kernel coercions
1113
user_hash = Types::Hash.schema(name: Types::String, age: Types::Coercible::Integer)
1214

@@ -78,6 +80,8 @@ The process of converting types to constructors like that can be automated, see
7880
By default, all keys are required to present in the input. You can mark a key as optional by adding `?` to its name:
7981

8082
```ruby
83+
Types = Dry.Types()
84+
8185
user_hash = Types::Hash.schema(name: Types::String, age?: Types::Integer)
8286

8387
user_hash[name: 'Jane']
@@ -89,6 +93,8 @@ user_hash[name: 'Jane']
8993
All keys not declared in the schema are silently ignored. This behavior can be changed by calling `.strict` on the schema:
9094

9195
```ruby
96+
Types = Dry.Types()
97+
9298
user_hash = Types::Hash.schema(name: Types::String).strict
9399
user_hash[name: 'Jane', age: 21]
94100
# => Dry::Types::UnknownKeysError: unexpected keys [:age] in Hash input
@@ -99,6 +105,8 @@ user_hash[name: 'Jane', age: 21]
99105
Keys are supposed to be symbols but you can attach a key tranformation to a schema, e.g. for converting strings into symbols:
100106

101107
```ruby
108+
Types = Dry.Types()
109+
102110
user_hash = Types::Hash.schema(name: Types::String).with_key_transform(&:to_sym)
103111
user_hash['name' => 'Jane']
104112

@@ -110,6 +118,8 @@ user_hash['name' => 'Jane']
110118
Hash schemas can be inherited in a sense you can define a new schema based on an existing one. Declared keys will be merged, key and type transformations will be preserved. The `strict` option is also passed to the new schema if present.
111119

112120
```ruby
121+
Types = Dry.Types()
122+
113123
# Building an empty base schema
114124
StrictSymbolizingHash = Types::Hash.schema({}).strict.with_key_transform(&:to_sym)
115125

@@ -131,6 +141,8 @@ The resulting schema will have the sum of both sets
131141
of attributes.
132142

133143
```ruby
144+
Types = Dry.Types()
145+
134146
user_hash = Types::Hash.schema(
135147
name: Types::String
136148
)
@@ -153,6 +165,8 @@ while each attribute keeps the type transformations from its original hash.
153165
A schema can transform types with a block. For example, the following code makes all keys optional:
154166

155167
```ruby
168+
Types = Dry.Types()
169+
156170
user_hash = Types::Hash.with_type_transform { |type| type.required(false) }.schema(
157171
name: Types::String,
158172
age: Types::Integer

docsite/source/index.html.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ sections:
2828
require 'dry-types'
2929
require 'dry-struct'
3030

31-
module Types
32-
include Dry.Types()
33-
end
31+
Types = Dry.Types()
3432

3533
User = Dry.Struct(name: Types::String, age: Types::Integer)
3634

@@ -111,7 +109,7 @@ User.schema.key(:age).meta
111109
# => {:info=>"extra info about age"}
112110
```
113111

114-
- Pass values directly to `Dry::Types` without creating an object using `[]`:
112+
- Pass values directly to types without creating an object using `[]`:
115113

116114
```ruby
117115
Types::Strict::String["foo"]

0 commit comments

Comments
 (0)