API 0.9.5
Foundation/CPPredicate/CPExpression_variable.j
Go to the documentation of this file.
00001 /*
00002  * CPExpression_variable.j
00003  *
00004  * Portions based on NSExpression_variable.m in Cocotron (http://www.cocotron.org/)
00005  * Copyright (c) 2006-2007 Christopher J. W. Lloyd
00006  *
00007  * Created by cacaodev.
00008  * Copyright 2010.
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00023  */
00024 
00025 
00026 @implementation CPExpression_variable :  CPExpression
00027 {
00028     CPString _variable;
00029 }
00030 
00031 - (id)initWithVariable:(CPString)variable
00032 {
00033     self = [super initWithExpressionType:CPVariableExpressionType];
00034 
00035     if (self)
00036     {
00037         _variable = [variable copy];
00038     }
00039     return self;
00040 }
00041 
00042 - (BOOL)isEqual:(id)object
00043 {
00044     if (self == object)
00045         return YES;
00046 
00047     if (object.isa != self.isa || [object expressionType] != [self expressionType] || ![[object variable] isEqualToString:[self variable]])
00048         return NO;
00049 
00050     return YES;
00051 }
00052 
00053 - (CPString)variable
00054 {
00055     return _variable;
00056 }
00057 
00058 - (id)expressionValueWithObject:object context:(CPDictionary)context
00059 {
00060     var expression = [self _expressionWithSubstitutionVariables:context];
00061 
00062     return [expression expressionValueWithObject:object context:context];
00063 }
00064 
00065 - (CPString)description
00066 {
00067     return [CPString stringWithFormat:@"$%s", _variable];
00068 }
00069 
00070 - (CPExpression)_expressionWithSubstitutionVariables:(CPDictionary)variables
00071 {
00072     var value = [variables objectForKey:_variable];
00073     if (value == nil)
00074         [CPException raise:CPInvalidArgumentException reason:@"Can't get value for '" + _variable + "' in bindings" + variables];
00075 
00076     if ([value isKindOfClass:[CPExpression class]])
00077         return value;
00078 
00079     return [CPExpression expressionForConstantValue:value];
00080 }
00081 
00082 @end
00083 
00084 var CPVariableKey = @"CPVariable";
00085 
00086 @implementation CPExpression_variable (CPCoding)
00087 
00088 - (id)initWithCoder:(CPCoder)coder
00089 {
00090     var variable = [coder decodeObjectForKey:CPVariableKey];
00091     return [self initWithVariable:variable];
00092 }
00093 
00094 - (void)encodeWithCoder:(CPCoder)coder
00095 {
00096     [coder encodeObject:_variable forKey:CPVariableKey];
00097 }
00098 
00099 @end
00100 
 All Classes Files Functions Variables Defines