mapping question

0 views
Skip to first unread message

roobnoob

unread,
Feb 20, 2008, 9:26:29 PM2/20/08
to rubyamf
Hi, i'm starting to use rubyamf, and if i have a ruby object that i
want to map that is NOT an ActiveRecord object, how would i do that?

Eg, the following works fine:

ClassMappings.register(
:actionscript => 'com.pomodo.vo.AccountVO',
:ruby => 'Account',
:type => 'active_record',
:attributes => ['id', 'email', 'birthdate', 'gender'] )

Account is an ActiveRecord class found in the 'models' directory. The
following does not work:

ClassMappings.register(
:actionscript => 'com.pomodo.vo.ServiceErrorVO',
:ruby => 'ServiceError',
:type => 'custom',
:attributes => ['code', 'message' ] )

ServiceError is a ruby class in the models directory, but it does not
inherit from ActiveRecord. I noticed a comment in the config file
saying one must specify the "classpath" for non AR classes, but not
sure what that would be (i tried 'models.ServiceError') and that
didn't work.

What happens is that the flex side recognizes the incoming amf as
com.pomodo.vo.AccountVO in the first case, and in the 2nd it just
thinks it's an Object:

(mx.messaging.messages::AcknowledgeMessage)#0
body = (Object)#1
code = 1
message = "not found"
clientId = "3c2621bb-1fcd-7c05-ee27-3974ade2a15b"
correlationId = "B88E6DD5-9C17-5B8C-CBFE-39C341334A87"
destination = (null)
headers = (Object)#2
messageId = "5cf69c75-ff77-ae4d-27c5-a0bb8ae8aa10"
timestamp = NaN
timeToLive = 0


A 2nd question has to do with error handling. Eg, i hit the server,
the controller does something, and finds that there's a problem. I
want to return some sort of structured object containing the error
info:

if all_went_well
render :amf => obj_to_return
else
render :amf => ServiceError.new(SYSTEM_ERROR, "system error")
end

In the success case i return the object the client asked for, but in
the error case i want to return a different object. What's the best
practice for handling errors?

Thanks.

aaron smith

unread,
Feb 20, 2008, 10:02:47 PM2/20/08
to rub...@googlegroups.com
so you're just wondering about handling errors, not neccessarily mapping non AR objects?

for error handling, there is an object called FaultObject, when used it triggers your fault handler in flash. You want to use this instead because of the fact that it triggers fault handlers.

so you'd do something like;

render :amf => FaultObject.new(1, "my error");  //1 is any code you want

and in flash i believe it comes in faultCode and faultMessage / faultDetails. I can't remember off hand, but that's pretty much what you want.

and just in case you want to map non-ar objects, you need to require the file in the rubyamf init.rb file (in the rubyamf plugins dir). So that the class is in memory and available at runtime. So your service error class mapping is correct, the class just needs to be required in that init file.

-Aaron

roobnoob

unread,
Feb 21, 2008, 7:58:15 PM2/21/08
to rubyamf


Thanks for the response. Comments inline..

On Feb 20, 7:02 pm, "aaron smith" <beingthexempl...@gmail.com> wrote:
> so you're just wondering about handling errors, not neccessarily mapping non
> AR objects?

I was asking about both...
>
> for error handling, there is an object called FaultObject, when used it
> triggers your fault handler in flash. You want to use this instead because
> of the fact that it triggers fault handlers.
>
> so you'd do something like;
>
> render :amf => FaultObject.new(1, "my error"); //1 is any code you want
>
> and in flash i believe it comes in faultCode and faultMessage /
> faultDetails. I can't remember off hand, but that's pretty much what you
> want.

Ok, i tried this, but i get back this monster object which includes a
stack trace. This stack trace also appears on the server-side which i
don't want. Also, it has a particular structure; i want to define my
own error object, not something pre-defined for me. Basically i want
to define my own class (as in the ServiceError example above) and
return that. It looks like i'll have to distinguish between the two
(or create a wrapper class) in the result handler.
>
> and just in case you want to map non-ar objects, you need to require the
> file in the rubyamf init.rb file (in the rubyamf plugins dir). So that the
> class is in memory and available at runtime. So your service error class
> mapping is correct, the class just needs to be required in that init file.

I do want to map non-AR objects, but adding it to init.rb did not
help, i was still getting the below:

From GetAccount result: (mx.messaging.messages::AcknowledgeMessage)#0
body = (Object)#1
code = 101
message = "object not found"
clientId = "dfa4d449-517e-0556-ee1c-d3d9f7e6b3d7"
correlationId = "9E2230A5-D462-2FA4-AB30-3DAFEA8E1A79"
destination = (null)
headers = (Object)#2
messageId = "eec4ce23-d61d-9f09-c52c-3397b0ebd1cf"
timestamp = NaN
timeToLive = 0

In other words, getting "Object" back instead of
com.pomodo.vo.ServiceErrorVO. HOWEVER, the client code wasn't actually
instantiating a ServiceErrorVO since i was just testing (ie, dumping
the contents of ResultEvent.message). When i added some client code
that instantiated a ServiceErrorVO from the result object, then it was
fine. Note that this worked fine w/o any require statement in init.rb.
Freaky.

Eg, this is my function that receives the result:

private function handleGetAccountResult(e:ResultEvent): void {
Debug.log( e.message ); // this prints out the above

// So if the below is commented out, then the above
says that i have an "Object" back. But if it's not, then i properly
get a "ServiceErrorVO" object.
//var error: ServiceErrorVO = ServiceErrorVO(e.result);
...
}

Reply all
Reply to author
Forward
0 new messages