Class: RubyAMF::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyamf/configuration.rb

Overview

RubyAMF configuration container. It can be accessed by calling RubyAMF.configuration, or modified in a block like so:

RubyAMF.configure do |config|
  config.gateway_path = "/amf"
end

Gateway configuration

Gateway configuration includes the gateway path and details about parameter mapping.

gateway_path

Default: "/rubyamf/gateway". The URL that responds to AMF requests. The URL should start with a “/” and not end with a “/”.

populate_params_hash

Default: true. For Rails users, all amf parameters can be accessed in the controller by calling rubyamf_params. If enabled, the amf parameters are merged into the params hash as well.

show_html_gateway

Default: true. If enabled, non-AMF requests to the gateway url will result in a simple HTML page being returned.

param_mappings

A hash that stores parameter mappings. Should only be modified through calls to map_params

Serialization options

RubyAMF provides a wide variety of customization options for serialization to simplify integration.

translate_case

Default: false. If enabled, properties will be converted to underscore style on deserialization from actionscript and will be converted to camelcase on serialization. This allows you to use language appropriate case style and have RubyAMF automatically care of translation.

auto_class_mapping

Default: false. If a class mapping for a given ruby or actionscript class has not been defined, automatically maps it during serialization or deserialization. Nested ruby or actionscript classes will be automatically mapped to the class name without the namespace. Example: com.rubyamf.User => User or RubyAMF::User => User.

use_array_collection

Default: false. If enabled, all arrays will be serialized as ArrayCollection objects. You can override this on a per-array basis by setting is_array_collection on the array to true or false. (Implementation in RocketAMF)

hash_key_access

Default: :string. If set to :symbol, all deserialized hashes have the keys as symbols. RocketAMF defaults to strings, so setting to :symbol will reduce performance and possibly open you up to memory usage attacks.

preload_models

If you are using in-model mapping and don’t have auto_class_mapping enabled, you may need to force classes to be loaded before mapping takes effect. Simply include all necessary classes as strings to have RubyAMF force them to be loaded on initialization. Example: config.preload_models = ["User", "Course"]

check_for_associations

Default: true. If enabled, associations that have been pre-loaded, either through :include or simply by being accessed, will be automatically included during serialization without any additional configuration.

ignore_fields

Default: ['created_at', 'created_on', 'updated_at', 'updated_on']. A list of all properties that should not be deserialized by default. The class-level :ignore_fields config overrides this.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rubyamf/configuration.rb', line 87

def initialize
  @gateway_path = "/rubyamf/gateway"
  @param_mappings = {}
  @populate_params_hash = true
  @show_html_gateway = true

  @translate_case = false
  @auto_class_mapping = false
  @use_array_collection = false
  @hash_key_access = :string
  @preload_models = []
  @check_for_associations = true
  @ignore_fields = ['created_at', 'created_on', 'updated_at', 'updated_on']
end

Instance Attribute Details

#auto_class_mappingObject

Serialization options



83
84
85
# File 'lib/rubyamf/configuration.rb', line 83

def auto_class_mapping
  @auto_class_mapping
end

#check_for_associationsObject

Serialization options



83
84
85
# File 'lib/rubyamf/configuration.rb', line 83

def check_for_associations
  @check_for_associations
end

#gateway_pathObject

Gateway options



79
80
81
# File 'lib/rubyamf/configuration.rb', line 79

def gateway_path
  @gateway_path
end

#hash_key_accessObject

Serialization options



83
84
85
# File 'lib/rubyamf/configuration.rb', line 83

def hash_key_access
  @hash_key_access
end

#ignore_fieldsObject

Serialization options



83
84
85
# File 'lib/rubyamf/configuration.rb', line 83

def ignore_fields
  @ignore_fields
end

#param_mappingsObject (readonly)

Returns the value of attribute param_mappings.



80
81
82
# File 'lib/rubyamf/configuration.rb', line 80

def param_mappings
  @param_mappings
end

#populate_params_hashObject

Gateway options



79
80
81
# File 'lib/rubyamf/configuration.rb', line 79

def populate_params_hash
  @populate_params_hash
end

#preload_modelsObject

Serialization options



83
84
85
# File 'lib/rubyamf/configuration.rb', line 83

def preload_models
  @preload_models
end

#show_html_gatewayObject

Gateway options



79
80
81
# File 'lib/rubyamf/configuration.rb', line 79

def show_html_gateway
  @show_html_gateway
end

#translate_caseObject

Serialization options



83
84
85
# File 'lib/rubyamf/configuration.rb', line 83

def translate_case
  @translate_case
end

#use_array_collectionObject

Serialization options



83
84
85
# File 'lib/rubyamf/configuration.rb', line 83

def use_array_collection
  @use_array_collection
end

Instance Method Details

#class_mapperObject

Returns the class mapper class being used



118
119
120
121
122
123
# File 'lib/rubyamf/configuration.rb', line 118

def class_mapper
  if @class_mapper.nil?
    @class_mapper = RubyAMF::ClassMapping
  end
  @class_mapper
end

#class_mapper=(klass) ⇒ Object

Set to the class of any conforming class mapper to use it instead of RubyAMF::ClassMapping. If you don’t need any of the advanced features offered by the RubyAMF class mapper, you can gain some substantial performance improvements by settings this to RocketAMF::Ext::FastClassMapping or RocketAMF::ClassMapping for the slower pure-ruby version.



131
132
133
# File 'lib/rubyamf/configuration.rb', line 131

def class_mapper= klass
  @class_mapper = klass
end

#load_legacy(path = nil) ⇒ Object

Loads the legacy config file at the given path or tries to locate it by looking for a rubyamf_config.rb file in several possible locations. Automatically run by RubyAMF if you are using Rails 2, it should be run before any additional configuration if you are not using Rails 2, as it overrides all previous configuration.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rubyamf/configuration.rb', line 140

def load_legacy path=nil
  # Locate legacy config
  unless path
    possible = []
    possible << File.join(RAILS_ROOT, 'config', 'rubyamf_config.rb') if defined?(RAILS_ROOT)
    possible << File.join('config', 'rubyamf_config.rb')
    possible << 'rubyamf_config.rb'
    unless path = possible.find {|p| File.exists?(p)}
      raise "rubyamf_config.rb not found"
    end
  end

  # Load legacy config
  $" << "app/configuration" # prevent legacy code require from doing anything
  LegacySandbox.module_eval(File.read(path))
  $".pop
  cm = LegacySandbox::RubyAMF::ClassMappings
  pm = LegacySandbox::RubyAMF::ParameterMappings

  # Raise exceptions for disabled settings
  if cm.force_active_record_ids != nil; raise "CONFIG PARSE ERROR: force_active_record_ids is no longer supported. Use <tt>:except</tt> if you want to prevent the id from being serialized."; end
  if cm.hash_key_access == :indifferent; raise "CONFIG PARSE ERROR: indifferent hash_key_access is not supported for performance reasons. Use either :string or :symbol, the default."; end
  if cm.default_mapping_scope != nil; raise "CONFIG PARSE ERROR: default_mapping_scope is not supported globally. Please log a feature request if you need it, or use switch to the new config syntax which supports per-model defaults."; end
  if cm.use_ruby_date_time == true; raise "CONFIG PARSE ERROR: use_ruby_date_time is not supported by RocketAMF. Please log a feature request if you need it."; end
  if pm.scaffolding == true; raise "CONFIG PARSE ERROR: scaffolding is not supported. Please log a feature request if you need it."; end

  # Populate ClassMappings configs from legacy config
  @ignore_fields = cm.ignore_fields unless cm.ignore_fields.nil?
  @translate_case = cm.translate_case if cm.translate_case == true
  @hash_key_access = cm.hash_key_access unless cm.hash_key_access.nil?
  @use_array_collection = cm.use_array_collection if cm.use_array_collection == true
  @check_for_associations = cm.check_for_associations if cm.check_for_associations == false
  mapset = class_mapper.mappings
  (cm.mappings || []).each do |legacy_mapping|
    mapping = {}
    if legacy_mapping[:type] == 'active_resource'
      raise "CONFIG PARSE ERROR: active_resource mapping type is not supported. Please log a feature request or stop using it."
    end

    # Extract unscoped settings
    mapping[:as] = legacy_mapping[:actionscript]
    mapping[:ruby] = legacy_mapping[:ruby]
    mapping[:methods] = legacy_mapping[:methods] unless legacy_mapping[:methods].nil?
    mapping[:ignore_fields] = legacy_mapping[:ignore_fields] unless legacy_mapping[:ignore_fields].nil?

    # Process possibly scoped settings
    attrs = legacy_mapping[:attributes]
    assoc = legacy_mapping[:associations]
    if attrs.is_a?(Hash) || assoc.is_a?(Hash)
      # Extract scopes
      scopes = []
      scopes += attrs.keys if attrs.is_a?(Hash)
      scopes += assoc.keys if assoc.is_a?(Hash)

      # Build settings for scopes
      scopes.each do |scope|
        scoped = mapping.dup
        scoped[:scope] = scope.to_sym
        scoped[:only] = attrs.is_a?(Hash) ? attrs[scope] : attrs
        scoped.delete(:only) if scoped[:only].nil?
        scoped[:include] = assoc.is_a?(Hash) ? assoc[scope] : assoc
        scoped.delete(:include) if scoped[:include].nil?
        mapset.map scoped
      end
    else
      # No scoping
      mapping[:only] = attrs unless attrs.nil?
      mapping[:include] = assoc unless assoc.nil?
      mapset.map mapping
    end
  end

  # Populate ParameterMapping configs from legacy config
  @populate_params_hash = pm.always_add_to_params if pm.always_add_to_params == false
  (pm.mappings || []).each do |m|
    params = []
    m[:params].each do |k, v|
      unless v =~ /\[(\d+)\]/
        raise "CONFIG PARSE ERROR: parameter mappings are no longer evalled - '#{v}' must match [DIGITS]. Please log a feature request if you need this."
      end
      params[$1.to_i] = k
    end
    self.map_params m[:controller], m[:action], params
  end

  # Reset sandbox
  cm.reset
  pm.reset

  self
end

#map_params(controller, action, params) ⇒ Object

Maps the given array of named parameters to the arguments for calls to the given controller and action. For Rails users, the prefered method of parameter mapping is through routing (see RubyAMF::Rails::Routing).

Example:

config.map_params "UserController", "login", [:session_token, :username, :password]
# params hash => {
#   0 => "asdf", 1 => "user", 2 => "pass",
#   :session_token => "asdf", :username => "user", :password => "pass"
# }


113
114
115
# File 'lib/rubyamf/configuration.rb', line 113

def map_params controller, action, params
  @param_mappings[controller.to_s+"#"+action.to_s] = params
end