Skip to content

Commit 09cbf39

Browse files
committed
Update examples in Readme
1 parent e80e9c2 commit 09cbf39

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,22 @@ end
9292
A forge builds objects using class's `.new`, passing all attributes as a single hash. Forge can be called through any of `#[]`, `#forge`, or `#build` methods (they are aliases):
9393
```ruby
9494
ObjectForge[:point]
95-
# => #<Point:0x00007f6109dcad40 @id="a", @x=0.17176955469852973, @y=0.3423901951181103>
95+
# => #<Point:0x00007f6109dcad40 @id="a", @x=0.17176955469852973, @y=0.3423901951181103>
9696
# Positional arguments define used traits:
9797
ObjectForge.build(:point, :z)
98-
# => #<Point:0x00007f61099e7980 @id="Z_d", @x=0.0, @y=0.0>
98+
# => #<Point:0x00007f61099e7980 @id="Z_b", @x=0.0, @y=0.0>
9999
# Attributes can be overridden with keyword arguments:
100100
ObjectForge.forge(:point, x: 10)
101-
# => #<Point:0x00007f6109aabf88 @id="c", @x=10, @y=-0.3458802496120402>
101+
# => #<Point:0x00007f6109aabf88 @id="c", @x=10, @y=-0.3458802496120402>
102102
# Traits and overrides are combined in the given order:
103103
ObjectForge[:point, :z, :invalid, id: "NaN"]
104-
# => #<Point:0x00007f6109b82e48 @id="NaN", @x=0.0, @y=NaN>
104+
# => #<Point:0x00007f6109b82e48 @id="NaN", @x=0.0, @y=NaN>
105105
# A Proc override behaves the same as an attribute definition:
106106
ObjectForge[:point, :z, x: -> { rand(100..200) + delta }]
107-
# => #<Point:0x00007f6109932418 @id="ez", @x=135.0, @y=0.0>
107+
# => #<Point:0x00007f6109932418 @id="Z_d", @x=135.0, @y=0.0>
108+
# A block can be passed to do something with the created object:
109+
ObjectForge[:point, :z] { puts "#{_1.id}: #{_1.x},#{_1.y}" }
110+
# outputs "Z_e: 0.0,0.0"
108111
```
109112

110113
### Separate forgeyards and forges
@@ -124,12 +127,13 @@ end
124127
Now, this forgeyard can be used just like the default one:
125128
```ruby
126129
forgeyard[:point, :z, id: "0"]
127-
# => #<Point:0x00007f6109b719e0 @id="0", @x=0, @y=0>
130+
# => #<Point:0x00007f6109b719e0 @id="0", @x=0, @y=0>
128131
```
129132

130133
Note how the forge isn't registered in the default forgeyard:
131134
```ruby
132-
ObjectForge[:point] # => ArgumentError: unknown forge: point
135+
ObjectForge[:point]
136+
# ArgumentError: unknown forge: point
133137
```
134138

135139
If you find it more convenient not to use a forgeyard (for example, if you only need a single forge for your service), you can create individual forges:
@@ -142,17 +146,17 @@ forge = ObjectForge::Forge.define(Point) do |f|
142146
f.trait :z do f.radius { 0 } end
143147
end
144148
forge[:z, id: "0"]
145-
# => #<Point:0x00007f6109b719e0 @id="0", @x=0, @y=0>
149+
# => #<Point:0x00007f6109b719e0 @id="0", @x=0, @y=0>
146150
```
147151

148152
`Forge` has the same building interface as a `Forgeyard`, but it doesn't have the name argument:
149153
```ruby
150154
forge[]
151-
# => #<Point:0x00007f610deae578 @id="a", @x=0.3317733939650964, @y=-0.1363936629550252>
155+
# => #<Point:0x00007f610deae578 @id="a", @x=0.3317733939650964, @y=-0.1363936629550252>
152156
forge[:z]
153-
# => #<Point:0x00007f61099f6520 @id="b", @x=0, @y=0>
157+
# => #<Point:0x00007f61099f6520 @id="b", @x=0, @y=0>
154158
forge[radius: 500]
155-
# => #<Point:0x00007f6109960048 @id="c", @x=-141, @y=109>
159+
# => #<Point:0x00007f6109960048 @id="c", @x=-141, @y=109>
156160
```
157161

158162
### Differences and limitations (compared to FactoryBot)

0 commit comments

Comments
 (0)