Module: Serializable::InstanceMethods

Defined in:
lib/acts_as_serializable.rb

Overview

This module contains instance methods

Instance Method Summary collapse

Instance Method Details

#serialize(builder, options = {}, &block) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/acts_as_serializable.rb', line 119

def serialize(builder, options = {}, &block)
  if version_number = options[:version]
    version = version_number.is_a?(Serializable::Version) ? version_number : Version.new(version_number)
    if found_version = self.class.serialization_versions.find_version(version)
      return self.send("serialize_to_version_#{found_version.to_s_underscored}", builder, options, &block)
    else
      raise "Version #{version} given but no serialization method found"
    end
  else
    if version = self.class.default_serialization_version
      return self.send("serialize_to_version_#{version.to_s_underscored}", builder, options, &block)
    else
      raise "No serialization method found"
    end
  end
end

#serialize_for(builder, options = {}, &block) ⇒ Object



136
137
138
# File 'lib/acts_as_serializable.rb', line 136

def serialize_for(builder, options = {}, &block)
  self.send(builder.serialization_method!, options, &block)
end

#to_hash(options = {}, &block) ⇒ Object



107
108
109
# File 'lib/acts_as_serializable.rb', line 107

def to_hash(options = {}, &block)
  serialize(Builder::HashStructure.new, options, &block)
end

#to_json(options = {}, &block) ⇒ Object



111
112
113
# File 'lib/acts_as_serializable.rb', line 111

def to_json(options = {}, &block)
  serialize(Builder::HashStructure.new, options, &block).to_json
end

#to_xml(options = {}, &block) ⇒ Object



115
116
117
# File 'lib/acts_as_serializable.rb', line 115

def to_xml(options = {}, &block)
  serialize(Builder::XmlMarkup.new, options, &block)
end