/*
Copyright (c) 2009 Yahoo! Inc. All rights reserved.
The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
*/
package com.yahoo.astra.utils {
import com.yahoo.astra.utils.IValueParser;
import flash.text.TextField;
/**
* ValueParser is a helper class that provides easy data collection from an object. It stores the source
object and property
to be collected, and return the actual value of it.
* ValueParser
stores source
and property
of target data object,
* and calling the function(setValue
) when the target object is ready to be collected data.
*
* @example The following code shows a use of ValueParser
and FlValueParser
:
*
source
and property
of data. Returns getValue
function as object.
* See the use of ValueParser
in FormDataManager
.
*
* @return Function getValue
function.
*
* @param source Object contained data.
* @param property Property of the source object. If source is TextField, property
will be set as "text" be default.
*/
public function setValue(source : Object = null, property : Object = null) : Function {
this.source = source;
this.property = property;
return getValue;
}
/**
* Returns actual value set from setValue
.
*
* @return Object Data collected out of source
*/
public function getValue() : Object {
if(!this.source) return null;
// if the source is Array, will return the result as a string.
if(this.source is Array) {
var curReturnedStr : Object = "";
var sourceLength : int = source.length;
for (var i : int = 0;i < sourceLength; i++) {
var curSource : Object = this.source[i];
var curProperty : Object = (this.property) ? ((this.property is Array) ? this.property[i] : this.property ) : null;
var result : Object = objectValue(curSource, curProperty);
curReturnedStr += (result) ? result.toString() : "";
}
return curReturnedStr;
}
return objectValue(this.source, this.property);
return this.source;
}
/**
* @private
*
* If source is TextField, property will be set as "text".
*/
private function objectValue(source : Object = null, property : Object = null) : Object {
var returnedvalue : Object = source;
if(source is TextField) {
var textInput : TextField = source as TextField;
if(!property) property = "text";
returnedvalue = textInput[property];
} else {
if(source && property) returnedvalue = source[property];
}
return returnedvalue;
}
}
}