Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve/handling children #28

Merged
merged 6 commits into from
Dec 20, 2016
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
8 changes: 6 additions & 2 deletions Brick.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
BDBA5F221D940F09003A5C6B /* Brick.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BDFD54221D940EDC003CB98B /* Brick.framework */; };
BDDA5FEB1CBBCF45000FD5A6 /* ItemConfigurable.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDDA5FEA1CBBCF45000FD5A6 /* ItemConfigurable.swift */; };
BDDA5FEC1CBBCF45000FD5A6 /* ItemConfigurable.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDDA5FEA1CBBCF45000FD5A6 /* ItemConfigurable.swift */; };
BDDB1A541E09AF7300DFF834 /* DictionaryConvertable.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDDB1A531E09AF7300DFF834 /* DictionaryConvertable.swift */; };
BDE7D2171D940FC300027CE4 /* ItemSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5C629971C3A8BDA007F7B7C /* ItemSpec.swift */; };
BDE7D2181D940FC300027CE4 /* ExtensionsSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5ACAC9C1CCE449A00567809 /* ExtensionsSpec.swift */; };
BDE7D2191D940FC300027CE4 /* Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = D57832961CE142C8005ED144 /* Helpers.swift */; };
Expand Down Expand Up @@ -85,6 +86,7 @@
BD80D9201DA2AACA00675D5F /* Indexable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Indexable.swift; sourceTree = "<group>"; };
BDBA5F1D1D940F09003A5C6B /* Brick-tvOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Brick-tvOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
BDDA5FEA1CBBCF45000FD5A6 /* ItemConfigurable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ItemConfigurable.swift; sourceTree = "<group>"; };
BDDB1A531E09AF7300DFF834 /* DictionaryConvertable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DictionaryConvertable.swift; sourceTree = "<group>"; };
BDE7D2151D940F7800027CE4 /* Info-tvOS.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Info-tvOS.plist"; sourceTree = "<group>"; };
BDE7D2161D940FAA00027CE4 /* Info-tvOS.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Info-tvOS.plist"; sourceTree = "<group>"; };
BDFD54221D940EDC003CB98B /* Brick.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Brick.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -218,11 +220,12 @@
D5C6296E1C3A809D007F7B7C /* Shared */ = {
isa = PBXGroup;
children = (
BDDB1A531E09AF7300DFF834 /* DictionaryConvertable.swift */,
D5ACAC991CCE439F00567809 /* Extensions.swift */,
BD80D9201DA2AACA00675D5F /* Indexable.swift */,
D5C629861C3A89A8007F7B7C /* Item.swift */,
BDDA5FEA1CBBCF45000FD5A6 /* ItemConfigurable.swift */,
DAA38AA41CBEEAD900D48603 /* StringConvertible.swift */,
D5ACAC991CCE439F00567809 /* Extensions.swift */,
BD80D9201DA2AACA00675D5F /* Indexable.swift */,
);
path = Shared;
sourceTree = "<group>";
Expand Down Expand Up @@ -647,6 +650,7 @@
buildActionMask = 2147483647;
files = (
D5C629871C3A89A8007F7B7C /* Item.swift in Sources */,
BDDB1A541E09AF7300DFF834 /* DictionaryConvertable.swift in Sources */,
D5ACAC9A1CCE439F00567809 /* Extensions.swift in Sources */,
BD80D9211DA2AACA00675D5F /* Indexable.swift in Sources */,
BDDA5FEB1CBBCF45000FD5A6 /* ItemConfigurable.swift in Sources */,
Expand Down
21 changes: 21 additions & 0 deletions BrickTests/Shared/ItemSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class ItemSpec: QuickSpec {
"kind" : faker.team.name(),
"size" : ["width" : 320.0, "height" : 240.0],
"action" : faker.internet.ipV6Address(),
"children" : [
"child 1" : "Anna",
"child 2" : "Elsa"
],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bonus points for Frozen reference? 😁

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

"meta" : [
"domain" : faker.internet.domainName()
]
Expand Down Expand Up @@ -185,6 +189,23 @@ class ItemSpec: QuickSpec {
}
}

describe("#compareChildren") {
beforeEach {
item = Item(data)
}

it("compare children properly") {
let sameItem = Item(data)

var newData: [String : Any] = data!
newData["children"] = [["child 1" : "Anna"]]
let otherItem = Item(newData)

expect(item === sameItem).to(beTrue())
expect(item === otherItem).to(beFalse())
}
}

describe("#compareRelations") {
beforeEach {
data["relations"] = ["Items" : [data, data, data]]
Expand Down
6 changes: 6 additions & 0 deletions Sources/Shared/DictionaryConvertable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
A protocol for returning a dictionary representation of self
*/
public protocol DictionaryConvertible {
var dictionary: [String : Any] { get }
}
5 changes: 4 additions & 1 deletion Sources/Shared/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Tailor
/**
A value type struct, it conforms to the Mappable protocol so that it can be instantiated with JSON
*/
public struct Item: Mappable, Indexable {
public struct Item: Mappable, Indexable, DictionaryConvertible {

/**
An enum with all the string keys used in the view model
Expand Down Expand Up @@ -157,6 +157,7 @@ public struct Item: Mappable, Indexable {
action: String? = nil,
size: CGSize = CGSize(width: 0, height: 0),
meta: [String : Any] = [:],
children: [DictionaryConvertible] = [],
relations: [String : [Item]] = [:]) {
self.identifier = identifier
self.title = title
Expand All @@ -167,6 +168,7 @@ public struct Item: Mappable, Indexable {
self.action = action
self.size = size
self.meta = meta
self.children = children.map { $0.dictionary }
self.relations = relations
}

Expand Down Expand Up @@ -333,6 +335,7 @@ public func ===(lhs: Item, rhs: Item) -> Bool {
lhs.kind == rhs.kind &&
lhs.action == rhs.action &&
lhs.size == rhs.size &&
(lhs.children as NSArray).isEqual(to: rhs.children) &&
(lhs.meta as NSDictionary).isEqual(to: rhs.meta) &&
compareRelations(lhs, rhs)

Expand Down