Module: Resourceful::Serialize::Array

Included in:
Array
Defined in:
lib/vendor/plugins/make_resourceful/lib/resourceful/serialize.rb

Overview

This module contains the definitions of serialize and to_serializable that are included in ActiveRecord::Base.

Instance Method Summary collapse

Instance Method Details

#serialize(format, options) ⇒ Object

:call-seq:

serialize format, options = {}, :attributes => [ ... ]

See the module documentation for Serialize for details.



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/vendor/plugins/make_resourceful/lib/resourceful/serialize.rb', line 153

def serialize(format, options)
  raise "Not all elements respond to to_serializable" unless all? { |e| e.respond_to? :to_serializable }
  raise "Must specify :attributes option" unless options[:attributes]

  serialized = map { |e| e.to_serializable(options[:attributes]) }
  root = first.class.to_s.pluralize.underscore

  if format == :xml
    serialized.send("to_#{format}", :root => root)
  else
    {root => serialized}.send("to_#{format}")
  end
end

#to_serializable(attributes) ⇒ Object

See the module documentation for Serialize for details.



168
169
170
171
172
173
174
175
# File 'lib/vendor/plugins/make_resourceful/lib/resourceful/serialize.rb', line 168

def to_serializable(attributes)
  if first.respond_to?(:to_serializable)
    attributes = Serialize.normalize_attributes(attributes)
    map { |e| e.to_serializable(attributes) }
  else
    self
  end
end