]> err.no Git - sope/blob - sope-core/NGExtensions/NGRuleEngine.subproj/NGRuleAssignment.m
bumped framework versions
[sope] / sope-core / NGExtensions / NGRuleEngine.subproj / NGRuleAssignment.m
1 /*
2   Copyright (C) 2003-2004 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "NGRuleAssignment.h"
23 #include "common.h"
24
25 @implementation NGRuleAssignment
26
27 + (id)assignmentWithKeyPath:(NSString *)_kp value:(id)_value {
28   return [[[self alloc] initWithKeyPath:_kp value:_value] autorelease];
29 }
30 - (id)initWithKeyPath:(NSString *)_kp value:(id)_value {
31   if ((self = [super init])) {
32     self->keyPath = [_kp copy];
33     self->value   = [_value retain];
34   }
35   return self;
36 }
37 - (id)init {
38   return [self initWithKeyPath:nil value:nil];
39 }
40
41 - (void)dealloc {
42   [self->keyPath release];
43   [self->value   release];
44   [super dealloc];
45 }
46
47 /* accessors */
48
49 - (void)setKeyPath:(NSString *)_kp {
50   ASSIGNCOPY(self->keyPath, _kp);
51 }
52 - (NSString *)keyPath {
53   return self->keyPath;
54 }
55
56 - (void)setValue:(id)_value {
57   ASSIGN(self->value, _value);
58 }
59 - (id)value {
60   return self->value;
61 }
62
63 /* operations */
64
65 - (BOOL)isCandidateForKey:(NSString *)_key {
66   if (_key == nil) return YES;
67   
68   // TODO: perform a real keypath check
69   return [self->keyPath isEqualToString:_key];
70 }
71
72 - (id)fireInContext:(id)_ctx {
73   // TODO: shouldn't we apply the value on ctx ?
74   return self->value;
75 }
76
77 /* key/value archiving */
78
79 - (id)initWithKeyValueUnarchiver:(EOKeyValueUnarchiver *)_unarchiver {
80   return [self initWithKeyPath:[_unarchiver decodeObjectForKey:@"keyPath"]
81                value:[_unarchiver decodeObjectForKey:@"value"]];
82 }
83 - (void)encodeWithKeyValueArchiver:(EOKeyValueArchiver *)_archiver {
84   [_archiver encodeObject:[self keyPath] forKey:@"keyPath"];
85   [_archiver encodeObject:[self value]   forKey:@"value"];
86 }
87
88 /* description */
89
90 - (NSString *)valueStringValue {
91   return [self->value isKindOfClass:[NSNumber class]]
92     ? [self->value stringValue]
93     : [NSString stringWithFormat:@"\"%@\"", self->value];
94 }
95
96 - (NSString *)stringValue {
97   return [NSString stringWithFormat:@"%@ = %@",
98                      [self keyPath],
99                      [self valueStringValue]];
100 }
101
102 - (NSString *)description {
103   return [self stringValue];
104 }
105
106 @end /* NGRuleAssignment */
107
108 @implementation NGRuleKeyAssignment
109
110 /* operations */
111
112 - (id)fireInContext:(id)_ctx {
113   // TODO: shouldn't we apply the value on ctx ?
114   return [_ctx valueForKeyPath:[[self value] stringValue]];
115 }
116
117 /* description */
118
119 - (NSString *)valueStringValue {
120   return [self->value stringValue];
121 }
122
123 @end /* NGRuleKeyAssignment */