class AMF::ClassMapper
Attributes
map[R]
Public Class Methods
initialize()
click to toggle source
Methods
# File lib/amf/pure/mapping/class_mapper.rb 33 def initialize 34 @map = MappingSet.new 35 end
new()
click to toggle source
Methods
# File lib/amf/pure/mapping/class_mapper.rb 65 def initialize 66 #cache static variable 67 @map = ClassMapper.map 68 @ignored_props = Object.new.public_methods 69 end
register_class_alias(class_local, class_remote)
click to toggle source
# File lib/amf/pure/mapping/class_mapper.rb 38 def register_class_alias(class_local, class_remote) 39 @map.register_class_alias(class_local, class_remote) 40 end
register_classes(map)
click to toggle source
# File lib/amf/pure/mapping/class_mapper.rb 43 def register_classes(map) 44 map.each do |class_local, class_remote| 45 self.register_class_alias(class_local, class_remote) 46 end 47 end
reset()
click to toggle source
Reset all class mappings except the defaults
# File lib/amf/pure/mapping/class_mapper.rb 51 def reset 52 @map = MappingSet.new 53 nil 54 end
Public Instance Methods
create_object(class_name_remote)
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 AMF::HashWithType
with the serialized class name.
# File lib/amf/pure/mapping/class_mapper.rb 88 def create_object(class_name_remote) 89 result = nil 90 91 if class_name_remote.nil? || class_name_remote.empty? 92 return {} 93 end 94 95 class_name_local = @map.get_class_name_local(class_name_remote) 96 97 if class_name_local.nil? 98 # Populate a simple hash, since no mapping 99 result = HashWithType.new(class_name_remote) 100 else 101 ruby_class = class_name_local.split('::').inject(Kernel) { |scope, const_name| scope.const_get(const_name) } 102 result = ruby_class.new 103 end 104 105 result 106 end
get_class_name_remote(object)
click to toggle source
Returns the other-language class name for the given ruby object.
# File lib/amf/pure/mapping/class_mapper.rb 73 def get_class_name_remote(object) 74 # Get class name 75 return object.class_type if object.is_a?(HashWithType) 76 return nil if object.is_a?(Hash) 77 78 class_name_local = object.class.name 79 80 # Get mapped remote class name 81 @map.get_class_name_remote(class_name_local) 82 end
object_deserialize(target, props)
click to toggle source
Populates the ruby object using the given properties. props will be hashes with symbols for keys.
# File lib/amf/pure/mapping/class_mapper.rb 110 def object_deserialize(target, props) 111 # Don't even bother checking if it responds to setter methods if it's a TypedHash 112 if target.is_a?(HashWithType) 113 target.merge! props 114 return target 115 end 116 117 # Some type of object 118 hash_like = target.respond_to?('[]=') 119 120 props.each do |key, value| 121 if target.respond_to?("#{key}=") 122 target.send("#{key}=", value) 123 elsif hash_like 124 target[key] = value 125 end 126 end 127 128 target 129 end
object_serialize(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/amf/pure/mapping/class_mapper.rb 137 def object_serialize(ruby_obj) 138 result = {} 139 140 # Handle hashes 141 if ruby_obj.is_a?(Hash) 142 143 # Stringify keys to make it easier later on and allow sorting 144 ruby_obj.each { |k, v| result[k.to_s] = v } 145 146 else 147 148 # Generic object serializer 149 #todo: cache result of subtract 150 (ruby_obj.public_methods - @ignored_props).each do |method_name| 151 # Add them to the prop hash if they take no arguments 152 method_def = ruby_obj.method(method_name) 153 result[method_name.to_s] = ruby_obj.send(method_name) if method_def.arity == 0 154 end 155 end 156 157 result 158 end