-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseProfile.swift
91 lines (78 loc) · 1.99 KB
/
BaseProfile.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// BaseProfile.swift
// liteterm
//
// Created by yjroot on 2015. 9. 10..
// Copyright (c) 2015년 Liteterm team. All rights reserved.
//
import Foundation
protocol BaseProfile: CustomStringConvertible {
var parent: BaseProfile { get set }
func getValue(keys: [String]) -> String?
func setValue(keys: [String], value: String)
}
extension BaseProfile {
subscript(key : String) -> ProfileSelector {
get {
return ProfileSelector(profile: self, keyList: [key])
}
}
}
class ProfileSelector {
var profile: BaseProfile
var list: [String]
init(profile: BaseProfile, keyList: [String]) {
self.profile = profile
self.list = keyList
}
init(selector: ProfileSelector, key: String) {
self.profile = selector.profile
self.list = selector.list + [key]
}
subscript(key : String) -> ProfileSelector {
get {
return ProfileSelector(selector: self, key: key)
}
}
var exist: Bool {
return profile.getValue(self.list) != nil
}
var value: String {
get {
var profile = self.profile
var result: String? = nil
while result == nil {
result = profile.getValue(self.list)
profile = profile.parent
}
return result!
}
set(newValue) {
self.profile.setValue(self.list, value: newValue)
}
}
var int: Int {
get {
return Int(self.value) ?? 0
}
set(newValue) {
self.value = String(newValue)
}
}
var string: String {
get {
return self.value
}
set(newValue) {
self.value = newValue
}
}
var bool: Bool {
get {
return self.value.lowercaseString == "true"
}
set(newValue) {
self.value = newValue ? "true" : "false"
}
}
}