Module: Resourceful::Serialize::Model

Included in:
ActiveRecord::Base
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.



111
112
113
114
115
116
117
118
119
120
# File 'lib/vendor/plugins/make_resourceful/lib/resourceful/serialize.rb', line 111

def serialize(format, options)
  raise "Must specify :attributes option" unless options[:attributes]
  hash = self.to_serializable(options[:attributes])
  root = self.class.to_s.underscore
  if format == :xml
    hash.send("to_#{format}", :root => root)
  else
    {root => hash}.send("to_#{format}")
  end
end

#to_serializable(attributes) ⇒ Object

See the module documentation for Serialize for details.



123
124
125
126
127
128
129
130
# File 'lib/vendor/plugins/make_resourceful/lib/resourceful/serialize.rb', line 123

def to_serializable(attributes)
  raise "Must specify attributes for #{self.inspect}.to_serializable" if attributes.nil?

  Serialize.normalize_attributes(attributes).inject({}) do |hash, (key, value)|
    hash[key.to_s] = attr_hash_value(self.send(key), value)
    hash
  end
end