Skip to content

Commit 75d7475

Browse files
aridlehooverFito
andcommitted
Initializes replicable objects with positional and named arguments
- Positional and named arguments may be passed when initializing "replicable" classes - When multiple classes are registered under the same key, all classes receive the same arguments Co-authored-by: Fito von Zastrow <adolfovon@gmail.com>
1 parent 27872aa commit 75d7475

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.1.0] - 2023-01-03
11+
1012
### Added
1113

1214
- Abstract classes can extend Replicator::Replicable to make their descendants "replicable"
1315
- Concrete, "replicable" classes can self register under one or more keys by calling `replicates :symbol`
1416
- Multiple concrete, "replicable" classes can self register under the same key by calling `replicates :symbol`
1517
- Registered, "replicable" classes can be instantiated using the abstract parent's `.replicate(:symbol)` method
18+
- Positional and named arguments may be passed when initializing "replicable" classes

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Replicator is ready to use in it's current state. The interfaces are unlikey to
4242
- [x] Concrete, "replicable" classes can self register under one or more keys by calling `replicates :symbol`
4343
- [x] Multiple concrete, "replicable" classes can self register under the same key by calling `replicates :symbol`
4444
- [x] Registered, "replicable" classes can be instantiated using the abstract parent's `.replicate(:symbol)` method
45-
- [ ] Positional and named arguments may be passed when initializing "replicable" classes
45+
- [x] Positional and named arguments may be passed when initializing "replicable" classes
4646
- [ ] A "replicable" class may register as the `default_replica` for an extended abstract parent class, and will be instantiated when an unknown key is requested (good for the null object pattern)
4747
- [ ] A "replicable" class may register as `replicates_all` in which case, it is always instantiated and returned in addition to the classes that match a requested key
4848

spec/replicator_spec.cr

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
require "./spec_helper"
22

3-
abstract class Starship; extend Replicator::Replicable; end
4-
class Enterprise < Starship; replicates :galaxy; replicates :enterprise; end
5-
class Cerritos < Starship; replicates :galaxy; replicates :cerritos; end
3+
abstract class Starship
4+
extend Replicator::Replicable
5+
6+
getter :designation
7+
8+
def initialize(@designation = "")
9+
end
10+
end
11+
12+
class Enterprise < Starship
13+
replicates :galaxy
14+
replicates :enterprise
15+
end
16+
17+
class Cerritos < Starship
18+
replicates :galaxy
19+
replicates :cerritos
20+
end
621

722
Spectator.describe Replicator do
823
it "returns an empty array when an unregistered class is requested" do
@@ -22,4 +37,10 @@ Spectator.describe Replicator do
2237
expect(replicas.first).to be_a_kind_of(Enterprise)
2338
expect(replicas.last).to be_a_kind_of(Cerritos)
2439
end
40+
41+
it "allows passing arguments when instantiating classes" do
42+
replicas = Starship.replicate(:enterprise, designation: "NCC-1701-D")
43+
expect(replicas.size).to eq(1)
44+
expect(replicas.first.designation).to eq("NCC-1701-D")
45+
end
2546
end

src/replicator/replicable.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ module Replicator
77
{{@type}}.__replicable_registry.add(key, self)
88
end
99

10-
def self.replicate(key)
10+
def self.replicate(key, *args, **kwargs)
1111
{{@type}}.__replicable_registry
1212
.get(key)
13-
.map { |klass| klass.new }
13+
.map { |klass| klass.new(*args, **kwargs) }
1414
end
1515

1616
protected def self.__replicable_registry

0 commit comments

Comments
 (0)