Files
cappuccino/Foundation/_CPCollectionKVCOperators.j
T
Aparajita Fishman abaa1e81f2 Support for multiple-value bindings
- CPView and subclasses support multiple-value hidden bindings.
- CPControl and subclasses support multiple-value enabled bindings.
- CPWindow and CPBox support multiple-value title with pattern bindings.
- CPButton supports multiple argument + target bindings.
- CPImageView and CPTextField support multiple-value editable bindings.
- CPMenuItem supports multiple-value enabled bindings.
- Fixed bugs in CPObjectController with simple collection operators.
- CPColorWell uses black as the placeholder color.
- Runtime object attributes from a cib are applied as they are read, not deferred.
- NSNumberFormatter now reads the number style from the xib.
- Normalized some parameter names.
- Formatting.
- Test app for all binding types.
2013-02-15 12:21:13 -05:00

129 lines
3.5 KiB
Plaintext

/*
* _CPCollectionKVCOperators.j
* Foundation
*
* Created by Aparajita Fishman.
* Copyright 2012 Cappuccino Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import "CPObject.j"
var _CPCollectionKVCOperatorSimpleRE = /^@(avg|count|m(ax|in)|sum)(\.|$)/;
@implementation _CPCollectionKVCOperator : CPObject
+ (BOOL)isSimpleCollectionOperator:(CPString)operator
{
return _CPCollectionKVCOperatorSimpleRE.test(operator);
}
+ (id)performOperation:(CPString)operator withCollection:(id)aCollection propertyPath:(CPString)propertyPath
{
var selector = CPSelectorFromString(operator + @"ForCollection:propertyPath:");
if (![self respondsToSelector:selector])
return [aCollection valueForUndefinedKey:@"@" + operator];
return [self performSelector:selector withObject:aCollection withObject:propertyPath];
}
+ (double)avgForCollection:(id)aCollection propertyPath:(CPString)propertyPath
{
if (!propertyPath)
return [aCollection valueForUndefinedKey:@"@avg"];
var objects = [aCollection valueForKeyPath:propertyPath],
average = 0.0,
enumerator = [objects objectEnumerator],
object;
while ((object = [enumerator nextObject]) !== nil)
average += [object doubleValue];
return average / [objects count];
}
+ (double)minForCollection:(id)aCollection propertyPath:(CPString)propertyPath
{
if (!propertyPath)
return [aCollection valueForUndefinedKey:@"@min"];
var objects = [aCollection valueForKeyPath:propertyPath];
if ([objects count] === 0)
return nil;
var enumerator = [objects objectEnumerator],
min = [enumerator nextObject],
object;
while ((object = [enumerator nextObject]) !== nil)
{
if ([min compare:object] > 0)
min = object;
}
return min;
}
+ (double)maxForCollection:(id)aCollection propertyPath:(CPString)propertyPath
{
if (!propertyPath)
return [aCollection valueForUndefinedKey:@"@max"];
var objects = [aCollection valueForKeyPath:propertyPath];
if ([objects count] === 0)
return nil;
var enumerator = [objects objectEnumerator],
max = [enumerator nextObject],
object;
while ((object = [enumerator nextObject]) !== nil)
{
if ([max compare:object] < 0)
max = object;
}
return max;
}
+ (double)sumForCollection:(id)aCollection propertyPath:(CPString)propertyPath
{
if (!propertyPath)
return [aCollection valueForUndefinedKey:@"@sum"];
var objects = [aCollection valueForKeyPath:propertyPath],
sum = 0.0,
enumerator = [objects objectEnumerator],
object;
while ((object = [enumerator nextObject]) !== nil)
sum += [object doubleValue];
return sum;
}
+ (int)countForCollection:(id)aCollection propertyPath:(CPString)propertyPath
{
return [aCollection count];
}
@end