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