Skip to content

Commit

Permalink
Merge branch 'static-configure' of github.com:square/cleanse into sta…
Browse files Browse the repository at this point in the history
…tic-configure
  • Loading branch information
mikelikespie committed Sep 28, 2016
2 parents e2a95c7 + 03f3315 commit 621081b
Show file tree
Hide file tree
Showing 29 changed files with 246 additions and 238 deletions.
10 changes: 6 additions & 4 deletions Cleanse.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
F6760C601D2DF9BA009C6F8A /* Scoped.swift in Sources */ = {isa = PBXBuildFile; fileRef = F6760C5F1D2DF9BA009C6F8A /* Scoped.swift */; };
F69A20621CD1941D00B954F8 /* CombinedHashable.swift in Sources */ = {isa = PBXBuildFile; fileRef = F69A205F1CD1941D00B954F8 /* CombinedHashable.swift */; };
F69D1A971CDDAFF7009132B4 /* CollectionBindingBuilderDecorator.swift in Sources */ = {isa = PBXBuildFile; fileRef = F69D1A961CDDAFF7009132B4 /* CollectionBindingBuilderDecorator.swift */; };
F69D1A991CDDB874009132B4 /* ScopedProviderDecorator.swift in Sources */ = {isa = PBXBuildFile; fileRef = F69D1A981CDDB874009132B4 /* ScopedProviderDecorator.swift */; };
F69D1A991CDDB874009132B4 /* ScopedBindingDecorator.swift in Sources */ = {isa = PBXBuildFile; fileRef = F69D1A981CDDB874009132B4 /* ScopedBindingDecorator.swift */; };
F69D1A9B1CDDBC53009132B4 /* BindingBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = F69D1A9A1CDDBC53009132B4 /* BindingBuilder.swift */; };
F69D1A9D1CDDD5A3009132B4 /* TaggedBindingBuilderDecorator.swift in Sources */ = {isa = PBXBuildFile; fileRef = F69D1A9C1CDDD5A3009132B4 /* TaggedBindingBuilderDecorator.swift */; };
F69D1AA01CDDE229009132B4 /* BinderArities.swift in Sources */ = {isa = PBXBuildFile; fileRef = F69D1A9E1CDDE229009132B4 /* BinderArities.swift */; };
Expand Down Expand Up @@ -118,7 +118,7 @@
F6760C5F1D2DF9BA009C6F8A /* Scoped.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Scoped.swift; sourceTree = "<group>"; };
F69A205F1CD1941D00B954F8 /* CombinedHashable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CombinedHashable.swift; sourceTree = "<group>"; };
F69D1A961CDDAFF7009132B4 /* CollectionBindingBuilderDecorator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionBindingBuilderDecorator.swift; sourceTree = "<group>"; };
F69D1A981CDDB874009132B4 /* ScopedProviderDecorator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ScopedProviderDecorator.swift; sourceTree = "<group>"; };
F69D1A981CDDB874009132B4 /* ScopedBindingDecorator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ScopedBindingDecorator.swift; sourceTree = "<group>"; };
F69D1A9A1CDDBC53009132B4 /* BindingBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BindingBuilder.swift; sourceTree = "<group>"; };
F69D1A9C1CDDD5A3009132B4 /* TaggedBindingBuilderDecorator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TaggedBindingBuilderDecorator.swift; sourceTree = "<group>"; };
F69D1A9E1CDDE229009132B4 /* BinderArities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BinderArities.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -298,7 +298,7 @@
F69D1A9A1CDDBC53009132B4 /* BindingBuilder.swift */,
F6B387911CE18CB1001CBC4F /* BindingBuilderDecorator.swift */,
F6B3878F1CE18BB2001CBC4F /* BaseBindingBuilder.swift */,
F69D1A981CDDB874009132B4 /* ScopedProviderDecorator.swift */,
F69D1A981CDDB874009132B4 /* ScopedBindingDecorator.swift */,
F69D1A9C1CDDD5A3009132B4 /* TaggedBindingBuilderDecorator.swift */,
);
name = BindingBuilder;
Expand Down Expand Up @@ -508,7 +508,7 @@
F69D1A971CDDAFF7009132B4 /* CollectionBindingBuilderDecorator.swift in Sources */,
F6A4D0DD1CD983130070EBB9 /* ComposedComponentExtension.swift in Sources */,
F6445E2D1CDB425E00036515 /* BaseComponent.swift in Sources */,
F69D1A991CDDB874009132B4 /* ScopedProviderDecorator.swift in Sources */,
F69D1A991CDDB874009132B4 /* ScopedBindingDecorator.swift in Sources */,
F6760C601D2DF9BA009C6F8A /* Scoped.swift in Sources */,
F61133BC1CD41D8A00069972 /* Swift3Compat.swift in Sources */,
F69A20621CD1941D00B954F8 /* CombinedHashable.swift in Sources */,
Expand Down Expand Up @@ -612,6 +612,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
OTHER_SWIFT_FLAGS = "-DSUPPORT_LEGACY_OBJECT_GRAPH";
Expand Down Expand Up @@ -658,6 +659,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = NO;
OTHER_SWIFT_FLAGS = "-DSUPPORT_LEGACY_OBJECT_GRAPH";
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos macosx";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,32 @@

},
"DVTSourceControlWorkspaceBlueprintWorkingCopyStatesKey" : {
"FA78EFA588CAAA527A39EFF7FEB4200B880E2491" : 0,
"C29DD0E89C17000393FD71820E9E7C86451DD28A" : 0
"C29DD0E89C17000393FD71820E9E7C86451DD28A" : 0,
"7F4664FA65AADBC384859AD71F1C93E5320699FF" : 9223372036854775807,
"68EDA75FC77F318B0BF9BAED4D569D9555E8E0D3" : 9223372036854775807,
"FA78EFA588CAAA527A39EFF7FEB4200B880E2491" : 0
},
"DVTSourceControlWorkspaceBlueprintIdentifierKey" : "85D0171F-7EAD-4682-B2C2-5BBDC5C235DD",
"DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : {
"FA78EFA588CAAA527A39EFF7FEB4200B880E2491" : "Cleanse\/",
"C29DD0E89C17000393FD71820E9E7C86451DD28A" : ".."
"C29DD0E89C17000393FD71820E9E7C86451DD28A" : "..\/..\/..\/..",
"7F4664FA65AADBC384859AD71F1C93E5320699FF" : "..\/..",
"68EDA75FC77F318B0BF9BAED4D569D9555E8E0D3" : "..",
"FA78EFA588CAAA527A39EFF7FEB4200B880E2491" : "Cleanse\/"
},
"DVTSourceControlWorkspaceBlueprintNameKey" : "Cleanse",
"DVTSourceControlWorkspaceBlueprintVersion" : 204,
"DVTSourceControlWorkspaceBlueprintRelativePathToProjectKey" : "Cleanse.xcodeproj",
"DVTSourceControlWorkspaceBlueprintRemoteRepositoriesKey" : [
{
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "ssh:\/\/git.corp.squareup.com\/ml\/UITestRunner.git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "68EDA75FC77F318B0BF9BAED4D569D9555E8E0D3"
},
{
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "ssh:\/\/git.corp.squareup.com\/ml\/snapshottestbinaries.git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "7F4664FA65AADBC384859AD71F1C93E5320699FF"
},
{
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "ssh:\/\/git.corp.squareup.com\/ios\/appointments.git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
Expand Down
4 changes: 1 addition & 3 deletions Cleanse/BaseComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import Foundation
public protocol _AnyBaseComponent : Configurable {
}


/// Base protocol for both Components and Subcomponenents

public protocol _BaseComponent : _AnyBaseComponent {
/// This is the binding required to construct a new subcomponent
/// This is the binding required to construct a new Component. Think of it as somewhat of an initialization value.
associatedtype Seed = Void

/// This should be set to the root type of object that is created.
Expand Down
6 changes: 0 additions & 6 deletions Cleanse/Component.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,3 @@ public protocol Component : _BaseComponent {
/// can have custom scopes associated with them.
associatedtype Scope = _Unscoped
}


/// Subcomponents have been renamed to Components
@available(*, deprecated, renamed="Component")
public typealias Subcomponent = Component

15 changes: 5 additions & 10 deletions Cleanse/ComponentFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,28 @@

import Foundation

/// Useful for extensions on `ComponentFactory` that also require
public protocol ComponentFactoryProtocol {
associatedtype ComponentElement: Cleanse.Component

func build(seed: ComponentElement.Seed) -> ComponentElement.Root
}

/// Instantiates a subcomponent and returns its root object.
public struct ComponentFactory<C: Component> : ComponentFactoryProtocol {
public typealias ComponentElement = C
private let factoryFunction: (seed: ComponentElement.Seed) -> C.Root

public func build(seed: ComponentElement.Seed) -> ComponentElement.Root {
return factoryFunction(seed: seed)
}

init(factoryFunction: (seed: ComponentElement.Seed) -> C.Root) {
self.factoryFunction = factoryFunction
}

/// Builds the Component and returns its root object.
public func build(seed: ComponentElement.Seed) -> ComponentElement.Root {
return factoryFunction(seed: seed)
}
}

extension ComponentFactoryProtocol where ComponentElement.Seed: ProviderProtocol {

/// Convenience initializer to construct subcomponents which have seeds of Provider types such as `TaggedProvider`s.

public func build(seed: ComponentElement.Seed.Element) -> ComponentElement.Root {
return build(ComponentElement.Seed.init(value: seed))
}

}
2 changes: 1 addition & 1 deletion Cleanse/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public struct InvalidScopeNesting : CleanseError {
let outerComponent: Any.Type

public var description: String {
return "Component (\(outerComponent)) contains subcomponent (\(innerComponent)) with same scope (\(scope))"
return "Component (\(outerComponent)) contains a component (\(innerComponent)) with the same scope: (\(scope))"
}
}

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ class Thermosiphon : Pump {

//: Module that configures `Thermosiphon` to be our `Pump`
struct PumpModule : Module {
func configure<B : Binder>(binder binder: B) {
binder.bind(Pump.self).to(factory: Thermosiphon.init)
static func configure<B : Binder>(binder binder: B) {
binder
.bind(Pump.self)
.to(factory: Thermosiphon.init)
}
}

Expand All @@ -61,17 +63,20 @@ class ElectricHeater : Heater {

//: Module that configures `ElectricHeater` to be our `Heater`. It also has a dependency on `PumpModule`.
struct DripCoffeeModule : Module {
func configure<B : Binder>(binder binder: B) {
binder.install(module: PumpModule())

binder.bind(Heater.self).asSingleton().to(factory: ElectricHeater.init)
static func configure<B : Binder>(binder binder: B) {
binder.install(module: PumpModule.self)

binder
.bind(Heater.self)
.asSingleton()
.to(factory: ElectricHeater.init)
}
}


//: Our root object. It requires both `Heater` and `Pump` to be configured.
class CoffeeMaker {
let heater: Provider<Heater> // Create a possibly costly heater only when we use it.
let heater: Provider<Heater> // Create a possibly costly heater only when we need it.
let pump: Pump

init(heater: Provider<Heater>, pump: Pump) {
Expand All @@ -87,28 +92,23 @@ class CoffeeMaker {
}
}

//: A component defines what our root of our object graph is and the modules it depends on to construct the root object
//: Now let's create the Component. A component defines what the root of our object graph is and the modules it depends on to construct that root object.

struct CoffeeMakerComponent : Component {
struct CoffeeMakerComponent : RootComponent {
typealias Root = CoffeeMaker

func configure<B : Binder>(binder binder: B) {
binder.install(module: DripCoffeeModule())
static func configure<B : Binder>(binder binder: B) {
binder.install(module: DripCoffeeModule.self)


// Bind our root object
binder.bind().to(factory: CoffeeMaker.init)
binder
.bind()
.to(factory: CoffeeMaker.init)
}
}

//: One calls the `build()` method on an instance of a component. This method returns the Root object if successful

let coffeeMaker = try! CoffeeMakerComponent().build()
// Create a ComponentFactory from the Component you'd like to use. Then call the `build()` method on that instance, which returns the Component's `Root`.
let coffeeMaker = try! ComponentFactory.of(CoffeeMakerComponent.self).build()

//: Now that we have our coffee maker, let's brew a cup of Joe!

coffeeMaker.brew()




0 comments on commit 621081b

Please sign in to comment.