module Resourceful::Serialize::Model

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

Public Instance Methods

serialize format, options = {}, :attributes → [ ... ] click to toggle source

See the module documentation for Serialize for details.

# File 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) click to toggle source

See the module documentation for Serialize for details.

# File 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

Private Instance Methods

attr_hash_value(attr, sub_attributes) click to toggle source

Given an attribute value and a normalized (see above) attribute hash, returns the serializable form of that attribute.

# File lib/resourceful/serialize.rb, line 137
def attr_hash_value(attr, sub_attributes)
  if attr.respond_to?(:to_serializable)
    attr.to_serializable(sub_attributes)
  else
    attr
  end
end