Skip to content

Commit 19624f0

Browse files
committed
Eliminate wrapping factories
1 parent 5b4aad1 commit 19624f0

File tree

5 files changed

+10
-44
lines changed

5 files changed

+10
-44
lines changed

src/Wrapper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export class Wrapper {
1414
this.#factory = factory
1515
}
1616

17+
public static as<T>(constructor: new (term: Term, dataset: DatasetCore, factory: DataFactory) => T): ValueMapping<T> {
18+
return (n: Wrapper) => new constructor(n.term, n.dataset, n.factory)
19+
}
20+
1721
get dataset(): DatasetCore {
1822
return this.#dataset
1923
}

test/unit/example.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ prefix : <https://example.org/>
2525
dataset.addQuads(new Parser().parse(rdf));
2626
const x = DataFactory.namedNode("x")
2727

28-
const p = Parent.wrap(x, dataset, DataFactory)
28+
const p = new Parent(x, dataset, DataFactory)
2929

3030

3131
assert.equal("o1", p.hasString)
@@ -39,7 +39,7 @@ prefix : <https://example.org/>
3939
assert.equal("x", p.hasString)
4040

4141
const newNode = DataFactory.namedNode("example.com/s")
42-
const newChild = Child.wrap(newNode, dataset, DataFactory)
42+
const newChild = new Child(newNode, dataset, DataFactory)
4343
newChild.hasName = "new name"
4444
p.hasChild = newChild
4545

@@ -71,7 +71,7 @@ prefix : <https://example.org/>
7171
dataset.addQuads(new Parser().parse(rdf))
7272
const x = DataFactory.namedNode("x")
7373

74-
const someModelClass = SomeModelClass.wrap(x, dataset, DataFactory)
74+
const someModelClass = new SomeModelClass(x, dataset, DataFactory)
7575

7676
assert.equal("o1", someModelClass.p1)
7777

test/unit/model/Child.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
import { TermMappings, ValueMappings, Wrapper } from "rdfjs-wrapper"
22
import { Vocabulary } from "../Vocabulary.js"
3-
import type { DataFactory, DatasetCore, Term } from "@rdfjs/types"
43

54
export class Child extends Wrapper {
6-
private constructor(node: Term, dataset: DatasetCore, factory: DataFactory) {
7-
super(node, dataset, factory)
8-
}
9-
10-
public static wrap(node: Term, dataset: DatasetCore, factory: DataFactory): Child {
11-
return new Child(node, dataset, factory)
12-
}
13-
14-
public static wrap2(node: Wrapper): Child {
15-
return Child.wrap(node.term, node.dataset, node.factory)
16-
}
17-
185
public get hasName(): string | undefined {
196
return this.singular(Vocabulary.hasName, ValueMappings.literalToString)
207
}

test/unit/model/Parent.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
import { Child } from "./Child.js"
22
import { TermMappings, ValueMappings, Wrapper } from "rdfjs-wrapper"
33
import { Vocabulary } from "../Vocabulary.js"
4-
import type { DataFactory, DatasetCore, Term } from "@rdfjs/types"
54

65
export class Parent extends Wrapper {
7-
private constructor(node: Term, dataset: DatasetCore, factory: DataFactory) {
8-
super(node, dataset, factory)
9-
}
10-
11-
public static wrap(node: Term, dataset: DatasetCore, factory: DataFactory): Parent {
12-
return new Parent(node, dataset, factory)
13-
}
14-
156
public get hasString(): string | undefined {
167
return this.singular(Vocabulary.hasString, ValueMappings.literalToString)
178
}
@@ -21,14 +12,14 @@ export class Parent extends Wrapper {
2112
}
2213

2314
public get hasChild(): Child {
24-
return this.singular(Vocabulary.hasChild, Child.wrap2)
15+
return this.singular(Vocabulary.hasChild, Wrapper.as(Child))
2516
}
2617

2718
public set hasChild(value: Child) {
28-
this.overwriteNullable(Vocabulary.hasChild, value, Child.wrap2)
19+
this.overwriteNullable(Vocabulary.hasChild, value, Wrapper.as(Child))
2920
}
3021

3122
public get hasChildSet(): Set<Child> {
32-
return this.objects(Vocabulary.hasChildSet, Child.wrap2, Child.wrap2)
23+
return this.objects(Vocabulary.hasChildSet, Wrapper.as(Child), Wrapper.as(Child))
3324
}
3425
}

test/unit/model/SomeModelClass.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,6 @@
11
import { getter, GetterArity, setter, SetterArity, TermMappings, ValueMappings, Wrapper } from "rdfjs-wrapper"
2-
import type { DataFactory, DatasetCore, Term } from "@rdfjs/types"
32

43
export class SomeModelClass extends Wrapper {
5-
private constructor(node: Term, dataset: DatasetCore, factory: DataFactory) {
6-
super(node, dataset, factory)
7-
}
8-
9-
static wrap(wrapper: Wrapper): SomeModelClass
10-
static wrap(n: Term, dataset: DatasetCore, factory: DataFactory): SomeModelClass
11-
static wrap(nodeOrWrapper: Term | Wrapper, dataset?: DatasetCore, factory?: DataFactory): SomeModelClass {
12-
if (dataset !== undefined && factory !== undefined) {
13-
return new SomeModelClass(nodeOrWrapper as Term, dataset, factory)
14-
} else {
15-
const {term, dataset, factory} = nodeOrWrapper as Wrapper
16-
return new SomeModelClass(term, dataset, factory)
17-
}
18-
}
19-
204
@getter("https://example.org/hasString", GetterArity.Singular, ValueMappings.literalToString)
215
get p1(): string {
226
throw new Error

0 commit comments

Comments
 (0)