Skip to content

Commit

Permalink
feat: Item now has published, created, and lastModified properties
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the `date` property has been renamed to `published`
  • Loading branch information
kevinrenskers committed Nov 19, 2024
1 parent ea16d98 commit 384e5ad
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Example/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"repositoryURL": "https://github.com/loopwerk/SagaSwimRenderer",
"state": {
"branch": null,
"revision": "f53481e5c9972b83ca2d16ca2b962c63d460e23f",
"version": "0.7.0"
"revision": "ee8c082934d1daf0a6819b8120170dd60760b9a9",
"version": "0.8.0"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Example/Sources/Example/run.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func itemProcessor(item: Item<ArticleMetadata>) async {
}

// Set the date
item.date = date
item.published = date

// And remove the first 11 characters from the filename
let first11 = String(item.relativeSource.lastComponentWithoutExtension.prefix(11))
Expand Down
13 changes: 9 additions & 4 deletions Sources/Saga/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public protocol AnyItem: AnyObject {
var title: String { get set }
var rawContent: String { get set }
var body: String { get set }
var date: Date { get set }
var published: Date { get set }
var created: Date { get set }
var lastModified: Date { get set }
var url: String { get }
}
Expand All @@ -41,21 +42,25 @@ public class Item<M: Metadata>: AnyItem {
public var body: String

/// The published date of the item.
public var date: Date
public var published: Date

/// The creation date of the item.
public var created: Date

/// The last modified date of the item.
public var lastModified: Date

/// The parsed metadata. ``Metadata`` can be any `Codable` object.
public var metadata: M

public init(relativeSource: Path, relativeDestination: Path, title: String, rawContent: String, body: String, date: Date, lastModified: Date, metadata: M) {
public init(relativeSource: Path, relativeDestination: Path, title: String, rawContent: String, body: String, published: Date, created: Date, lastModified: Date, metadata: M) {
self.relativeSource = relativeSource
self.relativeDestination = relativeDestination
self.title = title
self.rawContent = rawContent
self.body = body
self.date = date
self.published = published
self.created = created
self.lastModified = lastModified
self.metadata = metadata
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/Saga/Path+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import PathKit
import Foundation

public extension Path {
var creationDate: Date? {
return self.attributes[.creationDate] as? Date
}

var modificationDate: Date? {
return self.attributes[.modificationDate] as? Date
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Saga/ProcessingStep.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ internal class AnyProcessStep {
}
}

step.items = items.sorted(by: { left, right in left.date > right.date })
step.items = items.sorted(by: { left, right in left.published > right.published })
}

runWriters = {
let allItems = fileStorage
.compactMap(\.item)
.sorted(by: { left, right in left.date > right.date })
.sorted(by: { left, right in left.published > right.published })

for writer in step.writers {
try writer.run(step.items, allItems, outputPath, step.folder ?? "", fileIO)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Saga/Writer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public extension Writer {
var itemsPerYear = [Int: [Item<M>]]()

for item in items {
let year = item.date.year
let year = item.published.year
if var itemsArray = itemsPerYear[year] {
itemsArray.append(item)
itemsPerYear[year] = itemsArray
Expand Down
6 changes: 4 additions & 2 deletions Tests/SagaTests/SagaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ extension Reader {
title: "Test",
rawContent: "test",
body: "<p>\(relativeSource)</p>",
date: Date(timeIntervalSince1970: 1580598000),
published: Date(timeIntervalSince1970: 1580598000),
created: Date(timeIntervalSince1970: 1580598000),
lastModified: Date(timeIntervalSince1970: 1580598000),
metadata: metadata
)
Expand All @@ -34,7 +35,8 @@ extension Reader {
title: "Test",
rawContent: "test",
body: "<p>\(relativeSource)</p>",
date: Date(timeIntervalSince1970: 1612220400),
published: Date(timeIntervalSince1970: 1612220400),
created: Date(timeIntervalSince1970: 1612220400),
lastModified: Date(timeIntervalSince1970: 1612220400),
metadata: metadata
)
Expand Down

0 comments on commit 384e5ad

Please sign in to comment.