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

Convenience Methods #44

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Packages/
.DS_Store
*.xcodeproj

.idea
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class TodosController: ApplicationController {
// shared todo variable used to pass value between setTodo filter and actions
var todo: Todo?
override func controller() {

super.controller()
// sets before filter setTodo only for specified actions.
beforeAction("setTodo", only: ["show", "edit", "update", "destroy"])

Expand Down
39 changes: 39 additions & 0 deletions Sources/Swifton/Controller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,42 @@ public func respondTo(_ request: Request, _ responders: [String: () -> Response]
}
return Response(status: .notAcceptable)
}


public extension Controller {
enum REST: String {
case Index = "index", New = "new", Create = "create", Show = "show", Edit = "edit", Update = "update", Destroy = "destroy"
}

public func index(body: Action) {
action(.Index, body: body)
}

public func new(body: Action) {
action(.New, body: body)
}

public func create(body: Action) {
action(.Create, body: body)
}

public func show(body: Action) {
action(.Show, body: body)
}

public func edit(body: Action) {
action(.Edit, body: body)
}

public func update(body: Action) {
action(.Update, body: body)
}

public func destroy(body: Action) {
action(.Destroy, body: body)
}

private func action(_ type: REST, body: Action) {
action(type.rawValue, body: body)
}
}