Skip to content

Commit 554ae41

Browse files
committed
Change #[] forging method to #call
`[]` clashes with accessing forges by name, which I want to include. `call` can be used as `.()` which isn't much longer and also plays nicer with parameters.
1 parent e0182fe commit 554ae41

File tree

8 files changed

+36
-33
lines changed

8 files changed

+36
-33
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ end
103103

104104
A forge builds objects, using attributes hash:
105105
```ruby
106-
ObjectForge[:point]
106+
ObjectForge.call(:point)
107107
# => #<Point:0x00007f6109dcad40 @id="a", @x=0.17176955469852973, @y=0.3423901951181103>
108108
# Positional arguments define used traits:
109109
ObjectForge.build(:point, :z)
@@ -112,17 +112,17 @@ ObjectForge.build(:point, :z)
112112
ObjectForge.forge(:point, x: 10)
113113
# => #<Point:0x00007f6109aabf88 @id="c", @x=10, @y=-0.3458802496120402>
114114
# Traits and overrides are combined in the given order:
115-
ObjectForge[:point, :z, :invalid, id: "NaN"]
115+
ObjectForge.call(:point, :z, :invalid, id: "NaN")
116116
# => #<Point:0x00007f6109b82e48 @id="NaN", @x=0.0, @y=NaN>
117117
# A Proc override behaves the same as an attribute definition:
118-
ObjectForge[:point, :z, x: -> { rand(100..200) + delta }]
118+
ObjectForge.call(:point, :z, x: -> { rand(100..200) + delta })
119119
# => #<Point:0x00007f6109932418 @id="Z_d", @x=135.0, @y=0.0>
120120
# A block can be passed to do something with the created object:
121-
ObjectForge[:point, :z] { puts "#{_1.id}: #{_1.x},#{_1.y}" }
121+
ObjectForge.call(:point, :z) { puts "#{_1.id}: #{_1.x},#{_1.y}" }
122122
# outputs "Z_e: 0.0,0.0"
123123
```
124124
> [!TIP]
125-
> Forging can be done through any of `#[]`, `#forge`, or `#build` methods, they are aliases.
125+
> Forging can be done through any of `#call`, `#forge`, or `#build` methods, they are aliases.
126126
127127
### Separate forgeyards and forges
128128

@@ -140,13 +140,13 @@ end
140140

141141
Now, this forgeyard can be used just like the default one:
142142
```ruby
143-
forgeyard[:point, :z, id: "0"]
143+
forgeyard.forge(:point, :z, id: "0")
144144
# => #<Point:0x00007f6109b719e0 @id="0", @x=0, @y=0>
145145
```
146146

147147
Note how the forge isn't registered in the default forgeyard:
148148
```ruby
149-
ObjectForge[:point]
149+
ObjectForge.forge(:point)
150150
# ArgumentError: unknown forge: point
151151
```
152152

@@ -159,7 +159,7 @@ forge = ObjectForge::Forge.define(Point) do |f|
159159
f.radius { 0.5 }
160160
f.trait :z do f.radius { 0 } end
161161
end
162-
forge[:z, id: "0"]
162+
forge.(:z, id: "0")
163163
# => #<Point:0x00007f6109b719e0 @id="0", @x=0, @y=0>
164164
```
165165

@@ -169,7 +169,7 @@ forge.build
169169
# => #<Point:0x00007f610deae578 @id="a", @x=0.3317733939650964, @y=-0.1363936629550252>
170170
forge.forge(:z)
171171
# => #<Point:0x00007f61099f6520 @id="b", @x=0, @y=0>
172-
forge[radius: 500]
172+
forge.(radius: 500)
173173
# => #<Point:0x00007f6109960048 @id="c", @x=-141, @y=109>
174174
```
175175

@@ -237,7 +237,7 @@ General:
237237

238238
Forge definition:
239239
- Class specification for a forge is non-optional, there is no assumption about the class name.
240-
- If DSL block declares a block argument, `self` context is not changed, so DSL methods can't be called with an implicit receiver.
240+
- If the DSL block declares a block argument, `self` context is not changed, and DSL methods can't be called with an implicit receiver.
241241

242242
Attributes:
243243
- For now, transient attributes have no difference to regular ones, they just aren't set in the final object.
@@ -249,7 +249,7 @@ Traits:
249249
- There are no default traits.
250250

251251
Sequences:
252-
- There is no way to define shared sequences, unless you pass the same object yourself to multiple `sequence` calls.
252+
- There is no explicit way to define shared sequences, unless you pass the same object yourself to multiple `sequence` calls.
253253
- Sequences work with values implementing `#succ`, not `#next`, expressly prohibiting `Enumerator`. This may be relaxed in the future.
254254

255255
## Current and planned features (roadmap)

lib/object_forge.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
# # => #<struct Frobinator frob="Frobinator", inator=#<Proc:...>>
3939
# ObjectForge.build(:frobber, frob: -> { "Frob" + inator }, inator: "orn")
4040
# # => #<struct Frobinator frob="Froborn", inator="orn">
41-
# ObjectForge[:frobber, :static, inator: "Value"]
41+
# ObjectForge.call(:frobber, :static, inator: "Value")
4242
# # => #<struct Frobinator frob="Static", inator="Value">
4343
module ObjectForge
4444
# Base error class for ObjectForge.
@@ -104,9 +104,7 @@ def self.forge(...)
104104
end
105105

106106
class << self
107-
# @since 0.1.0
108107
alias build forge
109-
# @since 0.1.0
110-
alias [] forge
108+
alias call forge
111109
end
112110
end

lib/object_forge/forge.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def self.define(forged, name: nil, &)
5555

5656
# @param forged [Class, Any] class or object to forge
5757
# @param parameters [Parameters, ForgeDSL] forge parameters
58-
# @param name [Symbol, nil] forge name
58+
# @param name [Symbol, nil] forge name;
59+
# only used for identification purposes
5960
def initialize(forged, parameters, name: nil)
6061
@name = name
6162
@forged = forged
@@ -92,7 +93,7 @@ def forge(*traits, **overrides)
9293
end
9394

9495
alias build forge
95-
alias [] forge
96+
alias call forge
9697

9798
private
9899

lib/object_forge/forgeyard.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ def register(name, forge)
5858
#
5959
# @raise [KeyError] if forge with the specified name is not registered
6060
def forge(name, ...)
61-
@forges.fetch(name).[](...)
61+
@forges.fetch(name).call(...)
6262
end
6363

6464
alias build forge
65-
alias [] forge
65+
alias call forge
6666
end
6767
end

sig/object_forge.rbs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ module ObjectForge
3535

3636
def self.forge
3737
: (Symbol name, *Symbol traits, **untyped overrides) ?{ (untyped) -> void } -> ObjectForge::_Forgable
38+
def self.build
39+
: (Symbol name, *Symbol traits, **untyped overrides) ?{ (untyped) -> void } -> ObjectForge::_Forgable
40+
def self.call
41+
: (Symbol name, *Symbol traits, **untyped overrides) ?{ (untyped) -> void } -> ObjectForge::_Forgable
3842
end
3943

4044
class ObjectForge::Sequence
@@ -67,7 +71,7 @@ class ObjectForge::Forgeyard
6771
def forge
6872
: (Symbol name, *Symbol traits, **untyped overrides) ?{ (untyped) -> void } -> ObjectForge::_Forgable
6973
alias build forge
70-
alias [] forge
74+
alias call forge
7175
end
7276

7377
class ObjectForge::Forge
@@ -93,7 +97,7 @@ class ObjectForge::Forge
9397
def forge
9498
: (*Symbol traits, **untyped overrides) ?{ (untyped) -> void } -> ObjectForge::_Forgable
9599
alias build forge
96-
alias [] forge
100+
alias call forge
97101

98102
private
99103

spec/object_forge/forge_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@ def initialize(attributes)
8585
end
8686

8787
include_examples "has an alias", :build, :forge
88-
include_examples "has an alias", :[], :forge
88+
include_examples "has an alias", :call, :forge
8989
end
9090
end

spec/object_forge/forgeyard_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ module ObjectForge
44
RSpec.describe Forgeyard do
55
subject(:forgeyard) { described_class.new }
66

7-
let(:forge) { instance_double(Forge, "Forge", "[]": instance) }
7+
let(:forge) { instance_double(Forge, "Forge", call: instance) }
88
let(:instance) { Object.new }
99

1010
describe "#define" do
11-
let(:definition) { forgeyard.define(:foo, Object) { |f| f.attribute(:[]) { nil } } }
11+
let(:definition) { forgeyard.define(:foo, Object) { |f| f.attribute(:call) { nil } } }
1212

1313
before { allow(Forge).to receive(:define).and_return(forge) }
1414

@@ -69,32 +69,32 @@ module ObjectForge
6969
context "with a single argument" do
7070
it "builds an instance through named forge with default parameters" do
7171
expect(forgeyard.forge(:test)).to be instance
72-
expect(forge).to have_received(:[]).with(no_args)
72+
expect(forge).to have_received(:call).with(no_args)
7373
end
7474
end
7575

7676
context "with multiple arguments" do
7777
it "builds an instance through named forge with specified parameters" do
7878
expect(forgeyard.forge(:test, :trait, attribute: 2)).to be instance
79-
expect(forge).to have_received(:[]).with(:trait, attribute: 2)
79+
expect(forge).to have_received(:call).with(:trait, attribute: 2)
8080
end
8181
end
8282

8383
context "with a block" do
8484
let(:forge) { Forge.new(forged_class, Forge::Parameters.new(attributes: { foo: 1, bar: 2 }, traits: {})) }
8585
let(:forged_class) { Struct.new(:foo, :bar, keyword_init: true) }
8686

87-
before { allow(forge).to receive(:[]).and_call_original }
87+
before { allow(forge).to receive(:call).and_call_original }
8888

8989
it "allows tapping into the object" do
90-
expect(forgeyard[:test] { _1.foo = 33 }).to eq forged_class.new(foo: 33, bar: 2)
91-
expect(forge).to have_received(:[]).with(no_args)
90+
expect(forgeyard.(:test) { _1.foo = 33 }).to eq forged_class.new(foo: 33, bar: 2)
91+
expect(forge).to have_received(:call).with(no_args)
9292
end
9393

9494
it "runs the block after forging the object with resolved attributes" do
95-
expect(forgeyard[:test, foo: :foo, bar: -> { foo }] { _1.foo = 33 })
95+
expect(forgeyard.(:test, foo: :foo, bar: -> { foo }) { _1.foo = 33 })
9696
.to eq forged_class.new(foo: 33, bar: :foo)
97-
expect(forge).to have_received(:[]).with(foo: :foo, bar: Proc)
97+
expect(forge).to have_received(:call).with(foo: :foo, bar: Proc)
9898
end
9999
end
100100

@@ -106,6 +106,6 @@ module ObjectForge
106106
end
107107

108108
include_examples "has an alias", :build, :forge
109-
include_examples "has an alias", :[], :forge
109+
include_examples "has an alias", :call, :forge
110110
end
111111
end

spec/object_forge_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@
4848

4949
describe described_class.singleton_class do
5050
include_examples "has an alias", :build, :forge
51-
include_examples "has an alias", :[], :forge
51+
include_examples "has an alias", :call, :forge
5252
end
5353
end

0 commit comments

Comments
 (0)