Skip to content

Commit

Permalink
Merge pull request #13 from Teknasyon-Teknoloji/v1.2.1
Browse files Browse the repository at this point in the history
[fix] Fix objectsCount in UserDefaultsStore
  • Loading branch information
omaralbeik committed Jan 2, 2019
2 parents a340bb4 + 4dae43f commit 5cb669c
Show file tree
Hide file tree
Showing 37 changed files with 112 additions and 94 deletions.
2 changes: 1 addition & 1 deletion PersistenceKit.podspec
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "PersistenceKit"
s.version = "1.2"
s.version = "1.2.1"
s.summary = "
Store and retrieve Codable objects to various persistence layers, in a couple lines of code! "
s.description = <<-DESC
Expand Down
12 changes: 6 additions & 6 deletions Sources/Files/FilesStore.swift
Expand Up @@ -31,16 +31,16 @@ open class FilesStore<T: Codable & Identifiable> {
/// **Warning**: Never use the same identifier for two -or more- different stores.
public let uniqueIdentifier: String

/// Store `Expiration` option. _default is `.never`_
/// Store `Expiration` option. _default is .never_
public let expiration: Expiration

/// JSON encoder. _default is `JSONEncoder()`_
/// JSON encoder. _default is JSONEncoder()_
open var encoder = JSONEncoder()

/// JSON decoder. _default is `JSONDecoder()`_
/// JSON decoder. _default is JSONDecoder()_
open var decoder = JSONDecoder()

/// FileManager. _default is `FileManager.default`_
/// FileManager. _default is FileManager.default_
private var manager = FileManager.default

/// Whether keys should be hashed before storing or not.
Expand All @@ -52,8 +52,8 @@ open class FilesStore<T: Codable & Identifiable> {
///
/// - Parameters:
/// - uniqueIdentifier: store's unique identifier.
/// - useHashing: Whether keys should be hashed before storing or not. _default is `false`_
/// - expiryDuration: optional store's expiry duration _default is `.never`_.
/// - useHashing: Whether keys should be hashed before storing or not. _default is false_
/// - expiryDuration: optional store's expiry duration _default is .never_.
required public init(uniqueIdentifier: String, useHashing: Bool = false, expiration: Expiration = .never) {
self.uniqueIdentifier = uniqueIdentifier
self.useHashing = useHashing
Expand Down
10 changes: 5 additions & 5 deletions Sources/Files/SingleFileStore.swift
Expand Up @@ -31,16 +31,16 @@ open class SingleFileStore<T: Codable> {
/// **Warning**: Never use the same identifier for two -or more- different stores.
public let uniqueIdentifier: String

/// Store `Expiration` option. _default is `.never`_
/// Store `Expiration` option. _default is .never_
public let expiration: Expiration

/// JSON encoder. _default is `JSONEncoder()`_
/// JSON encoder. _default is JSONEncoder()_
open var encoder = JSONEncoder()

/// JSON decoder. _default is `JSONDecoder()`_
/// JSON decoder. _default is JSONDecoder()_
open var decoder = JSONDecoder()

/// FileManager. _default is `FileManager.default`_
/// FileManager. _default is FileManager.default_
private var manager = FileManager.default

/// Initialize store with given identifier.
Expand All @@ -49,7 +49,7 @@ open class SingleFileStore<T: Codable> {
///
/// - Parameters:
/// - uniqueIdentifier: store's unique identifier.
/// - expiryDuration: optional store's expiry duration _default is `.never`_.
/// - expiryDuration: optional store's expiry duration _default is .never_.
required public init(uniqueIdentifier: String, expiration: Expiration = .never) {
self.uniqueIdentifier = uniqueIdentifier
self.expiration = expiration
Expand Down
2 changes: 1 addition & 1 deletion Sources/Info.plist
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.2</string>
<string>1.2.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
10 changes: 5 additions & 5 deletions Sources/Keychain/SingleKeychainStore.swift
Expand Up @@ -37,10 +37,10 @@ open class SingleKeychainStore<T: Codable> {
/// **Warning**: Never use the same identifier for two -or more- different stores.
public let uniqueIdentifier: String

/// JSON encoder. _default is `JSONEncoder()`_
/// JSON encoder. _default is JSONEncoder()_
open var encoder = JSONEncoder()

/// JSON decoder. _default is `JSONDecoder()`_
/// JSON decoder. _default is JSONDecoder()_
open var decoder = JSONDecoder()

/// Initialize store.
Expand Down Expand Up @@ -82,7 +82,7 @@ open class SingleKeychainStore<T: Codable> {
///
/// - Parameters:
/// - object: object to save.
/// - option: `KeychainAccessibilityOption`. _default is `KeychainAccessibilityOption.whenUnlocked`_
/// - option: `KeychainAccessibilityOption`. _default is KeychainAccessibilityOption.whenUnlocked_
/// - Throws: JSON encoding error.
open func update(_ object: T, withAccessibilityOption option: KeychainAccessibilityOption = .whenUnlocked) throws {
let data = try encoder.encode(generateDict(for: object))
Expand All @@ -102,7 +102,7 @@ open class SingleKeychainStore<T: Codable> {

/// Get object from store.
///
/// - Parameter option: `KeychainAccessibilityOption`. _default is `KeychainAccessibilityOption.whenUnlocked`_
/// - Parameter option: `KeychainAccessibilityOption`. _default is KeychainAccessibilityOption.whenUnlocked_
/// - Returns: optional object.
open func object(accessibilityOption option: KeychainAccessibilityOption = .whenUnlocked) -> T? {
var query = generateQuery(accessibilityOption: option)
Expand All @@ -119,7 +119,7 @@ open class SingleKeychainStore<T: Codable> {

/// Delete object from store.
///
/// - Parameter option: KeychainAccessibilityOption. _default is `KeychainAccessibilityOption.whenUnlocked`_
/// - Parameter option: KeychainAccessibilityOption. _default is KeychainAccessibilityOption.whenUnlocked_
open func delete(accessibilityOption option: KeychainAccessibilityOption = .whenUnlocked) {
let query = generateQuery(accessibilityOption: option)
SecItemDelete(query as CFDictionary)
Expand Down
6 changes: 3 additions & 3 deletions Sources/UserDefaults/SingleUserDefaultsStore.swift
Expand Up @@ -31,10 +31,10 @@ open class SingleUserDefaultsStore<T: Codable> {
/// **Warning**: Never use the same identifier for two -or more- different stores.
public let uniqueIdentifier: String

/// JSON encoder. _default is `JSONEncoder()`_
/// JSON encoder. _default is JSONEncoder()_
open var encoder = JSONEncoder()

/// JSON decoder. _default is `JSONDecoder()`_
/// JSON decoder. _default is JSONDecoder()_
open var decoder = JSONDecoder()

/// UserDefaults store.
Expand All @@ -46,7 +46,7 @@ open class SingleUserDefaultsStore<T: Codable> {
///
/// - Parameters:
/// - uniqueIdentifier: uniqueIdentifier: store's unique identifier.
/// - initialObject: optional object to save to store initially. _default is `nil`_
/// - initialObject: optional object to save to store initially. _default is nil_
required public init?(uniqueIdentifier: String, initialObject: T? = nil) {
guard let store = UserDefaults(suiteName: uniqueIdentifier) else { return nil }
self.uniqueIdentifier = uniqueIdentifier
Expand Down
13 changes: 8 additions & 5 deletions Sources/UserDefaults/UserDefaultsStore.swift
Expand Up @@ -31,10 +31,10 @@ open class UserDefaultsStore<T: Codable & Identifiable> {
/// **Warning**: Never use the same identifier for two -or more- different stores.
public let uniqueIdentifier: String

/// JSON encoder. _default is `JSONEncoder()`_
/// JSON encoder. _default is JSONEncoder()_
open var encoder = JSONEncoder()

/// JSON decoder. _default is `JSONDecoder()`_
/// JSON decoder. _default is JSONDecoder()_
open var decoder = JSONDecoder()

/// UserDefaults store.
Expand All @@ -49,7 +49,7 @@ open class UserDefaultsStore<T: Codable & Identifiable> {
///
/// - Parameters:
/// - uniqueIdentifier: uniqueIdentifier: store's unique identifier.
/// - useHashing: Whether keys should be hashed before storing or not. _default is `false`_
/// - useHashing: Whether keys should be hashed before storing or not. _default is false_
required public init?(uniqueIdentifier: String, useHashing: Bool = false) {
guard let store = UserDefaults(suiteName: uniqueIdentifier) else { return nil }
self.uniqueIdentifier = uniqueIdentifier
Expand All @@ -63,8 +63,11 @@ open class UserDefaultsStore<T: Codable & Identifiable> {
/// - Throws: JSON encoding error.
public func save(_ object: T) throws {
let data = try encoder.encode(object)
let isObjectAlreadyAdded = hasObject(withId: object[keyPath: T.idKey])
store.set(data, forKey: key(for: object))
increaseCounter()
if !isObjectAlreadyAdded {
increaseCounter()
}
}

/// Save optional object (if not nil) to store.
Expand Down Expand Up @@ -198,7 +201,7 @@ private extension UserDefaultsStore {
/// - Parameter id: object id.
/// - Returns: UserDefaults key for given id.
func key(for id: T.ID) -> String {
return useHashing ? "\(uniqueIdentifier)-\(id.hashValue)" : "\(uniqueIdentifier)-\(id.hashValue)"
return useHashing ? "\(uniqueIdentifier)-\(id.hashValue)" : "\(uniqueIdentifier)-\(id)"
}

/// Check if a UserDefaults key is an object key.
Expand Down
15 changes: 15 additions & 0 deletions Tests/UserDefaults/UserDefaultsStoreTests.swift
Expand Up @@ -99,6 +99,21 @@ final class StoreTests: XCTestCase {
XCTAssert(users.contains(TestUser.johnson))
}

func testCount() {
let store = createFreshUsersStore()!
XCTAssertEqual(store.objectsCount, 0)

XCTAssertNoThrow(try store.save(TestUser.james))
XCTAssertEqual(store.objectsCount, 1)

XCTAssertNoThrow(try store.save(TestUser.james))
XCTAssertNoThrow(try store.save(TestUser.james))
XCTAssertEqual(store.objectsCount, 1)

XCTAssertNoThrow(try store.save(TestUser.johnson))
XCTAssertEqual(store.objectsCount, 2)
}

func testDeleteObject() {
let store = createFreshUsersStore()!

Expand Down
2 changes: 1 addition & 1 deletion docs/Classes.html
Expand Up @@ -244,7 +244,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2018 <a class="link" href="https://github.com/Teknasyon-Teknoloji/PersistenceKit" target="_blank" rel="external">Omar Albeik</a>. All rights reserved. (Last updated: 2018-12-31)</p>
<p>&copy; 2019 <a class="link" href="https://github.com/Teknasyon-Teknoloji/PersistenceKit" target="_blank" rel="external">Omar Albeik</a>. All rights reserved. (Last updated: 2019-01-02)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.4</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
12 changes: 6 additions & 6 deletions docs/Classes/FilesStore.html
Expand Up @@ -129,7 +129,7 @@ <h4>Declaration</h4>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>Store <code><a href="../Enums/Expiration.html">Expiration</a></code> option. <em>default is <code>.never</code></em></p>
<p>Store <code><a href="../Enums/Expiration.html">Expiration</a></code> option. <em>default is .never</em></p>

</div>
<div class="declaration">
Expand All @@ -156,7 +156,7 @@ <h4>Declaration</h4>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>JSON encoder. <em>default is <code>JSONEncoder()</code></em></p>
<p>JSON encoder. <em>default is JSONEncoder()</em></p>

</div>
<div class="declaration">
Expand All @@ -183,7 +183,7 @@ <h4>Declaration</h4>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>JSON decoder. <em>default is <code>JSONDecoder()</code></em></p>
<p>JSON decoder. <em>default is JSONDecoder()</em></p>

</div>
<div class="declaration">
Expand Down Expand Up @@ -247,7 +247,7 @@ <h4>Parameters</h4>
</td>
<td>
<div>
<p>Whether keys should be hashed before storing or not. <em>default is <code>false</code></em></p>
<p>Whether keys should be hashed before storing or not. <em>default is false</em></p>
</div>
</td>
</tr>
Expand All @@ -259,7 +259,7 @@ <h4>Parameters</h4>
</td>
<td>
<div>
<p>optional store&rsquo;s expiry duration <em>default is <code>.never</code></em>.</p>
<p>optional store&rsquo;s expiry duration <em>default is .never</em>.</p>
</div>
</td>
</tr>
Expand Down Expand Up @@ -800,7 +800,7 @@ <h4>Parameters</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2018 <a class="link" href="https://github.com/Teknasyon-Teknoloji/PersistenceKit" target="_blank" rel="external">Omar Albeik</a>. All rights reserved. (Last updated: 2018-12-31)</p>
<p>&copy; 2019 <a class="link" href="https://github.com/Teknasyon-Teknoloji/PersistenceKit" target="_blank" rel="external">Omar Albeik</a>. All rights reserved. (Last updated: 2019-01-02)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.4</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
10 changes: 5 additions & 5 deletions docs/Classes/SingleFileStore.html
Expand Up @@ -129,7 +129,7 @@ <h4>Declaration</h4>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>Store <code><a href="../Enums/Expiration.html">Expiration</a></code> option. <em>default is <code>.never</code></em></p>
<p>Store <code><a href="../Enums/Expiration.html">Expiration</a></code> option. <em>default is .never</em></p>

</div>
<div class="declaration">
Expand All @@ -156,7 +156,7 @@ <h4>Declaration</h4>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>JSON encoder. <em>default is <code>JSONEncoder()</code></em></p>
<p>JSON encoder. <em>default is JSONEncoder()</em></p>

</div>
<div class="declaration">
Expand All @@ -183,7 +183,7 @@ <h4>Declaration</h4>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>JSON decoder. <em>default is <code>JSONDecoder()</code></em></p>
<p>JSON decoder. <em>default is JSONDecoder()</em></p>

</div>
<div class="declaration">
Expand Down Expand Up @@ -247,7 +247,7 @@ <h4>Parameters</h4>
</td>
<td>
<div>
<p>optional store&rsquo;s expiry duration <em>default is <code>.never</code></em>.</p>
<p>optional store&rsquo;s expiry duration <em>default is .never</em>.</p>
</div>
</td>
</tr>
Expand Down Expand Up @@ -445,7 +445,7 @@ <h4>Declaration</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2018 <a class="link" href="https://github.com/Teknasyon-Teknoloji/PersistenceKit" target="_blank" rel="external">Omar Albeik</a>. All rights reserved. (Last updated: 2018-12-31)</p>
<p>&copy; 2019 <a class="link" href="https://github.com/Teknasyon-Teknoloji/PersistenceKit" target="_blank" rel="external">Omar Albeik</a>. All rights reserved. (Last updated: 2019-01-02)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.4</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down
12 changes: 6 additions & 6 deletions docs/Classes/SingleKeychainStore.html
Expand Up @@ -158,7 +158,7 @@ <h4>Declaration</h4>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>JSON encoder. <em>default is <code>JSONEncoder()</code></em></p>
<p>JSON encoder. <em>default is JSONEncoder()</em></p>

</div>
<div class="declaration">
Expand All @@ -185,7 +185,7 @@ <h4>Declaration</h4>
<section class="section">
<div class="pointer"></div>
<div class="abstract">
<p>JSON decoder. <em>default is <code>JSONDecoder()</code></em></p>
<p>JSON decoder. <em>default is JSONDecoder()</em></p>

</div>
<div class="declaration">
Expand Down Expand Up @@ -375,7 +375,7 @@ <h4>Parameters</h4>
</td>
<td>
<div>
<p><code><a href="../Enums/KeychainAccessibilityOption.html">KeychainAccessibilityOption</a></code>. <em>default is <code><a href="../Enums/KeychainAccessibilityOption.html#/s:14PersistenceKit27KeychainAccessibilityOptionO12whenUnlockedyA2CmF">KeychainAccessibilityOption.whenUnlocked</a></code></em></p>
<p><code><a href="../Enums/KeychainAccessibilityOption.html">KeychainAccessibilityOption</a></code>. <em>default is KeychainAccessibilityOption.whenUnlocked</em></p>
</div>
</td>
</tr>
Expand Down Expand Up @@ -421,7 +421,7 @@ <h4>Parameters</h4>
</td>
<td>
<div>
<p><code><a href="../Enums/KeychainAccessibilityOption.html">KeychainAccessibilityOption</a></code>. <em>default is <code><a href="../Enums/KeychainAccessibilityOption.html#/s:14PersistenceKit27KeychainAccessibilityOptionO12whenUnlockedyA2CmF">KeychainAccessibilityOption.whenUnlocked</a></code></em></p>
<p><code><a href="../Enums/KeychainAccessibilityOption.html">KeychainAccessibilityOption</a></code>. <em>default is KeychainAccessibilityOption.whenUnlocked</em></p>
</div>
</td>
</tr>
Expand Down Expand Up @@ -471,7 +471,7 @@ <h4>Parameters</h4>
</td>
<td>
<div>
<p>KeychainAccessibilityOption. <em>default is <code><a href="../Enums/KeychainAccessibilityOption.html#/s:14PersistenceKit27KeychainAccessibilityOptionO12whenUnlockedyA2CmF">KeychainAccessibilityOption.whenUnlocked</a></code></em></p>
<p>KeychainAccessibilityOption. <em>default is KeychainAccessibilityOption.whenUnlocked</em></p>
</div>
</td>
</tr>
Expand All @@ -486,7 +486,7 @@ <h4>Parameters</h4>
</section>
</section>
<section id="footer">
<p>&copy; 2018 <a class="link" href="https://github.com/Teknasyon-Teknoloji/PersistenceKit" target="_blank" rel="external">Omar Albeik</a>. All rights reserved. (Last updated: 2018-12-31)</p>
<p>&copy; 2019 <a class="link" href="https://github.com/Teknasyon-Teknoloji/PersistenceKit" target="_blank" rel="external">Omar Albeik</a>. All rights reserved. (Last updated: 2019-01-02)</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.4</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</article>
Expand Down

0 comments on commit 5cb669c

Please sign in to comment.