Class: RestfulX::Serialization::AMFSerializer
- Inherits:
-
ActiveRecord::Serialization::Serializer
- Object
- ActiveRecord::Serialization::Serializer
- RestfulX::Serialization::AMFSerializer
- Defined in:
- lib/restfulx/rx_active_record.rb
Overview
:nodoc:
Instance Method Summary collapse
- #add_associations(association, records, opts, serializer) ⇒ Object
-
#initialize(record, options = {}) ⇒ AMFSerializer
constructor
A new instance of AMFSerializer.
- #serializable_attributes ⇒ Object
- #serialize ⇒ Object
Constructor Details
#initialize(record, options = {}) ⇒ AMFSerializer
Returns a new instance of AMFSerializer.
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/restfulx/rx_active_record.rb', line 193 def initialize(record, = {}) super(record, ) @options[:methods] ||= [] @options[:amf_version] = 3 @options[:serializer] ||= RestfulX::AMF::RxAMFSerializer.new # options are duplicated by default so we need a copy for caching attributes @original_options = @original_options[:cached_attributes] ||= {} @options[:cached_instances] = @original_options[:cached_instances] ||= {} end |
Instance Method Details
#add_associations(association, records, opts, serializer) ⇒ Object
249 250 251 252 253 254 255 256 257 258 |
# File 'lib/restfulx/rx_active_record.rb', line 249 def add_associations(association, records, opts, serializer) serializer.write_vr(association.to_s.camelcase(:lower)) if records.is_a?(Enumerable) serializer.serialize_models_array(records, opts) else if record = @record.send(association) record.to_amf(opts) end end end |
#serializable_attributes ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/restfulx/rx_active_record.rb', line 219 def serializable_attributes includes = @options[:include] ||= {} # if we are serializing an array we only need to compute serializable_attributes for the # objects of the same type at the same level once if @original_options[:cached_attributes].has_key?(@record.class.name) @original_options[:cached_attributes][@record.class.name] else associations = Hash[*@record.class.reflect_on_all_associations(:belongs_to).collect do |assoc| if assoc..has_key?(:polymorphic) && assoc.[:polymorphic] @options[:except] = ([] << @options[:except] << "#{assoc.name}_type".to_sym).flatten # class_name = @record[assoc.options[:foreign_type]].constantize class_name = "polymorphic" else class_name = assoc.klass end [assoc.primary_key_name, {:name => assoc.name, :klass => class_name}] end.flatten] attributes = serializable_names.select do |name| !includes.include?(associations[name][:name]) rescue true end.map do |name| associations.has_key?(name) ? {:name => name, :ref_name => associations[name][:name].to_s.camelize(:lower), :ref_class => associations[name][:klass], :orig_ref_name => associations[name][:name].to_s } : name.to_sym end @original_options[:cached_attributes][@record.class.name] = attributes attributes end end |
#serialize ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/restfulx/rx_active_record.rb', line 205 def serialize @options[:serializer].serialize_record(@record, serializable_attributes, @options) do |serializer| ([].concat(@options[:methods])).each do |method| if @record.respond_to?(method) serializer.write_vr(method.to_s.camelcase(:lower)) serializer.serialize_property(@record.send(method)) end end add_includes do |association, records, opts| add_associations(association, records, opts, serializer) end end.to_s end |