Skip to content

Commit fd81ba9

Browse files
committed
The settings engine returns self for chaining now
But discardable. Added comments.
1 parent 447b46e commit fd81ba9

File tree

2 files changed

+112
-13
lines changed

2 files changed

+112
-13
lines changed

Sources/express/Express.swift

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,61 @@ open class Express: SettingsHolder, MountableMiddlewareObject, MiddlewareObject,
149149

150150
// MARK: - SettingsHolder
151151

152-
public func set(_ key: String, _ value: Any?) {
152+
/**
153+
* Sets or removes a configuration key in the settings store.
154+
*
155+
* Example:
156+
* ```swift
157+
* app.set("view engine", "html")
158+
* .set("views", __dirname() + "/views")
159+
* ```
160+
*
161+
* - Parameters:
162+
* - key: The name of the key, e.g. "view engine"
163+
* - value: The associated value, if `nil` is passed in, the value is
164+
* removed from the store.
165+
* - Returns: `self` for chaining.
166+
*/
167+
@discardableResult
168+
public func set(_ key: String, _ value: Any?) -> Self {
153169
if let v = value { settingsStore[key] = v }
154170
else { settingsStore.removeValue(forKey: key) }
171+
return self
155172
}
173+
174+
/**
175+
* Returns the value of a configuration key from the settings store.
176+
*
177+
* Example:
178+
* ```swift
179+
* let engine = app.get("view engine")
180+
* ```
181+
*
182+
* - Parameters:
183+
* - key: The name of the key, e.g. "view engine"
184+
* - Returns: The value in the store, or `nil` if missing.
185+
*/
156186
public func get(_ key: String) -> Any? {
157187
return settingsStore[key]
158188
}
159189

190+
160191
// MARK: - Engines
161192

162-
var engines = [ String : ExpressEngine]()
193+
var engines = [ String : ExpressEngine ]()
163194

164-
public func engine(_ key: String, _ engine: @escaping ExpressEngine) {
195+
/**
196+
* Sets an engine implementation.
197+
*
198+
* Example:
199+
* ```swift
200+
* app.engine("mustache", mustacheExpress)
201+
* ```
202+
*/
203+
@discardableResult
204+
public func engine(_ key: String, _ engine: @escaping ExpressEngine) -> Self {
165205
engines[key] = engine
206+
return self
166207
}
167208

168209

Sources/express/Settings.swift

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,89 @@
77
//
88

99
/**
10-
* Just a special kind of dictionary. The `Express` application class is
10+
* Just a special kind of dictionary. The ``Express`` application class is
1111
* currently the sole example.
1212
*
1313
* Examples:
14+
* ```swift
15+
* app.set("env", "production")
16+
* app.enable("x-powered-by")
1417
*
15-
* app.set("env", "production")
16-
* app.enable("x-powered-by")
17-
*
18-
* let env = app.settings.env
18+
* let env = app.settings.env
19+
* ```
1920
*/
2021
public protocol SettingsHolder {
2122

22-
func set(_ key: String, _ value: Any?)
23+
/**
24+
* Sets or removes a configuration key in the settings store.
25+
*
26+
* Example:
27+
* ```swift
28+
* app.set("view engine", "html")
29+
* .set("views", __dirname() + "/views")
30+
* .enable("x-powered-by")
31+
* ```
32+
*
33+
* - Parameters:
34+
* - key: The name of the key, e.g. "view engine"
35+
* - value: The associated value, if `nil` is passed in, the value is
36+
* removed from the store.
37+
* - Returns: `self` for chaining.
38+
*/
39+
@discardableResult
40+
func set(_ key: String, _ value: Any?) -> Self
41+
42+
/**
43+
* Returns the value of a configuration key from the settings store.
44+
*
45+
* Example:
46+
* ```swift
47+
* let engine = app.get("view engine")
48+
* ```
49+
*
50+
* - Parameters:
51+
* - key: The name of the key, e.g. "view engine"
52+
* - Returns: The value in the store, or `nil` if missing.
53+
*/
2354
func get(_ key: String) -> Any?
2455
}
2556

2657
public extension SettingsHolder {
2758

59+
/**
60+
* Set configuration key in the settings store to `true`.
61+
*
62+
* Example:
63+
* ```swift
64+
* app.enable("x-powered-by")
65+
* ```
66+
*
67+
* - Parameters:
68+
* - key: The name of the bool key, e.g. "view engine"
69+
* - Returns: `self` for chaining.
70+
*/
2871
@inlinable
29-
func enable(_ key: String) {
30-
set(key, true)
72+
@discardableResult
73+
func enable(_ key: String) -> Self {
74+
return set(key, true)
3175
}
76+
77+
/**
78+
* Set configuration key in the settings store to `false`.
79+
*
80+
* Example:
81+
* ```swift
82+
* app.disable("x-powered-by")
83+
* ```
84+
*
85+
* - Parameters:
86+
* - key: The name of the bool key, e.g. "view engine"
87+
* - Returns: `self` for chaining.
88+
*/
3289
@inlinable
33-
func disable(_ key: String) {
34-
set(key, false)
90+
@discardableResult
91+
func disable(_ key: String) -> Self {
92+
return set(key, false)
3593
}
3694

3795
@inlinable

0 commit comments

Comments
 (0)