Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Brick.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
BD6D7AE21CBA768A00FBF0BD /* Fakery.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BD6D7AE11CBA768A00FBF0BD /* Fakery.framework */; };
BDDA5FEB1CBBCF45000FD5A6 /* ViewConfigurable.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDDA5FEA1CBBCF45000FD5A6 /* ViewConfigurable.swift */; };
BDDA5FEC1CBBCF45000FD5A6 /* ViewConfigurable.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDDA5FEA1CBBCF45000FD5A6 /* ViewConfigurable.swift */; };
D57832991CE142DE005ED144 /* Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = D57832961CE142C8005ED144 /* Helpers.swift */; };
D578329A1CE142DF005ED144 /* Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = D57832961CE142C8005ED144 /* Helpers.swift */; };
D5ACAC9A1CCE439F00567809 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5ACAC991CCE439F00567809 /* Extensions.swift */; };
D5ACAC9B1CCE439F00567809 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5ACAC991CCE439F00567809 /* Extensions.swift */; };
D5ACAC9F1CCE45CE00567809 /* ExtensionsSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5ACAC9C1CCE449A00567809 /* ExtensionsSpec.swift */; };
Expand Down Expand Up @@ -60,6 +62,7 @@
BDDA5FEA1CBBCF45000FD5A6 /* ViewConfigurable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewConfigurable.swift; sourceTree = "<group>"; };
D500FD111C3AABED00782D78 /* Playground-iOS.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = "Playground-iOS.playground"; sourceTree = "<group>"; };
D500FD121C3AAC8E00782D78 /* Playground-Mac.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = "Playground-Mac.playground"; sourceTree = "<group>"; };
D57832961CE142C8005ED144 /* Helpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Helpers.swift; sourceTree = "<group>"; };
D5ACAC991CCE439F00567809 /* Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
D5ACAC9C1CCE449A00567809 /* ExtensionsSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExtensionsSpec.swift; sourceTree = "<group>"; };
D5B2E89F1C3A780C00C0327D /* Brick.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Brick.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -206,6 +209,7 @@
children = (
D5C629971C3A8BDA007F7B7C /* VIewModelSpec.swift */,
D5ACAC9C1CCE449A00567809 /* ExtensionsSpec.swift */,
D57832961CE142C8005ED144 /* Helpers.swift */,
);
path = Shared;
sourceTree = "<group>";
Expand Down Expand Up @@ -482,6 +486,7 @@
buildActionMask = 2147483647;
files = (
D5ACAC9F1CCE45CE00567809 /* ExtensionsSpec.swift in Sources */,
D57832991CE142DE005ED144 /* Helpers.swift in Sources */,
D5C6299E1C3A8C75007F7B7C /* VIewModelSpec.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -502,6 +507,7 @@
buildActionMask = 2147483647;
files = (
D5ACACA01CCE45CF00567809 /* ExtensionsSpec.swift in Sources */,
D578329A1CE142DF005ED144 /* Helpers.swift in Sources */,
D5C6299C1C3A8BDA007F7B7C /* VIewModelSpec.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
14 changes: 14 additions & 0 deletions BrickTests/Shared/ExtensionsSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,19 @@ class ExtensionsSpec: QuickSpec {
}
}
}

describe("Mappable+Brick") {
let item = Meta(id: 11, name: "Name")

describe("#metaDictionary") {
it("returns an array of properties") {
var dictionary = ["id": 11, "name": "Name"]
var result = item.metaProperties

expect(result["id"] as? Int).to(equal(dictionary["id"]))
expect(result["name"] as? String).to(equal(dictionary["name"]))
}
}
}
}
}
15 changes: 15 additions & 0 deletions BrickTests/Shared/Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Tailor
import Sugar

struct Meta {
var id = 0
var name: String?
}

extension Meta: Mappable {

init(_ map: JSONDictionary) {
id <- map.property("id")
name <- map.property("name")
}
}
22 changes: 19 additions & 3 deletions BrickTests/Shared/VIewModelSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,28 @@ class ViewModelSpec: QuickSpec {
}

describe("#meta") {
beforeEach {
it("resolves meta data created from JSON") {
viewModel = ViewModel(data)
expect(viewModel.meta("domain", "")).to(equal(data["meta"]!["domain"]))
}

it("resolves meta data") {
expect(viewModel.meta("domain", "")).to(equal(data["meta"]!["domain"]))
it("resolves meta data created from object") {
var data = ["id": 11, "name": "Name"]

viewModel = ViewModel(meta: Meta(data))
expect(viewModel.meta("id", 0)).to(equal(data["id"]))
expect(viewModel.meta("name", "")).to(equal(data["name"]))
}
}

describe("#metaInstance") {
it("resolves meta data created from object") {
var data = ["id": 11, "name": "Name"]
viewModel = ViewModel(meta: Meta(data))
let result: Meta = viewModel.metaInstance()

expect(result.id).to(equal(data["id"]))
expect(result.name).to(equal(data["name"]))
}
}

Expand Down
28 changes: 28 additions & 0 deletions Sources/Shared/Extensions.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Tailor

// MARK: - Array

public extension _ArrayType where Generator.Element == ViewModel {
Expand Down Expand Up @@ -41,3 +43,29 @@ extension Dictionary where Key: StringLiteralConvertible {
}
}
}

// MARK: - Mappable

extension Mappable {

/**
- Returns: A key-value dictionary.
*/
var metaProperties: [String : AnyObject] {
var properties = [String : AnyObject]()

for tuple in Mirror(reflecting: self).children {
guard let key = tuple.label else { continue }

if let value = tuple.value as? AnyObject {
properties[key] = value
} else if let value = Mirror(reflecting: tuple.value).descendant("Some") as? AnyObject {
properties[key] = value
} else {
continue
}
}

return properties
}
}
21 changes: 21 additions & 0 deletions Sources/Shared/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ public struct ViewModel: Mappable {
self.relations = relations
}

/**
Initialization a new instance of a ViewModel and map it to a JSON dictionary

- Parameter title: The title string for the view model, defaults to empty string
- Parameter subtitle: The subtitle string for the view model, default to empty string
- Parameter image: Image name or URL as a string, default to empty string
*/
public init(title: String = "", subtitle: String = "", image: String = "", kind: StringConvertible = "", action: String? = nil, size: CGSize = CGSize(width: 0, height: 0), meta: Mappable, relations: [String : [ViewModel]] = [:]) {
self.init(title: title, subtitle: subtitle, image: image, kind: kind, action: action,
size: size, meta: meta.metaProperties, relations: relations)
}

// MARK: - Helpers

/**
Expand All @@ -162,6 +174,15 @@ public struct ViewModel: Mappable {
return meta[key] as? T
}

/**
A generic convenience method for resolving meta instance

- Returns: A generic meta instance based on `type`
*/
public func metaInstance<T: Mappable>() -> T {
return T(meta)
}

/**
A convenience lookup method for resolving view model relations

Expand Down