Skip to content

Commit

Permalink
The settings engine returns self for chaining now
Browse files Browse the repository at this point in the history
But discardable.
Added comments.
  • Loading branch information
helje5 committed Dec 14, 2024
1 parent 447b46e commit fd81ba9
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 13 deletions.
47 changes: 44 additions & 3 deletions Sources/express/Express.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,61 @@ open class Express: SettingsHolder, MountableMiddlewareObject, MiddlewareObject,

// MARK: - SettingsHolder

public func set(_ key: String, _ value: Any?) {
/**
* Sets or removes a configuration key in the settings store.
*
* Example:
* ```swift
* app.set("view engine", "html")
* .set("views", __dirname() + "/views")
* ```
*
* - Parameters:
* - key: The name of the key, e.g. "view engine"
* - value: The associated value, if `nil` is passed in, the value is
* removed from the store.
* - Returns: `self` for chaining.
*/
@discardableResult
public func set(_ key: String, _ value: Any?) -> Self {
if let v = value { settingsStore[key] = v }
else { settingsStore.removeValue(forKey: key) }
return self
}

/**
* Returns the value of a configuration key from the settings store.
*
* Example:
* ```swift
* let engine = app.get("view engine")
* ```
*
* - Parameters:
* - key: The name of the key, e.g. "view engine"
* - Returns: The value in the store, or `nil` if missing.
*/
public func get(_ key: String) -> Any? {
return settingsStore[key]
}


// MARK: - Engines

var engines = [ String : ExpressEngine]()
var engines = [ String : ExpressEngine ]()

public func engine(_ key: String, _ engine: @escaping ExpressEngine) {
/**
* Sets an engine implementation.
*
* Example:
* ```swift
* app.engine("mustache", mustacheExpress)
* ```
*/
@discardableResult
public func engine(_ key: String, _ engine: @escaping ExpressEngine) -> Self {
engines[key] = engine
return self
}


Expand Down
78 changes: 68 additions & 10 deletions Sources/express/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,89 @@
//

/**
* Just a special kind of dictionary. The `Express` application class is
* Just a special kind of dictionary. The ``Express`` application class is
* currently the sole example.
*
* Examples:
* ```swift
* app.set("env", "production")
* app.enable("x-powered-by")
*
* app.set("env", "production")
* app.enable("x-powered-by")
*
* let env = app.settings.env
* let env = app.settings.env
* ```
*/
public protocol SettingsHolder {

func set(_ key: String, _ value: Any?)
/**
* Sets or removes a configuration key in the settings store.
*
* Example:
* ```swift
* app.set("view engine", "html")
* .set("views", __dirname() + "/views")
* .enable("x-powered-by")
* ```
*
* - Parameters:
* - key: The name of the key, e.g. "view engine"
* - value: The associated value, if `nil` is passed in, the value is
* removed from the store.
* - Returns: `self` for chaining.
*/
@discardableResult
func set(_ key: String, _ value: Any?) -> Self

/**
* Returns the value of a configuration key from the settings store.
*
* Example:
* ```swift
* let engine = app.get("view engine")
* ```
*
* - Parameters:
* - key: The name of the key, e.g. "view engine"
* - Returns: The value in the store, or `nil` if missing.
*/
func get(_ key: String) -> Any?
}

public extension SettingsHolder {

/**
* Set configuration key in the settings store to `true`.
*
* Example:
* ```swift
* app.enable("x-powered-by")
* ```
*
* - Parameters:
* - key: The name of the bool key, e.g. "view engine"
* - Returns: `self` for chaining.
*/
@inlinable
func enable(_ key: String) {
set(key, true)
@discardableResult
func enable(_ key: String) -> Self {
return set(key, true)
}

/**
* Set configuration key in the settings store to `false`.
*
* Example:
* ```swift
* app.disable("x-powered-by")
* ```
*
* - Parameters:
* - key: The name of the bool key, e.g. "view engine"
* - Returns: `self` for chaining.
*/
@inlinable
func disable(_ key: String) {
set(key, false)
@discardableResult
func disable(_ key: String) -> Self {
return set(key, false)
}

@inlinable
Expand Down

0 comments on commit fd81ba9

Please sign in to comment.