Railsとflexの連携にWebOrbを使っていて気づいたことのメモ。
DBの属性として定義している項目などは、flex側で同じ名前のプロパティを用意しておけば自動的に受け取れる。では、メソッドの実行結果も渡したいときにどうすればよいか。チュートリアルを読んでもわからなかった。普通に実行すると、いちいち通信する必要がある。なので仕方なくソースを読んでみた。多分この辺が関係してそう。
(weborb/lib/writer/message_writer.rbの185行目付近)
def MessageWriter.write_active_record( data_object, formatter ) properties = data_object.attributes instance_variables = data_object.instance_variables instance_variables.each do |instance_variable| method_name = instance_variable.delete "@" next if(method_name == "attributes" or method_name == "new_record" or method_name == "new_record_before_save" or method_name == "errors" ) if data_object.respond_to?( method_name ) method = data_object.method method_name model_data = method.call properties.store( method_name, model_data ) else Log.debug( "active record object does not respond to the method: " + method_name ) if Log.debug? end end object_class = data_object.class attributes = object_class.get_attributes attributes.each do |attribute| attr_name = attribute.id2name attr_method = data_object.method attr_name attr_value = attr_method.call properties.store( attr_name, attr_value ) end class_name = data_object.class.name server_mappings = WebORBConfig.get_server_mappings resolved_name = server_mappings[class_name] if resolved_name.nil? MessageWriter.write_hash( properties, formatter ) else object_serializer = formatter.get_object_serializer object_serializer.write_object( resolved_name, properties, formatter ) end end end
インスタンス変数かアクセサとして定義してあれば、メソッドの実行結果が代入されるっぽい。以下のようにしてみたら、flex側でメソッドの実行結果を利用できた(discount_priceが実行結果を送付したいメソッド名)。もっとちゃんとした方法がありそうだけど。
require 'weborb/context' require 'rbconfig' class AMFProduct def getProduct(params) params = params.with_indifferent_access product = Product.find(params[:id]) product.instance_variable_set("@discount_price",nil) product end end