Skip to content

Commit

Permalink
Separate Mappable from ImmutableMappable
Browse files Browse the repository at this point in the history
To each one of them implement one way of `init?(fromFileName file: String)`.
  • Loading branch information
Rafael da Silva Ferreira committed Mar 30, 2017
1 parent ea3628e commit eb27dfb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion JSONStub.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'JSONStub'
s.version = '0.1.2'
s.version = '0.1.3'
s.summary = 'Use JSONStub to easily load JSON files into your Mappable objects.'

# This description is used to generate tags and improve search results.
Expand Down
10 changes: 0 additions & 10 deletions JSONStub/Classes/BaseMappable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ import struct Foundation.Data
import protocol ObjectMapper.BaseMappable

public extension BaseMappable {
init?(fromFileName file: String) {
guard let json = file.fileDictionary() else { return nil }

self.init(JSON: json)
}

init?(fromStub stub: FileStub) {
self.init(fromFileName: stub.fileName)
}

var asData: Data {
guard let json = toJSONString(), let data = json.data(using: .utf8), json != "{}" else { return Data() }

Expand Down
8 changes: 6 additions & 2 deletions JSONStub/Classes/ImmutableMappable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
import protocol ObjectMapper.ImmutableMappable

public extension ImmutableMappable {
init?(fromFileName file: String) {
init?(fromFileName file: String) throws {
guard let json = file.fileDictionary() else { return nil }

try self.init(JSON: json)
}

init?(fromStub stub: FileStub) {
do {
try self.init(JSON: json)
try self.init(fromFileName: stub.fileName)
} catch {
return nil
}
Expand Down
21 changes: 21 additions & 0 deletions JSONStub/Classes/Mappable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Mappable.swift
// Pods
//
// Created by Rafael Ferreira on 3/30/17.
//
//

import protocol ObjectMapper.Mappable

extension Mappable {
init?(fromFileName file: String) {
guard let json = file.fileDictionary() else { return nil }

self.init(JSON: json)
}

init?(fromStub stub: FileStub) {
self.init(fromFileName: stub.fileName)
}
}

0 comments on commit eb27dfb

Please sign in to comment.