class Serialism::Serializer
Base class for concrete serializers to inherit from.
Example:
class Foo attr_accessor :id end class FooSerializer < Serialism::Serializer attributes :id, :computed def computed "computed - #{object.id}" end end item = Foo.new item.id = 12 serializer = FooSerializer.new(item) serializer.render # => {id: 12, computed: "computed - 12"}
Attributes
object[R]
Public Class Methods
attributes(*attrs)
click to toggle source
# File lib/serialism/serializer.rb, line 28 def self.attributes(*attrs) @attributes = attrs if !attrs.empty? @attributes end
new(object)
click to toggle source
# File lib/serialism/serializer.rb, line 33 def initialize(object) @object = object end
Public Instance Methods
render()
click to toggle source
Transform `object` using rules defined by the serializer.
@return [Hash] Keys are defined by the classes `attributes`.
# File lib/serialism/serializer.rb, line 40 def render self.class.attributes.inject({}) do |memo, attr| if respond_to?(attr) memo[attr] = send(attr) elsif object.respond_to?(attr) memo[attr] = object.send(attr) else raise ArgumentError, "Unknown attribute :#{attr}" end memo end end