API 0.9.5
Foundation/CPPredicate/CPExpression_aggregate.j
Go to the documentation of this file.
00001 /*
00002  * CPExpression_aggregate.j
00003  *
00004  * Created by cacaodev.
00005  * Copyright 2010.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 
00023 @implementation CPExpression_aggregate : CPExpression
00024 {
00025     CPArray _aggregate;
00026 }
00027 
00028 - (id)initWithAggregate:(CPArray)collection
00029 {
00030     self = [super initWithExpressionType:CPAggregateExpressionType];
00031 
00032     if (self)
00033         _aggregate = collection;
00034     return self;
00035 }
00036 
00037 - (BOOL)isEqual:(id)object
00038 {
00039     if (self == object)
00040         return YES;
00041 
00042     if (object.isa != self.isa || [object expressionType] != [self expressionType] || ![[object collection] isEqual:[self collection]])
00043         return NO;
00044 
00045     return YES;
00046 }
00047 
00048 - (id)collection
00049 {
00050     return _aggregate;
00051 }
00052 
00053 - (id)expressionValueWithObject:(id)object context:(CPDictionary)context
00054 {
00055     var eval_array = [CPArray array],
00056         collection  = [_aggregate objectEnumerator],
00057         exp;
00058 
00059     while (exp = [collection nextObject])
00060     {
00061         var eval = [exp expressionValueWithObject:object context:context];
00062         [eval_array addObject:eval];
00063     }
00064 
00065     return eval_array;
00066 }
00067 
00068 - (CPString)description
00069 {
00070     var i = 0,
00071         count = [_aggregate count],
00072         result = "{";
00073 
00074     for (; i < count; i++)
00075         result = result + [CPString stringWithFormat:@"%s%s", [[_aggregate objectAtIndex:i] description], (i + 1 < count) ? @", " : @""];
00076 
00077     result = result + "}";
00078 
00079     return result;
00080 }
00081 
00082 - (CPExpression)_expressionWithSubstitutionVariables:(CPDictionary)variables
00083 {
00084     var subst_array = [CPArray array],
00085         count = [_aggregate count],
00086         i = 0;
00087 
00088     for (; i < count; i++)
00089         [subst_array addObject:[[_aggregate objectAtIndex:i] _expressionWithSubstitutionVariables:variables]];
00090 
00091     return [CPExpression expressionForAggregate:subst_array];
00092 }
00093 
00094 @end
00095 
00096 var CPCollectionKey = @"CPCollection";
00097 
00098 @implementation CPExpression_aggregate (CPCoding)
00099 
00100 - (id)initWithCoder:(CPCoder)coder
00101 {
00102     var collection = [coder decodeObjectForKey:CPCollectionKey];
00103     return [self initWithAggregate:collection];
00104 }
00105 
00106 - (void)encodeWithCoder:(CPCoder)coder
00107 {
00108     [coder encodeObject:_aggregate forKey:CPCollectionKey];
00109 }
00110 
00111 @end
 All Classes Files Functions Variables Defines