diff --git a/FieryCrucible.xcodeproj/xcshareddata/xcschemes/FieryCrucible.xcscheme b/FieryCrucible.xcodeproj/xcshareddata/xcschemes/FieryCrucible.xcscheme new file mode 100644 index 0000000..f23d202 --- /dev/null +++ b/FieryCrucible.xcodeproj/xcshareddata/xcschemes/FieryCrucible.xcscheme @@ -0,0 +1,110 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FieryCrucible/DependencyFactory.swift b/FieryCrucible/DependencyFactory.swift index 461bb8d..7de8385 100644 --- a/FieryCrucible/DependencyFactory.swift +++ b/FieryCrucible/DependencyFactory.swift @@ -2,7 +2,7 @@ // DependencyFactory.swift // FieryCrucible // -// Copyright (c) 2014 Justin Kolb - http://franticapparatus.net +// Copyright (c) 2015 Justin Kolb - http://franticapparatus.net // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -23,13 +23,13 @@ // THE SOFTWARE. // -protocol InstanceContainer : class { +private protocol InstanceContainer : class { typealias InstanceType var instance: InstanceType? { get } } -class StrongContainer : InstanceContainer { +private class StrongContainer : InstanceContainer { var instance: C? init(instance: C) { @@ -37,7 +37,7 @@ class StrongContainer : InstanceContainer { } } -class WeakContainer : InstanceContainer { +private class WeakContainer : InstanceContainer { weak var instance: C? init(instance: C) { @@ -45,12 +45,12 @@ class WeakContainer : InstanceContainer { } } -func ==(lhs: DependencyFactory.InstanceKey, rhs: DependencyFactory.InstanceKey) -> Bool { +private func ==(lhs: DependencyFactory.InstanceKey, rhs: DependencyFactory.InstanceKey) -> Bool { return (lhs.lifecycle == rhs.lifecycle) && (lhs.name == rhs.name) } public class DependencyFactory { - enum Lifecyle : String, Printable { + private enum Lifecyle : String, Printable { case Shared = "shared" case WeakShared = "weakShared" case Unshared = "unshared" @@ -61,7 +61,7 @@ public class DependencyFactory { } } - struct InstanceKey : Hashable, Printable { + private struct InstanceKey : Hashable, Printable { let lifecycle: Lifecyle let name: String @@ -74,38 +74,50 @@ public class DependencyFactory { } } - var sharedInstances: [String:AnyObject] = [:] - var weakSharedInstances: [String:AnyObject] = [:] - var scopedInstances: [String:AnyObject] = [:] - var instanceStack: [InstanceKey] = [] - var configureStack: [() -> ()] = [] - var requestDepth = 0 + private var sharedInstances: [String:AnyObject] = [:] + private var weakSharedInstances: [String:AnyObject] = [:] + private var scopedInstances: [String:AnyObject] = [:] + private var instanceStack: [InstanceKey] = [] + private var configureStack: [() -> ()] = [] + private var requestDepth = 0 public init() { } + + public final func shared(@autoclosure factory: () -> T, name: String = __FUNCTION__, configure: ((T) -> ())? = nil) -> T { + return shared(name, factory: factory, configure: configure) + } - public func shared(name: String, @autoclosure factory: () -> T, configure configureOrNil: ((T) -> ())? = nil) -> T { + public final func shared(name: String, @autoclosure factory: () -> T, configure: ((T) -> ())? = nil) -> T { return inject( lifecyle: .Shared, name: name, instancePool: &sharedInstances, containerFactory: { StrongContainer(instance: $0) }, factory: factory, - configure: configureOrNil + configure: configure ) } + + public final func weakShared(@autoclosure factory: () -> T, name: String = __FUNCTION__, configure: ((T) -> ())? = nil) -> T { + return weakShared(name, factory: factory, configure: configure) + } - public func weakShared(name: String, @autoclosure factory: () -> T, configure configureOrNil: ((T) -> ())? = nil) -> T { + public final func weakShared(name: String, @autoclosure factory: () -> T, configure: ((T) -> ())? = nil) -> T { return inject( lifecyle: .WeakShared, name: name, instancePool: &weakSharedInstances, containerFactory: { WeakContainer(instance: $0) }, factory: factory, - configure: configureOrNil + configure: configure ) } + + public final func unshared(@autoclosure factory: () -> T, name: String = __FUNCTION__, configure: ((T) -> ())? = nil) -> T { + return unshared(name, factory: factory, configure: configure) + } - public func unshared(name: String, @autoclosure factory: () -> T, configure configureOrNil: ((T) -> ())? = nil) -> T { + public final func unshared(name: String, @autoclosure factory: () -> T, configure: ((T) -> ())? = nil) -> T { var unsharedInstances: [String:AnyObject] = [:] return inject( lifecyle: .Unshared, @@ -113,22 +125,26 @@ public class DependencyFactory { instancePool: &unsharedInstances, containerFactory: { StrongContainer(instance: $0) }, factory: factory, - configure: configureOrNil + configure: configure ) } + + public final func scoped(@autoclosure factory: () -> T, name: String = __FUNCTION__, configure: ((T) -> ())? = nil) -> T { + return scoped(name, factory: factory, configure: configure) + } - public func scoped(name: String, @autoclosure factory: () -> T, configure configureOrNil: ((T) -> ())? = nil) -> T { + public final func scoped(name: String, @autoclosure factory: () -> T, configure: ((T) -> ())? = nil) -> T { return inject( lifecyle: .Scoped, name: name, instancePool: &scopedInstances, containerFactory: { StrongContainer(instance: $0) }, factory: factory, - configure: configureOrNil + configure: configure ) } - func inject(# lifecyle: Lifecyle, name: String, inout instancePool: [String:AnyObject], containerFactory: (T) -> C, @autoclosure factory: () -> T, configure configureOrNil: ((T) -> ())?) -> T { + private final func inject(# lifecyle: Lifecyle, name: String, inout instancePool: [String:AnyObject], containerFactory: (T) -> C, @autoclosure factory: () -> T, configure: ((T) -> ())?) -> T { if let container = instancePool[name] as? C { if let instance = container.instance { return instance @@ -148,7 +164,7 @@ public class DependencyFactory { let container = containerFactory(instance) instancePool[name] = container - if let configure = configureOrNil { + if let configure = configure { configureStack.append({configure(instance)}) } diff --git a/FieryCrucible/FieryCrucible.h b/FieryCrucible/FieryCrucible.h index 8e5f45e..c0dae60 100644 --- a/FieryCrucible/FieryCrucible.h +++ b/FieryCrucible/FieryCrucible.h @@ -2,7 +2,7 @@ // FieryCrucible.h // FieryCrucible // -// Copyright (c) 2014 Justin Kolb - http://franticapparatus.net +// Copyright (c) 2015 Justin Kolb - http://franticapparatus.net // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/FieryCrucible/Info.plist b/FieryCrucible/Info.plist index c581bd5..e296879 100644 --- a/FieryCrucible/Info.plist +++ b/FieryCrucible/Info.plist @@ -15,11 +15,11 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.0 + 1.2.0 CFBundleSignature ???? CFBundleVersion - $(CURRENT_PROJECT_VERSION) + 1.2.0 NSPrincipalClass diff --git a/FieryCrucibleTests/FieryCrucibleTests.swift b/FieryCrucibleTests/FieryCrucibleTests.swift index 2e77918..e13a7b3 100644 --- a/FieryCrucibleTests/FieryCrucibleTests.swift +++ b/FieryCrucibleTests/FieryCrucibleTests.swift @@ -1,36 +1,30 @@ // -// FieryCrucibleTests.swift -// FieryCrucibleTests +// FieryCrucibleTests.swift +// FieryCrucible // -// Created by Justin Kolb on 12/3/14. -// Copyright (c) 2014 Justin Kolb. All rights reserved. +// Copyright (c) 2015 Justin Kolb - http://franticapparatus.net +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. // import UIKit import XCTest class FieryCrucibleTests: XCTestCase { - - override func setUp() { - super.setUp() - // Put setup code here. This method is called before the invocation of each test method in the class. - } - - override func tearDown() { - // Put teardown code here. This method is called after the invocation of each test method in the class. - super.tearDown() - } - - func testExample() { - // This is an example of a functional test case. - XCTAssert(true, "Pass") - } - - func testPerformanceExample() { - // This is an example of a performance test case. - self.measureBlock() { - // Put the code you want to measure the time of here. - } - } - } diff --git a/README.md b/README.md index a0e8100..675c4c1 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,16 @@ Fiery Crucible A minimalist type safe Swift dependency injector factory. Where all true instances are forged. +[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) + +#### Changelog + +#####Version 1.2.0 +* Should now work with Carthage +* Whole Module Optimization has been enabled to speed up compile times +* Tightened up the access restrictions on the API +* You no longer have to specify the name parameter (see updated examples below) + #### Features * Constructor injection * Setter injection @@ -27,35 +37,23 @@ You can either copy the source into your project, or setup a git submodle of thi class CustomFactory : DependencyFactory { func application() -> CustomApplication { - return shared { - "application", - factory: CustomApplication(), - configure: { [unowned self] (instance) in - instance.factory = self - } + return shared(CustomApplication()) { instance in + instance.factory = self } } func mainWindow() -> UIWindow { - return shared { - "mainWindow", - factory: UIWindow(frame: UIScreen.mainScreen().bounds), - configure: { [unowned self] (instance) in - instance.rootViewController = self.rootViewController() - } + return shared(UIWindow(frame: UIScreen.mainScreen().bounds)) { instance in + instance.rootViewController = self.rootViewController() } } func rootViewController() -> UIViewController { - return scoped { - "rootViewController", - factory: UITabBarController(), - configure: { [unowned self] (instance) in - instance.viewControllers = [ - self.tab0ViewController(), - self.tab1ViewController(), - ] - } + return scoped(UITabBarController()) { instance in + instance.viewControllers = [ + self.tab0ViewController(), + self.tab1ViewController(), + ] } }