Class | RubyAMF::ClassMapping |
In: |
lib/rubyamf/class_mapping.rb
|
Parent: | ::RocketAMF::ClassMapping |
Advanced class mapper based off of RocketAMF class mapper. Adds support for advanced serialization and deserialization.
mapping_scope | [RW] | The mapping scope to use during serialization. This is populated during response serialization automatically by RubyAMF::Envelope. |
Override RocketAMF#mappings to return new RubyAMF::MappingSet object rather than RocketAMF::MappingSet
# File lib/rubyamf/class_mapping.rb, line 99 99: def self.mappings 100: @mappings ||= RubyAMF::MappingSet.new 101: end
Return the actionscript class name for the given ruby object. If the object is a string, that is assumed to be the ruby class name. Otherwise it extracts the ruby class name from the object based on its type. As RocketAMF calls this for all objects on serialization, auto-mapping takes place here if enabled.
# File lib/rubyamf/class_mapping.rb, line 112 112: def get_as_class_name obj 113: # Get class name 114: if obj.is_a?(String) 115: ruby_class_name = obj 116: elsif obj.is_a?(RocketAMF::Values::TypedHash) 117: ruby_class_name = obj.type 118: elsif obj.is_a?(Hash) 119: return nil 120: elsif obj.is_a?(RubyAMF::IntermediateObject) 121: ruby_class_name = obj.object.class.name 122: else 123: ruby_class_name = obj.class.name 124: end 125: 126: # Get AS class name 127: as_class_name = @mappings.get_as_class_name ruby_class_name 128: 129: # Auto-map if necessary, removing namespacing to create mapped class name 130: if RubyAMF.configuration.auto_class_mapping && ruby_class_name && as_class_name.nil? 131: as_class_name = ruby_class_name.split('::').pop 132: @mappings.map :as => as_class_name, :ruby => ruby_class_name 133: end 134: 135: as_class_name 136: end
Creates a ruby object to populate during deserialization for the given actionscript class name. If that actionscript class name is mapped to a ruby class, an object of that class is created using obj = ruby_class_name.constantize.allocate and then :initialize is sent to the new instance unless it implements rubyamf_init. If no ruby class name is defined, a RocketAMF::Values::TypedHash object is created and its type attribute is set to the actionscript class name. As RocketAMF calls this for all objects on deserialization, auto-mapping takes place here if enabled.
# File lib/rubyamf/class_mapping.rb, line 147 147: def get_ruby_obj as_class_name 148: # Get ruby class name 149: ruby_class_name = @mappings.get_ruby_class_name as_class_name 150: 151: # Auto-map if necessary, removing namespacing to create mapped class name 152: if RubyAMF.configuration.auto_class_mapping && as_class_name && ruby_class_name.nil? 153: ruby_class_name = as_class_name.split('.').pop 154: @mappings.map :as => as_class_name, :ruby => ruby_class_name 155: end 156: 157: # Create ruby object 158: if ruby_class_name.nil? 159: return RocketAMF::Values::TypedHash.new(as_class_name) 160: else 161: ruby_class = ruby_class_name.constantize 162: obj = ruby_class.allocate 163: obj.send(:initialize) unless obj.respond_to?(:rubyamf_init) # warhammerkid: Should we check if it has initialize? 164: return obj 165: end 166: end
Performs all enabled property translations (case, key type, ignore_fields) before passing to rubyamf_init if implemented, or to the RocketAMF class mapper implementation.
# File lib/rubyamf/class_mapping.rb, line 171 171: def populate_ruby_obj obj, props, dynamic_props=nil 172: # Translate case of properties before passing down to super 173: if RubyAMF.configuration.translate_case && !obj.is_a?(RocketAMF::Values::AbstractMessage) 174: case_translator = lambda {|injected, pair| injected[pair[0].to_s.underscore.to_sym] = pair[1]; injected} 175: props = props.inject({}, &case_translator) 176: dynamic_props = dynamic_props.inject({}, &case_translator) if dynamic_props 177: end 178: 179: # Convert hash key type to string if it's a hash 180: if RubyAMF.configuration.hash_key_access == :string && obj.is_a?(Hash) 181: key_change = lambda {|injected, pair| injected[pair[0].to_s] = pair[1]; injected} 182: props = props.inject({}, &key_change) 183: dynamic_props = dynamic_props.inject({}, &key_change) if dynamic_props 184: end 185: 186: # Remove ignore_fields if there is a config 187: config = @mappings.serialization_config(obj.class.name, mapping_scope) || {} 188: Array.wrap(config[:ignore_fields]).each do |ignore| 189: props.delete(ignore.to_s) 190: props.delete(ignore.to_sym) 191: if dynamic_props 192: dynamic_props.delete(ignore.to_s) 193: dynamic_props.delete(ignore.to_sym) 194: end 195: end 196: 197: # Handle custom init 198: if obj.respond_to?(:rubyamf_init) 199: obj.rubyamf_init props, dynamic_props 200: else 201: # Fall through to default populator 202: super(obj, props, dynamic_props) 203: end 204: end
Extracts a hash of all object properties for serialization from the object, using rubyamf_hash if implemented with the proper mapping configs for the scope, or the RocketAMF implementation. Before being returned to the serializer, case translation is performed if enabled.
# File lib/rubyamf/class_mapping.rb, line 210 210: def props_for_serialization ruby_obj 211: props = nil 212: 213: # Get properties for serialization 214: if ruby_obj.respond_to?(:rubyamf_hash) 215: if ruby_obj.is_a?(RubyAMF::IntermediateObject) 216: props = ruby_obj.rubyamf_hash 217: else 218: config = @mappings.serialization_config(ruby_obj.class.name, mapping_scope) 219: props = ruby_obj.rubyamf_hash config 220: end 221: else 222: # Fall through to default handlers 223: props = super(ruby_obj) 224: end 225: 226: # Translate case of properties if necessary 227: if RubyAMF.configuration.translate_case 228: props = props.inject({}) do |injected, pair| 229: injected[pair[0].to_s.camelize(:lower)] = pair[1] 230: injected 231: end 232: end 233: 234: props 235: end