This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking repository and datasource in CRUD based
- Loading branch information
Joan Martin
committed
Jun 27, 2018
1 parent
d1e5ff7
commit 6c88ff1
Showing
18 changed files
with
223 additions
and
99 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
MJSwiftCore/Classes/Common/Repository/DataSourceRepository.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
// | ||
// Copyright 2018 Mobile Jazz SL | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
/// | ||
/// Single data source repository. | ||
/// All repository methods are directly forwarded to a single data source. | ||
/// Operation parameter is not used in any case. | ||
/// | ||
public class GetDataSourceRepository<D: GetDataSource,T> : GetRepository where D.T == T { | ||
|
||
private let dataSource : D | ||
|
||
/// Default initializer | ||
/// | ||
/// - Parameters: | ||
/// - dataSource: The contained data source | ||
public init(_ dataSource: D) { | ||
self.dataSource = dataSource | ||
} | ||
|
||
public func get(_ query: Query, operation: Operation = BlankOperation()) -> Future<T> { | ||
return dataSource.get(query) | ||
} | ||
|
||
public func getAll(_ query: Query, operation: Operation = BlankOperation()) -> Future<[T]> { | ||
return dataSource.getAll(query) | ||
} | ||
} | ||
|
||
extension GetDataSource { | ||
/// Creates a single data source repository from a data source | ||
/// | ||
/// - Returns: A SingleGetDataSourceRepository repository | ||
public func toGetRepository() -> AnyGetRepository<T> { | ||
return GetDataSourceRepository(self).asAnyGetRepository() | ||
} | ||
} | ||
|
||
/// | ||
/// Single data source repository. | ||
/// All repository methods are directly forwarded to a single data source. | ||
/// Operation parameter is not used in any case. | ||
/// | ||
public class PutDataSourceRepository<D: PutDataSource,T> : PutRepository where D.T == T { | ||
|
||
private let dataSource : D | ||
|
||
/// Default initializer | ||
/// | ||
/// - Parameters: | ||
/// - dataSource: The contained data source | ||
public init(_ dataSource: D) { | ||
self.dataSource = dataSource | ||
} | ||
|
||
@discardableResult | ||
public func put(_ value: T?, in query: Query, operation: Operation = BlankOperation()) -> Future<T> { | ||
return dataSource.put(value, in: query) | ||
} | ||
|
||
@discardableResult | ||
public func putAll(_ array: [T], in query: Query, operation: Operation = BlankOperation()) -> Future<[T]> { | ||
return dataSource.putAll(array, in: query) | ||
} | ||
} | ||
|
||
extension PutDataSource { | ||
/// Creates a single data source repository from a data source | ||
/// | ||
/// - Returns: A SinglePutDataSourceRepository repository | ||
public func toPutRepository() -> AnyPutRepository<T> { | ||
return PutDataSourceRepository(self).asAnyPutRepository() | ||
} | ||
} | ||
|
||
/// | ||
/// Single data source repository. | ||
/// All repository methods are directly forwarded to a single data source. | ||
/// Operation parameter is not used in any case. | ||
/// | ||
public class DeleteDataSourceRepository<D: DeleteDataSource,T> : DeleteRepository where D.T == T { | ||
|
||
private let dataSource : D | ||
|
||
/// Default initializer | ||
/// | ||
/// - Parameters: | ||
/// - dataSource: The contained data source | ||
public init(_ dataSource: D) { | ||
self.dataSource = dataSource | ||
} | ||
|
||
@discardableResult | ||
public func delete(_ query: Query, operation: Operation = BlankOperation()) -> Future<Void> { | ||
return dataSource.delete(query) | ||
} | ||
|
||
@discardableResult | ||
public func deleteAll(_ query: Query, operation: Operation = BlankOperation()) -> Future<Void> { | ||
return dataSource.deleteAll(query) | ||
} | ||
} | ||
|
||
extension DeleteDataSource { | ||
/// Creates a single data source repository from a data source | ||
/// | ||
/// - Returns: A SingleDeleteDataSourceRepository repository | ||
public func toDeleteRepository() -> AnyDeleteRepository<T> { | ||
return DeleteDataSourceRepository(self).asAnyDeleteRepository() | ||
} | ||
} | ||
|
||
/// | ||
/// Single data source repository. | ||
/// All repository methods are directly forwarded to a single data source. | ||
/// Operation parameter is not used in any case. | ||
/// | ||
public class DataSourceRepository<Get: GetDataSource,Put: PutDataSource,Delete: DeleteDataSource,T> : Repository where Get.T == T, Put.T == T, Delete.T == T { | ||
|
||
private let getDataSource : Get | ||
private let putDataSource : Put | ||
private let deleteDataSource : Delete | ||
|
||
/// Main initializer | ||
/// | ||
/// - Parameters: | ||
/// - getDataSource: The get data source | ||
/// - putDataSource: The put data source | ||
/// - deleteDataSource: The delete data source | ||
public init(get getDataSource: Get, put putDataSource: Put, delete deleteDataSource: Delete) { | ||
self.getDataSource = getDataSource | ||
self.putDataSource = putDataSource | ||
self.deleteDataSource = deleteDataSource | ||
} | ||
|
||
public func get(_ query: Query, operation: Operation = BlankOperation()) -> Future<T> { | ||
return getDataSource.get(query) | ||
} | ||
|
||
public func getAll(_ query: Query, operation: Operation = BlankOperation()) -> Future<[T]> { | ||
return getDataSource.getAll(query) | ||
} | ||
|
||
@discardableResult | ||
public func put(_ value: T?, in query: Query, operation: Operation = BlankOperation()) -> Future<T> { | ||
return putDataSource.put(value, in: query) | ||
} | ||
|
||
@discardableResult | ||
public func putAll(_ array: [T], in query: Query, operation: Operation = BlankOperation()) -> Future<[T]> { | ||
return putDataSource.putAll(array, in: query) | ||
} | ||
|
||
@discardableResult | ||
public func delete(_ query: Query, operation: Operation = BlankOperation()) -> Future<Void> { | ||
return deleteDataSource.delete(query) | ||
} | ||
|
||
@discardableResult | ||
public func deleteAll(_ query: Query, operation: Operation = BlankOperation()) -> Future<Void> { | ||
return deleteDataSource.deleteAll(query) | ||
} | ||
} | ||
|
||
extension DataSourceRepository where Get == Put, Get == Delete { | ||
/// Initializer for a single DataSource | ||
/// | ||
/// - Parameter dataSource: The data source | ||
public convenience init(_ dataSource: Get) { | ||
self.init(get: dataSource, put: dataSource, delete: dataSource) | ||
} | ||
} | ||
|
||
extension DataSource { | ||
/// Creates a single data source repository from a data source | ||
/// | ||
/// - Returns: A SingleDataSourceRepository repository | ||
public func toRepository() -> AnyRepository<T> { | ||
return DataSourceRepository(self).asAnyRepository() | ||
} | ||
} |
Oops, something went wrong.