Class: RubyAMF::MappingSet

Inherits:
RocketAMF::MappingSet
  • Object
show all
Defined in:
lib/rubyamf/class_mapping.rb

Overview

Advanced mapping container to support various serialization customization settings. Used by RubyAMF class mapper to store advanced mappings.

Defined Under Namespace

Classes: Mapping

Constant Summary collapse

SERIALIZATION_PROPS =
[:except, :only, :methods, :include, :ignore_fields]

Instance Method Summary collapse

Constructor Details

#initializeMappingSet

Returns a new instance of MappingSet.



16
17
18
19
20
# File 'lib/rubyamf/class_mapping.rb', line 16

def initialize
  @as_mappings = {}
  @ruby_mappings = {}
  map_defaults
end

Instance Method Details

#get_as_class_name(ruby_class_name) ⇒ Object

Returns the actionscript class name mapped to the given ruby class name. Returns nil if not found.



69
70
71
72
# File 'lib/rubyamf/class_mapping.rb', line 69

def get_as_class_name ruby_class_name
  mapping = @ruby_mappings[ruby_class_name]
  return mapping.nil? ? nil : mapping.as
end

#get_ruby_class_name(as_class_name) ⇒ Object

Returns the ruby class name mapped to the given actionscript class name. Returns nil if not found.



76
77
78
79
# File 'lib/rubyamf/class_mapping.rb', line 76

def get_ruby_class_name as_class_name
  mapping = @as_mappings[as_class_name]
  return mapping.nil? ? nil : mapping.ruby
end

#map(params) ⇒ Object

Map a given actionscript class to a ruby class. You can also control which properties are serialized using :except, :only, :methods, :include for relations, and :ignore_fields for skipping certain fields during deserialization.

Use fully qualified names for both.

Examples:

m.map :as => 'com.example.Date', :ruby => 'Example::Date'
m.map :flash => 'User', :ruby => 'User', :only => 'username'
m.map :flash => 'User', :ruby => 'User', :scope => :other, :include => [:courses, :teacher]


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubyamf/class_mapping.rb', line 34

def map params
  # Extract and validate ruby and AS class names
  ruby_class = params[:ruby]
  as_class = params[:as] || params[:flash] || params[:actionscript]
  raise "Must pass ruby class name under :ruby key" unless ruby_class
  raise "Must pass as class name under :flash, :as, or :actionscript key" unless as_class

  # Get mapping if it already exists
  mapping = @as_mappings[as_class] || @ruby_mappings[ruby_class] || Mapping.new
  mapping.ruby = ruby_class
  mapping.as = as_class
  @as_mappings[as_class] = mapping
  @ruby_mappings[ruby_class] = mapping

  # If they tried to configure the serialization props, store that too under the proper scope
  serialization_config = {}
  params.each {|k,v| serialization_config[k] = v if SERIALIZATION_PROPS.include?(k)}
  if serialization_config.length > 0
    # Determine scope
    scope = nil
    if params[:default_scope]
      scope = mapping.default_scope = params[:default_scope]
    elsif params[:scope]
      scope = params[:scope]
    else
      scope = mapping.default_scope
    end

    # Add config to scope hash
    mapping.scopes[scope.to_sym] = serialization_config
  end
end

#serialization_config(ruby_class_name, scope = nil) ⇒ Object

Returns the property serialization config for the given ruby class name and scope. If scope is nil, it uses the default scope.



83
84
85
86
87
88
89
90
91
# File 'lib/rubyamf/class_mapping.rb', line 83

def serialization_config ruby_class_name, scope = nil
  mapping = @ruby_mappings[ruby_class_name]
  if mapping.nil?
    nil
  else
    scope ||= mapping.default_scope
    mapping.scopes[scope.to_sym]
  end
end