Files
cappuccino/Foundation/CPPredicate/CPExpression_variable.j
T
cacaodev 4b1fbf1eff Implemented subquery expressions.
Added missing getters in CPExpression.
Added a type: argument to FunctionExpression initializer used by  subclasses to init super with their own type.
Fixed evaluation with bindings dictionary in CPExpression_variable. Allow values in bindings to be a constant or an expression (per cocoa).

Tests: added tests for subqueryExpressions, for variableExpression evaluation where the value in the bindings dictionary can be either a constant or another expression.
Parsing a simple variable expression works (e.g. '$x = 12'), parsing a variable in a combined path (e.g.
'$x.path = 12' won't yet.
2010-10-29 18:58:29 +02:00

78 lines
1.7 KiB
Plaintext

@import "CPExpression.j"
@import <Foundation/CPString.j>
@import <Foundation/CPDictionary.j>
@implementation CPExpression_variable : CPExpression
{
CPString _variable;
}
- (id)initWithVariable:(CPString)variable
{
[super initWithExpressionType:CPVariableExpressionType];
_variable = [variable copy];
return self;
}
- (BOOL)isEqual:(id)object
{
if (self == object)
return YES;
if (object.isa != self.isa || [object expressionType] != [self expressionType] || ![[object variable] isEqualToString:[self variable]])
return NO;
return YES;
}
- (CPString)variable
{
return _variable;
}
- (id)expressionValueWithObject:object context:(CPDictionary)context
{
var expression = [self _expressionWithSubstitutionVariables:context];
return [expression expressionValueWithObject:object context:context];
}
- (CPString)description
{
return [CPString stringWithFormat:@"$%s", _variable];
}
- (CPExpression)_expressionWithSubstitutionVariables:(CPDictionary)variables
{
var value = [variables objectForKey:_variable];
if (value == nil)
[CPException raise:CPInvalidArgumentException reason:@"Can't get value for '" + _variable + "' in bindings" + variables];
if ([value isKindOfClass:[CPExpression class]])
return value;
return [CPExpression expressionForConstantValue:value];
}
@end
var CPVariableKey = @"CPVariable";
@implementation CPExpression_variable (CPCoding)
- (id)initWithCoder:(CPCoder)coder
{
var variable = [coder decodeObjectForKey:CPVariableKey];
return [self initWithVariable:variable];
}
- (void)encodeWithCoder:(CPCoder)coder
{
[coder encodeObject:_variable forKey:CPVariableKey];
}
@end