Skip to content

Commit

Permalink
Make resolve() public
Browse files Browse the repository at this point in the history
  • Loading branch information
avdyushin committed Jan 20, 2021
1 parent b58d7b8 commit 5c4196e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 9 additions & 8 deletions Sources/Injected/Injected.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,20 @@ open class Dependencies: Sequence {
return AnyIterator { iter.next()?.value }
}

/// Return dependency by given camelCase name of the object type
/// Returns dependency by given camelCase name of the object type
/// For example: if dependency registered as `MyService` name should be `myService`
public subscript<T>(dynamicMember name: String) -> T? {
dependencies.first { $0.name == name.prefix(1).capitalized + name.dropFirst() }?.value as? T
}

/// Returns resolved dependency
public func resolve<T>() -> T {
guard let dependency = dependencies.first(where: { $0.value is T })?.value as? T else {
fatalError("Can't resolve \(T.self)")
}
return dependency
}

// MARK: - Private

fileprivate init() { }
Expand All @@ -79,13 +87,6 @@ open class Dependencies: Sequence {
}
dependencies.append(dependency)
}

fileprivate func resolve<T>() -> T {
guard let dependency = dependencies.first(where: { $0.value is T })?.value as? T else {
fatalError("Can't resolve \(T.self)")
}
return dependency
}
}

@propertyWrapper
Expand Down
7 changes: 7 additions & 0 deletions Tests/InjectedTests/InjectedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ final class InjectedTests: XCTestCase {
XCTAssertEqual(serviceB.fetch().first, "Foo")
}

func testShared() {
let serviceA: ServiceA = Dependencies.shared.resolve()
XCTAssertTrue(serviceA.run())
let serviceB: ServiceB = Dependencies.shared.resolve()
XCTAssertEqual(serviceB.fetch().first, "Foo")
}

static var allTests = [
("testExample", testExample),
]
Expand Down

0 comments on commit 5c4196e

Please sign in to comment.