class RubySol::ClassMapping

Handles class name mapping between actionscript and ruby and assists in serializing and deserializing data between them. Simply map an AS class to a ruby class and when the object is (de)serialized it will end up as the appropriate class.

Example:

RubySol::ClassMapper.define do |m|
  m.map :as => 'AsClass', :ruby => 'RubyClass'
  m.map :as => 'vo.User', :ruby => 'Model::User'
end

Object Population/Serialization

In addition to handling class name mapping, it also provides helper methods for populating ruby objects from AMF and extracting properties from ruby objects for serialization. Support for hash-like objects and objects using attr_accessor for properties is currently built in, but custom classes may require subclassing the class mapper to add support.

Complete Replacement

In some cases, it may be beneficial to replace the default provider of class mapping completely. In this case, simply assign your class mapper class to RubySol::ClassMapper after loading RubySol. Through the magic of const_missing, ClassMapper is only defined after the first access by default, so you get no annoying warning messages. Custom class mappers must implement the following methods on instances: use_array_collection, get_as_class_name, get_ruby_obj, populate_ruby_obj, and props_for_serialization. In addition, it should have a class level mappings method that returns the mapping set it’s using, although its not required. If you’d like to see an example of what complete replacement offers, check out RubyAMF (github.com/rubyamf/rubyamf).

Example:

require 'rubygems'
require 'ruby_sol'

RubySol::ClassMapper = MyCustomClassMapper
# No warning about already initialized constant ClassMapper
RubySol::ClassMapper # MyCustomClassMapper

C ClassMapper

The C class mapper, RubySol::Ext::FastClassMapping, has the same public API that RubyAMF::ClassMapping does, but has some additional performance optimizations that may interfere with the proper serialization of objects. To reduce the cost of processing public methods for every object, its implementation of props_for_serialization caches valid properties by class, using the class as the hash key for property lookup. This means that adding and removing properties from instances while serializing using a given class mapper instance will result in the changes not being detected. As such, it’s not enabled by default. So long as you aren’t planning on modifying classes during serialization using encode_amf, the faster C class mapper should be perfectly safe to use.

Activating the C Class Mapper:

require 'rubygems'
require 'ruby_sol'
RubySol::ClassMapper = RubySol::Ext::FastClassMapping

Attributes

use_array_collection[RW]

Global configuration variable for sending Arrays as ArrayCollections. Defaults to false.

use_array_collection[R]

Public Class Methods

define() { |mapping_set| ... } click to toggle source

Define class mappings in the block. Block is passed a MappingSet object as the first parameter.

Example:

RubySol::ClassMapper.define do |m|
  m.map :as => 'AsClass', :ruby => 'RubyClass'
end
    # File lib/ruby_sol/class_mapping.rb
150 def define &block #:yields: mapping_set
151   yield mappings
152 end
mappings() click to toggle source

Returns the mapping set with all the class mappings that is currently being used.

    # File lib/ruby_sol/class_mapping.rb
138 def mappings
139   @mappings ||= MappingSet.new
140 end
new() click to toggle source

Copies configuration from class level configs to populate object

    # File lib/ruby_sol/class_mapping.rb
165 def initialize
166   @mappings = self.class.mappings
167   @use_array_collection = self.class.use_array_collection === true
168 end
reset() click to toggle source

Reset all class mappings except the defaults and return use_array_collection to false

    # File lib/ruby_sol/class_mapping.rb
156 def reset
157   @use_array_collection = false
158   @mappings = nil
159 end

Public Instance Methods

get_as_class_name(obj) click to toggle source

Returns the ActionScript class name for the given ruby object. Will also take a string containing the ruby class name.

    # File lib/ruby_sol/class_mapping.rb
172 def get_as_class_name obj
173   # Get class name
174   if obj.is_a?(String)
175     ruby_class_name = obj
176   elsif obj.is_a?(Values::TypedHash)
177     ruby_class_name = obj.type
178   elsif obj.is_a?(Hash)
179     return nil
180   else
181     ruby_class_name = obj.class.name
182   end
183 
184   # Get mapped AS class name
185   @mappings.get_as_class_name ruby_class_name
186 end
get_ruby_obj(as_class_name) click to toggle source

Instantiates a ruby object using the mapping configuration based on the source ActionScript class name. If there is no mapping defined, it returns a RubySol::Values::TypedHash with the serialized class name.

    # File lib/ruby_sol/class_mapping.rb
191 def get_ruby_obj as_class_name
192   ruby_class_name = @mappings.get_ruby_class_name as_class_name
193   if ruby_class_name.nil?
194     # Populate a simple hash, since no mapping
195     return Values::TypedHash.new(as_class_name)
196   else
197     ruby_class = ruby_class_name.split('::').inject(Kernel) {|scope, const_name| scope.const_get(const_name)}
198     return ruby_class.new
199   end
200 end
populate_ruby_obj(obj, props, dynamic_props=nil) click to toggle source

Populates the ruby object using the given properties. props and dynamic_props will be hashes with symbols for keys.

    # File lib/ruby_sol/class_mapping.rb
204 def populate_ruby_obj obj, props, dynamic_props=nil
205   props.merge! dynamic_props if dynamic_props
206 
207   # Don't even bother checking if it responds to setter methods if it's a TypedHash
208   if obj.is_a?(Values::TypedHash)
209     obj.merge! props
210     return obj
211   end
212 
213   # Some type of object
214   hash_like = obj.respond_to?("[]=")
215   props.each do |key, value|
216     if obj.respond_to?("#{key}=")
217       obj.send("#{key}=", value)
218     elsif hash_like
219       obj[key] = value
220     end
221   end
222   obj
223 end
props_for_serialization(ruby_obj) click to toggle source

Extracts all exportable properties from the given ruby object and returns them in a hash. If overriding, make sure to return a hash wth string keys unless you are only going to be using the native C extensions, as the pure ruby serializer performs a sort on the keys to acheive consistent, testable results.

    # File lib/ruby_sol/class_mapping.rb
230 def props_for_serialization ruby_obj
231   # Handle hashes
232   if ruby_obj.is_a?(Hash)
233     # Stringify keys to make it easier later on and allow sorting
234     h = {}
235     ruby_obj.each {|k,v| h[k.to_s] = v}
236     return h
237   end
238 
239   # Generic object serializer
240   props = {}
241   @ignored_props ||= Object.new.public_methods
242   (ruby_obj.public_methods - @ignored_props).each do |method_name|
243     # Add them to the prop hash if they take no arguments
244     method_def = ruby_obj.method(method_name)
245     props[method_name.to_s] = ruby_obj.send(method_name) if method_def.arity == 0
246   end
247   props
248 end