Skip to content

Commit

Permalink
Merge pull request #4 from aryaxt/PassContainerToModuleClass
Browse files Browse the repository at this point in the history
Passing container to module, this way nested dependencies can be done…
  • Loading branch information
aryaxt authored Jun 12, 2016
2 parents a3e3fee + adaf189 commit 0d91f0c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions SwiftInjection.podspec
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Pod::Spec.new do |s|
s.name = 'SwiftInjection'
s.version = '0.2'
s.version = '0.3'
s.summary = 'A dependency injection framework for swift'
s.homepage = 'https://github.com/aryaxt/SwiftInjection'
s.license = {
:type => 'MIT',
:file => 'License.txt'
}
s.author = {'Aryan Ghassemi' => 'https://github.com/aryaxt/SwiftInjection'}
s.source = {:git => 'https://github.com/aryaxt/SwiftInjection.git', :tag => '0.2'}
s.source = {:git => 'https://github.com/aryaxt/SwiftInjection.git', :tag => '0.3'}
s.platform = :ios, '8.0'
s.source_files = 'SwiftInjection/*.{swift}'
s.framework = 'Foundation'
Expand Down
2 changes: 1 addition & 1 deletion SwiftInjection/DIContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class DIContainer {
- parameter module: A subclass of DIAbstractModule that implements DIModule
*/
public func addModule<T: DIAbstractModule where T: DIModule>(module: T) {
module.load()
module.load(container: self)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion SwiftInjection/DIModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
//

public protocol DIModule {
func load()
func load(container container: DIContainer)
}
5 changes: 3 additions & 2 deletions SwiftInjectionExample/SwiftInjectionExample/AppModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import SwiftInjection

public class AppModule: DIAbstractModule, DIModule {

public func load() {
bind(GithubClient.self) { GithubHttpClient(baseUrl: "https://api.github.com") }
public func load(container container: DIContainer) {
bind(Client.self) { HttpClient(baseUrl: "https://api.github.com") }
bind(GithubClient.self) { GithubHttpClient(client: container.resolve(Client.self)) }
bind(NSUserDefaults.self, asSingleton: false) { NSUserDefaults.standardUserDefaults() }
bind(AnalyticsTracker.self, named: GoogleAnalyticsTracker.analyticsIdentifier()) { GoogleAnalyticsTracker() }
bind(AnalyticsTracker.self, named: AmplitudeAnalyticsTracker.analyticsIdentifier()) { AmplitudeAnalyticsTracker() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public enum ClientError: ErrorType {
case InvalidResponse
}

protocol Client {
public protocol Client {
func fetchObject<T: Mappable>(type type: T.Type, path: String, method: HttpMethod, completion: Result<T>->Void) -> NSURLSessionDataTask
func fetchObjects<T: Mappable>(type type: T.Type, path: String, method: HttpMethod, completion: Result<[T]>->Void) -> NSURLSessionDataTask
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@

import Foundation

public class GithubHttpClient: HttpClient, GithubClient {
public class GithubHttpClient: GithubClient {

private let client: Client

public init(client: Client) {
self.client = client
}

public func fetchRepos(user user: String, completion: Result<[Repository]>->Void) -> NSURLSessionTask {
return fetchObjects(type: Repository.self, path: "users/\(user)/repos", method: .Get, completion: completion)
return client.fetchObjects(type: Repository.self, path: "users/\(user)/repos", method: .Get, completion: completion)
}

}

0 comments on commit 0d91f0c

Please sign in to comment.