Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #38 from ApplauseOSS/bug/31-crash-fetching-not-map…
Browse files Browse the repository at this point in the history
…ped-type

Bug/30 Crash fetching not mapped type
  • Loading branch information
Przytua authored Jan 26, 2017
2 parents 04e8ba0 + 376476e commit 938d0ad
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 14 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Swifjection Changelog

## [Unreleased]
## [0.6.0] - 26.01.2017
### Added:
* Continuous Integration setup with build status badge in README
* Code documentation
* Swift Package manifest
* CHANGELOG
* `CHANGELOG`

### Changed:
* Resolved exception when `Swifjection` couldn't create instance of singleton object
* Changed order of closure binding function parameters:

```Swift
Expand Down
2 changes: 1 addition & 1 deletion Sources/Swifjection/Classes/Bindings/Bindings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ open class Bindings {
*/
public func findBinding(type: Any.Type) -> Any? {
let typeName = "\(type)"
if let injector = self.injector, let binding = bindings[typeName] {
if let injector = injector, let binding = bindings[typeName] {
return binding.getObject(withInjector: injector)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion Sources/Swifjection/Classes/Bindings/ClosureBinding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public class ClosureBinding: Binding {
- Returns: Object created using the `closure`.
*/
public func getObject(withInjector injector: Injecting) -> Any? {
return self.closure(injector)
return closure(injector)
}
}
6 changes: 3 additions & 3 deletions Sources/Swifjection/Classes/Bindings/SingletonBinding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ public class SingletonBinding: Binding {
- Returns: An singleton instance of `type` provided during initialization of this binding.
*/
public func getObject(withInjector injector: Injecting) -> Any? {
if self.instance == nil {
if instance == nil {
if let type = self.type as? Injectable.Type {
let instance = type.init(injector: injector)
instance?.injectDependencies(injector: injector)
self.instance = instance
} else if let type = self.type as? NSObject.Type {
self.instance = type.init()
}
}
return self.instance!
}
return self.instance
}
}
20 changes: 19 additions & 1 deletion Sources/Swifjection/Classes/Bindings/SingletonBindingSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,27 @@ class SingletonBindingSpec: QuickSpec {
}

}

}

context("created with not Injectable, nor NSObject type") {

beforeEach {
closureBinding = SingletonBinding(withType: EmptySwiftClass.self)
}

describe("getObject") {

var returnedObject: Any?

beforeEach {
returnedObject = closureBinding?.getObject(withInjector: FakeInjector())
}

it("should return nil") {
expect(returnedObject).to(beNil())
}
}
}
}

}
10 changes: 5 additions & 5 deletions Sources/Swifjection/Classes/Bindings/TypeBinding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TypeBinding: Binding {
- Returns: An initialized `TypeBinding` object.
*/
public init<T>(withType type: T.Type) {
self.objectCreationClosure = { injector in
objectCreationClosure = { injector in
return injector.getObject(withType: type)
}
}
Expand All @@ -50,7 +50,7 @@ class TypeBinding: Binding {
- Returns: An initialized `TypeBinding` object.
*/
public init<T>(withType type: T.Type) where T: Injectable {
self.objectCreationClosure = { injector in
objectCreationClosure = { injector in
return injector.getObject(withType: type)
}
}
Expand All @@ -63,7 +63,7 @@ class TypeBinding: Binding {
- Returns: An initialized `TypeBinding` object.
*/
public init<T>(withType type: T.Type) where T: NSObject {
self.objectCreationClosure = { injector in
objectCreationClosure = { injector in
return injector.getObject(withType: type)
}
}
Expand All @@ -76,7 +76,7 @@ class TypeBinding: Binding {
- Returns: An initialized `TypeBinding` object.
*/
public init<T>(withType type: T.Type) where T: NSObject, T: Injectable {
self.objectCreationClosure = { injector in
objectCreationClosure = { injector in
return injector.getObject(withType: type)
}
}
Expand All @@ -89,6 +89,6 @@ class TypeBinding: Binding {
- Returns: Object of `type` created using the `injector`, or nil.
*/
func getObject(withInjector injector: Injecting) -> Any? {
return self.objectCreationClosure(injector)
return objectCreationClosure(injector)
}
}
2 changes: 1 addition & 1 deletion Swifjection.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'Swifjection'
s.version = '0.5.0'
s.version = '0.6.0'
s.summary = 'Dependency Injection library for Swift'
s.description = <<-DESC
Lightweight and simplistic dependency injection framework written in Swift for Swift .
Expand Down

0 comments on commit 938d0ad

Please sign in to comment.