-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWOValueAssociation.swift
63 lines (49 loc) · 1.67 KB
/
WOValueAssociation.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// WOValueAssociation.swift
// SwiftObjects
//
// Created by Helge Hess on 11.05.18.
// Copyright © 2018-2019 ZeeZide. All rights reserved.
//
/**
* Represents a constant value.
*
* For example:
*
* <wo:str value="Hello World" />
*
* Will result in a WOValueAssociation<String> with the constant value
* "Hello World".
*/
public class WOValueAssociation<Element> : WOAssociation, SmartDescription {
public let value : Element
public init(_ value: Element) {
self.value = value
}
public var isValueConstant : Bool { return true }
public var isValueSettable : Bool { return false }
public func value(in component: Any?) -> Any? {
return value
}
// MARK: - Description
public func appendToDescription(_ ms: inout String) {
ms.append(" value=\(value)")
}
}
public extension WOValueAssociation where Element == String {
func boolValue(in component: Any?) -> Bool { return UObject.boolValue(value) }
func intValue (in component: Any?) -> Int { return UObject.intValue (value) }
func stringValue(in component: Any?) -> String? { return value }
}
public extension WOValueAssociation where Element == Bool {
func boolValue(in component: Any?) -> Bool { return value }
func intValue (in component: Any?) -> Int { return value ? 1 : 0 }
func stringValue(in component: Any?) -> String? {
return value ? "true" : "false"
}
}
public extension WOValueAssociation where Element == Int {
func boolValue (in component: Any?) -> Bool { return value != 0 }
func intValue (in component: Any?) -> Int { return value }
func stringValue(in component: Any?) -> String? { return String(value) }
}