Skip to content

Commit

Permalink
Rename RealmConfiguration to Realm.Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
segiddins committed Aug 12, 2015
1 parent a304203 commit 978ade1
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 276 deletions.
6 changes: 3 additions & 3 deletions RealmSwift-swift1.2/Migration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ exactly when and how migrations are performed.
*/
@availability(*, deprecated=1, message="Use migrateRealm(configuration:)")
public func migrateRealm(path: String, encryptionKey: NSData? = nil) -> NSError? {
var configuration = RealmConfiguration.defaultConfiguration
var configuration = Realm.Configuration.defaultConfiguration
configuration.path = path
configuration.encryptionKey = encryptionKey
return migrateRealm(configuration: configuration)
Expand All @@ -145,14 +145,14 @@ This method is called automatically when opening a Realm for the first time and
not need to be called explicitly. You can choose to call this method to control
exactly when and how migrations are performed.
:param: configuration The RealmConfiguration used to create the Realm to be
:param: configuration The Realm.Configuration used to create the Realm to be
migrated, and containing the schema version and migration
block used to perform the migration.
:returns: `nil` if the migration was successful, or an `NSError` object that describes the problem
that occured otherwise.
*/
public func migrateRealm(configuration: RealmConfiguration = RealmConfiguration.defaultConfiguration) -> NSError? {
public func migrateRealm(configuration: Realm.Configuration = Realm.Configuration.defaultConfiguration) -> NSError? {
return RLMRealm.migrateRealm(configuration.rlmConfiguration)
}

Expand Down
16 changes: 8 additions & 8 deletions RealmSwift-swift1.2/Realm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public final class Realm {
/// The Schema used by this realm.
public var schema: Schema { return Schema(rlmRealm.schema) }

/// Returns a `RealmConfiguration` that can be used to create this `Realm` instance.
public var configuration: RealmConfiguration { return RealmConfiguration.fromRLMConfiguration(rlmRealm.configuration) }
/// Returns a `Configuration` that can be used to create this `Realm` instance.
public var configuration: Configuration { return Configuration.fromRLMConfiguration(rlmRealm.configuration) }

/**
The location of the default Realm as a string. Can be overridden.
Expand All @@ -67,10 +67,10 @@ public final class Realm {
:returns: Location of the default Realm.
*/
@availability(*, deprecated=1, message="Use RealmConfiguration.defaultConfiguration")
@availability(*, deprecated=1, message="Use Configuration.defaultConfiguration")
public class var defaultPath: String {
get {
return RealmConfiguration.defaultConfiguration.path ?? RLMConfiguration.defaultRealmPath()
return Configuration.defaultConfiguration.path ?? RLMConfiguration.defaultRealmPath()
}
set {
RLMConfiguration.setDefaultPath(newValue)
Expand All @@ -81,14 +81,14 @@ public final class Realm {

/**
Obtains a Realm instance with the given configuration. Defaults to the default Realm configuration,
which can be changed by setting `RealmConfiguration.defaultConfiguration`.
which can be changed by setting `Realm.Configuration.defaultConfiguration`.
:param: configuration The configuration to use when creating the Realm instance.
:param: error If an error occurs, upon return contains an `NSError` object
that describes the problem. If you are not interested in
possible errors, omit the argument, or pass in `nil`.
*/
public convenience init?(configuration: RealmConfiguration, error: NSErrorPointer = nil) {
public convenience init?(configuration: Configuration, error: NSErrorPointer = nil) {
if let rlmRealm = RLMRealm(configuration: configuration.rlmConfiguration, error: error) {
self.init(rlmRealm)
} else {
Expand All @@ -98,7 +98,7 @@ public final class Realm {
}

/**
Obtains a Realm instance with the default `RealmConfiguration`.
Obtains a Realm instance with the default `Realm.Configuration`.
*/
public convenience init() {
let rlmRealm = RLMRealm.defaultRealm()
Expand Down Expand Up @@ -161,7 +161,7 @@ public final class Realm {
*/
@availability(*, deprecated=1, message="Use Realm(configuration:error:)")
public convenience init(inMemoryIdentifier: String) {
let configuration = RealmConfiguration(inMemoryIdentifier: inMemoryIdentifier).rlmConfiguration
let configuration = Configuration(inMemoryIdentifier: inMemoryIdentifier).rlmConfiguration
self.init(RLMRealm(configuration: configuration, error: nil)!)
}

Expand Down
260 changes: 131 additions & 129 deletions RealmSwift-swift1.2/RealmConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,140 +20,142 @@ import Foundation
import Realm
import Realm.Private

/**
A `RealmConfiguration` is used to describe the different options used to
create a `Realm` instance.
*/
public struct RealmConfiguration {

// MARK: Default Configuration

/// Returns the default RealmConfiguration used to create Realms when no other
/// configuration is explicitly specified (i.e. `Realm()`).
public static var defaultConfiguration: RealmConfiguration {
get {
return fromRLMConfiguration(RLMConfiguration.defaultConfiguration())
}
set {
RLMConfiguration.setDefaultConfiguration(newValue.rlmConfiguration)
}
}

// MARK: Initialization

/**
Initializes a `RealmConfiguration`, suitable for creating new `Realm` instances.
:param: path The path to the realm file.
:param: inMemoryIdentifier A string used to identify a particular in-memory Realm.
:param: encryptionKey 64-byte key to use to encrypt the data.
:param: readOnly Whether the Realm is read-only (must be true for read-only files).
:param: schemaVersion The current schema version.
:param: migrationBlock The block which migrates the Realm to the current version.
:returns: An initialized `RealmConfiguration`.
*/
public init(path: String? = RLMConfiguration.defaultRealmPath(),
inMemoryIdentifier: String? = nil,
encryptionKey: NSData? = nil,
readOnly: Bool = false,
schemaVersion: UInt64 = 0,
migrationBlock: MigrationBlock? = nil) {
self.path = path
self.inMemoryIdentifier = inMemoryIdentifier
self.encryptionKey = encryptionKey
self.readOnly = readOnly
self.schemaVersion = schemaVersion
self.migrationBlock = migrationBlock
}

// MARK: Configuration Properties

/// The path to the realm file.
/// Mutually exclusive with `inMemoryIdentifier`.
public var path: String? {
set {
if newValue != nil {
inMemoryIdentifier = nil
}
_path = newValue
}
get {
return _path
}
}

private var _path: String?

/// A string used to identify a particular in-memory Realm.
/// Mutually exclusive with `path`.
public var inMemoryIdentifier: String? {
set {
if newValue != nil {
path = nil
}
_inMemoryIdentifier = newValue
}
get {
return _inMemoryIdentifier
}
}

private var _inMemoryIdentifier: String? = nil

/// 64-byte key to use to encrypt the data.
public var encryptionKey: NSData? {
set {
_encryptionKey = RLMRealmValidatedEncryptionKey(newValue)
}
get {
return _encryptionKey
}
}

private var _encryptionKey: NSData? = nil

/// Whether the Realm is read-only (must be true for read-only files).
public var readOnly: Bool = false

/// The current schema version.
public var schemaVersion: UInt64 = 0

/// The block which migrates the Realm to the current version.
public var migrationBlock: MigrationBlock? = nil

// MARK: Private Methods

internal var rlmConfiguration: RLMConfiguration {
let configuration = RLMConfiguration()
configuration.path = self.path
configuration.inMemoryIdentifier = self.inMemoryIdentifier
configuration.encryptionKey = self.encryptionKey
configuration.readOnly = self.readOnly
configuration.schemaVersion = self.schemaVersion
configuration.migrationBlock = self.migrationBlock.map { accessorMigrationBlock($0) }
return configuration
}

internal static func fromRLMConfiguration(rlmConfiguration: RLMConfiguration) -> RealmConfiguration {
return RealmConfiguration(path: rlmConfiguration.path,
inMemoryIdentifier: rlmConfiguration.inMemoryIdentifier,
encryptionKey: rlmConfiguration.encryptionKey,
readOnly: rlmConfiguration.readOnly,
schemaVersion: UInt64(rlmConfiguration.schemaVersion),
migrationBlock: map(rlmConfiguration.migrationBlock) { rlmMigration in
return { migration, schemaVersion in
rlmMigration(migration.rlmMigration, schemaVersion)
}
})
}
extension Realm {
/**
A `Configuration` is used to describe the different options used to
create a `Realm` instance.
*/
public struct Configuration {

// MARK: Default Configuration

/// Returns the default Configuration used to create Realms when no other
/// configuration is explicitly specified (i.e. `Realm()`).
public static var defaultConfiguration: Configuration {
get {
return fromRLMConfiguration(RLMConfiguration.defaultConfiguration())
}
set {
RLMConfiguration.setDefaultConfiguration(newValue.rlmConfiguration)
}
}

// MARK: Initialization

/**
Initializes a `Configuration`, suitable for creating new `Realm` instances.
:param: path The path to the realm file.
:param: inMemoryIdentifier A string used to identify a particular in-memory Realm.
:param: encryptionKey 64-byte key to use to encrypt the data.
:param: readOnly Whether the Realm is read-only (must be true for read-only files).
:param: schemaVersion The current schema version.
:param: migrationBlock The block which migrates the Realm to the current version.
:returns: An initialized `Configuration`.
*/
public init(path: String? = RLMConfiguration.defaultRealmPath(),
inMemoryIdentifier: String? = nil,
encryptionKey: NSData? = nil,
readOnly: Bool = false,
schemaVersion: UInt64 = 0,
migrationBlock: MigrationBlock? = nil) {
self.path = path
self.inMemoryIdentifier = inMemoryIdentifier
self.encryptionKey = encryptionKey
self.readOnly = readOnly
self.schemaVersion = schemaVersion
self.migrationBlock = migrationBlock
}

// MARK: Configuration Properties

/// The path to the realm file.
/// Mutually exclusive with `inMemoryIdentifier`.
public var path: String? {
set {
if newValue != nil {
inMemoryIdentifier = nil
}
_path = newValue
}
get {
return _path
}
}

private var _path: String?

/// A string used to identify a particular in-memory Realm.
/// Mutually exclusive with `path`.
public var inMemoryIdentifier: String? {
set {
if newValue != nil {
path = nil
}
_inMemoryIdentifier = newValue
}
get {
return _inMemoryIdentifier
}
}

private var _inMemoryIdentifier: String? = nil

/// 64-byte key to use to encrypt the data.
public var encryptionKey: NSData? {
set {
_encryptionKey = RLMRealmValidatedEncryptionKey(newValue)
}
get {
return _encryptionKey
}
}

private var _encryptionKey: NSData? = nil

/// Whether the Realm is read-only (must be true for read-only files).
public var readOnly: Bool = false

/// The current schema version.
public var schemaVersion: UInt64 = 0

/// The block which migrates the Realm to the current version.
public var migrationBlock: MigrationBlock? = nil

// MARK: Private Methods

internal var rlmConfiguration: RLMConfiguration {
let configuration = RLMConfiguration()
configuration.path = self.path
configuration.inMemoryIdentifier = self.inMemoryIdentifier
configuration.encryptionKey = self.encryptionKey
configuration.readOnly = self.readOnly
configuration.schemaVersion = self.schemaVersion
configuration.migrationBlock = self.migrationBlock.map { accessorMigrationBlock($0) }
return configuration
}

internal static func fromRLMConfiguration(rlmConfiguration: RLMConfiguration) -> Configuration {
return Configuration(path: rlmConfiguration.path,
inMemoryIdentifier: rlmConfiguration.inMemoryIdentifier,
encryptionKey: rlmConfiguration.encryptionKey,
readOnly: rlmConfiguration.readOnly,
schemaVersion: UInt64(rlmConfiguration.schemaVersion),
migrationBlock: map(rlmConfiguration.migrationBlock) { rlmMigration in
return { migration, schemaVersion in
rlmMigration(migration.rlmMigration, schemaVersion)
}
})
}
}
}

// MARK: Printable

extension RealmConfiguration: Printable {
extension Realm.Configuration: Printable {
/// Returns a human-readable description of the configuration.
public var description: String {
return gsub("\\ARLMConfiguration", "RealmConfiguration", rlmConfiguration.description) ?? ""
return gsub("\\ARLMConfiguration", "Configuration", rlmConfiguration.description) ?? ""
}
}
2 changes: 1 addition & 1 deletion RealmSwift-swift1.2/Tests/ObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class ObjectTests: TestCase {
}
autoreleasepool {
var enumerated = false
let configuration = RealmConfiguration(schemaVersion: 1, migrationBlock: { migration, _ in
let configuration = Realm.Configuration(schemaVersion: 1, migrationBlock: { migration, _ in
migration.enumerate(SwiftObject.className()) { oldObject, newObject in
if let newObject = newObject {
block(newObject, migration)
Expand Down
12 changes: 6 additions & 6 deletions RealmSwift-swift1.2/Tests/RealmConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import class Realm.Private.RLMConfiguration

class RealmConfigurationTests: TestCase {
func testDefaultConfiguration() {
let defaultConfiguration = RealmConfiguration.defaultConfiguration
let defaultConfiguration = Realm.Configuration.defaultConfiguration

XCTAssertEqual(defaultConfiguration.path!, Realm().path);
XCTAssertNil(defaultConfiguration.inMemoryIdentifier);
Expand All @@ -33,10 +33,10 @@ class RealmConfigurationTests: TestCase {
}

func testSetDefaultConfiguration() {
let path = RealmConfiguration.defaultConfiguration.path!
let configuration = RealmConfiguration(path: "path")
RealmConfiguration.defaultConfiguration = configuration
XCTAssertEqual(RealmConfiguration.defaultConfiguration.path!, "path")
RealmConfiguration.defaultConfiguration.path = path
let path = Realm.Configuration.defaultConfiguration.path!
let configuration = Realm.Configuration(path: "path")
Realm.Configuration.defaultConfiguration = configuration
XCTAssertEqual(Realm.Configuration.defaultConfiguration.path!, "path")
Realm.Configuration.defaultConfiguration.path = path
}
}
Loading

0 comments on commit 978ade1

Please sign in to comment.