forked from couchbase/couchbase-lite-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CBLFragment.m
159 lines (136 loc) · 3.58 KB
/
CBLFragment.m
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// CBLFragment.m
// CouchbaseLite
//
// Copyright (c) 2017 Couchbase, Inc All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "CBLFragment.h"
#import "CBLDocument+Internal.h"
@implementation CBLFragment
- /* internal */ (instancetype) initWithParent: (id)parent key: (NSString*)parentKey {
self = [super init];
if (self) {
_parent = parent;
_key = parentKey;
}
return self;
}
- /* internal */ (instancetype) initWithParent: (id)parent index: (NSUInteger)parentIndex {
self = [super init];
if (self) {
_parent = parent;
_index = parentIndex;
}
return self;
}
#pragma mark - GET
- (NSObject*) value {
if (_key)
return [_parent valueForKey: _key];
else
return [_parent valueAtIndex: _index];
}
- (NSString*) string {
if (_key)
return [_parent stringForKey: _key];
else
return [_parent stringAtIndex: _index];
}
- (NSNumber*) number {
if (_key)
return [_parent numberForKey: _key];
else
return [_parent numberAtIndex: _index];
}
- (NSInteger) integerValue {
if (_key)
return [_parent integerForKey: _key];
else
return [_parent integerAtIndex: _index];
}
- (long long) longLongValue {
if (_key)
return [_parent longLongForKey: _key];
else
return [_parent longLongAtIndex: _index];
}
- (float) floatValue {
if (_key)
return [_parent floatForKey: _key];
else
return [_parent floatAtIndex: _index];
}
- (double) doubleValue {
if (_key)
return [_parent doubleForKey: _key];
else
return [_parent doubleAtIndex: _index];
}
- (BOOL) booleanValue {
if (_key)
return [_parent booleanForKey: _key];
else
return [_parent booleanAtIndex: _index];
}
- (NSDate*) date {
if (_key)
return [_parent dateForKey: _key];
else
return [_parent dateAtIndex: _index];
}
- (CBLBlob*) blob {
if (_key)
return [_parent blobForKey: _key];
else
return [_parent blobAtIndex: _index];
}
- (CBLArray*) array {
if (_key)
return [(CBLDictionary*)_parent arrayForKey: _key];
else
return [(CBLArray*)_parent arrayAtIndex: _index];
}
- (CBLDictionary*) dictionary {
if (_key)
return [(CBLDictionary*)_parent dictionaryForKey: _key];
else
return [(CBLArray*)_parent dictionaryAtIndex: _index];
}
#pragma mark - EXISTENCE
- (BOOL) exists {
return self.value != nil;
}
#pragma mark SUBSCRIPTING
- (CBLFragment*) objectForKeyedSubscript: (NSString*)key {
NSParameterAssert(key);
id value = self.value;
if (![value respondsToSelector: @selector(objectForKeyedSubscript:)])
return nil;
_parent = value;
_key = key;
return self;
}
- (CBLFragment*) objectAtIndexedSubscript: (NSUInteger)index {
id value = self.value;
if (![value respondsToSelector: @selector(objectAtIndexedSubscript:)])
return nil;
if (index >= [(CBLArray*)value count])
return nil;
_parent = value;
_index = index;
_key = nil;
return self;
}
@end