Skip to content

Commit

Permalink
Made improvements to the API based on feedback from lukelabonte
Browse files Browse the repository at this point in the history
  • Loading branch information
jkolb committed May 23, 2015
1 parent 32ee768 commit f4d3520
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 74 deletions.
110 changes: 110 additions & 0 deletions FieryCrucible.xcodeproj/xcshareddata/xcschemes/FieryCrucible.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0610"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E53405A91A3020CE00A474C1"
BuildableName = "FieryCrucible.framework"
BlueprintName = "FieryCrucible"
ReferencedContainer = "container:FieryCrucible.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E53405B41A3020CF00A474C1"
BuildableName = "FieryCrucibleTests.xctest"
BlueprintName = "FieryCrucibleTests"
ReferencedContainer = "container:FieryCrucible.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E53405B41A3020CF00A474C1"
BuildableName = "FieryCrucibleTests.xctest"
BlueprintName = "FieryCrucibleTests"
ReferencedContainer = "container:FieryCrucible.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E53405A91A3020CE00A474C1"
BuildableName = "FieryCrucible.framework"
BlueprintName = "FieryCrucible"
ReferencedContainer = "container:FieryCrucible.xcodeproj">
</BuildableReference>
</MacroExpansion>
</TestAction>
<LaunchAction
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E53405A91A3020CE00A474C1"
BuildableName = "FieryCrucible.framework"
BlueprintName = "FieryCrucible"
ReferencedContainer = "container:FieryCrucible.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E53405A91A3020CE00A474C1"
BuildableName = "FieryCrucible.framework"
BlueprintName = "FieryCrucible"
ReferencedContainer = "container:FieryCrucible.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
62 changes: 39 additions & 23 deletions FieryCrucible/DependencyFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,34 +23,34 @@
// THE SOFTWARE.
//

protocol InstanceContainer : class {
private protocol InstanceContainer : class {
typealias InstanceType

var instance: InstanceType? { get }
}

class StrongContainer<C> : InstanceContainer {
private class StrongContainer<C> : InstanceContainer {
var instance: C?

init(instance: C) {
self.instance = instance
}
}

class WeakContainer<C: AnyObject> : InstanceContainer {
private class WeakContainer<C: AnyObject> : InstanceContainer {
weak var instance: C?

init(instance: C) {
self.instance = instance
}
}

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"
Expand All @@ -61,7 +61,7 @@ public class DependencyFactory {
}
}

struct InstanceKey : Hashable, Printable {
private struct InstanceKey : Hashable, Printable {
let lifecycle: Lifecyle
let name: String

Expand All @@ -74,61 +74,77 @@ 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<T>(@autoclosure factory: () -> T, name: String = __FUNCTION__, configure: ((T) -> ())? = nil) -> T {
return shared(name, factory: factory, configure: configure)
}

public func shared<T>(name: String, @autoclosure factory: () -> T, configure configureOrNil: ((T) -> ())? = nil) -> T {
public final func shared<T>(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<T: AnyObject>(@autoclosure factory: () -> T, name: String = __FUNCTION__, configure: ((T) -> ())? = nil) -> T {
return weakShared(name, factory: factory, configure: configure)
}

public func weakShared<T: AnyObject>(name: String, @autoclosure factory: () -> T, configure configureOrNil: ((T) -> ())? = nil) -> T {
public final func weakShared<T: AnyObject>(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<T>(@autoclosure factory: () -> T, name: String = __FUNCTION__, configure: ((T) -> ())? = nil) -> T {
return unshared(name, factory: factory, configure: configure)
}

public func unshared<T>(name: String, @autoclosure factory: () -> T, configure configureOrNil: ((T) -> ())? = nil) -> T {
public final func unshared<T>(name: String, @autoclosure factory: () -> T, configure: ((T) -> ())? = nil) -> T {
var unsharedInstances: [String:AnyObject] = [:]
return inject(
lifecyle: .Unshared,
name: name,
instancePool: &unsharedInstances,
containerFactory: { StrongContainer(instance: $0) },
factory: factory,
configure: configureOrNil
configure: configure
)
}

public final func scoped<T>(@autoclosure factory: () -> T, name: String = __FUNCTION__, configure: ((T) -> ())? = nil) -> T {
return scoped(name, factory: factory, configure: configure)
}

public func scoped<T>(name: String, @autoclosure factory: () -> T, configure configureOrNil: ((T) -> ())? = nil) -> T {
public final func scoped<T>(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<T, C: InstanceContainer where C.InstanceType == T>(# lifecyle: Lifecyle, name: String, inout instancePool: [String:AnyObject], containerFactory: (T) -> C, @autoclosure factory: () -> T, configure configureOrNil: ((T) -> ())?) -> T {
private final func inject<T, C: InstanceContainer where C.InstanceType == T>(# 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
Expand All @@ -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)})
}

Expand Down
2 changes: 1 addition & 1 deletion FieryCrucible/FieryCrucible.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions FieryCrucible/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>1.2.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<string>1.2.0</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
Expand Down
48 changes: 21 additions & 27 deletions FieryCrucibleTests/FieryCrucibleTests.swift
Original file line number Diff line number Diff line change
@@ -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.
}
}

}
Loading

0 comments on commit f4d3520

Please sign in to comment.