class SimpleSerializer

Constants

VERSION

Attributes

fields[R]

Public Class Methods

[](object) click to toggle source
# File lib/simple_serializer.rb, line 39
def [](object)
  to_h(object)
end
attribute(*fields, **named, &block)
Alias for: attributes
attributes(*fields, **named, &block) click to toggle source
# File lib/simple_serializer.rb, line 51
def attributes(*fields, **named, &block)
  @fields ||= {}

  fields.each { |k| @fields[k] = block }

  @fields.merge!(named)
end
Also aliased as: attribute
helpers() click to toggle source
# File lib/simple_serializer.rb, line 25
def helpers
  serializer = self

  Module.new.tap do |m|
    m.define_singleton_method(:included) do |base|
      base.define_method(:to_h) { |o = {}| serializer.to_h(self, o) }

      base.define_method(:to_json) { |o = {}| serializer.to_json(self, o) }

      base.define_method(:serializer_class) { serializer }
    end
  end
end
inherited(sub) click to toggle source
# File lib/simple_serializer.rb, line 21
def inherited(sub)
  sub.instance_variable_set(:@fields, fields.to_h.dup)
end
new(object, opts = {}) click to toggle source
# File lib/simple_serializer.rb, line 6
def initialize(object, opts = {})
  @include = opts[:include] || []
  @exclude = opts[:exclude] || []
  @object = object
end
serialize(field, &block) click to toggle source
# File lib/simple_serializer.rb, line 61
def serialize(field, &block)
  attributes(field => Class.new(SimpleSerializer, &block))
end
to_h(object, opts = {}) click to toggle source
# File lib/simple_serializer.rb, line 43
def to_h(object, opts = {})
  new(object, opts).to_h
end
to_json(object, opts = {}) click to toggle source
# File lib/simple_serializer.rb, line 47
def to_json(object, opts = {})
  to_h(object, opts).to_json
end

Public Instance Methods

to_h() click to toggle source
# File lib/simple_serializer.rb, line 12
def to_h
  return @object.map(&method(:to_h_single)) if @object.is_a?(Enumerable)

  to_h_single(@object)
end

Private Instance Methods

nest(result) click to toggle source
# File lib/simple_serializer.rb, line 88
def nest(result)
  serializable = result.respond_to?(:serializer_class)
  serializable_array = result.is_a?(Enumerable) &&
                       result.first.respond_to?(:serializer_class)

  return result.serializer_class.to_h(result) if serializable
  return result.first.serializer_class.to_h(result) if serializable_array

  result
end
serialize_key_val(obj, key, val) click to toggle source
# File lib/simple_serializer.rb, line 76
def serialize_key_val(obj, key, val)
  return { key => nest(obj.send(key)) } if val.nil?

  return { key => nest(obj.instance_eval(&val)) } if val.is_a?(Proc)

  if val.is_a?(Class) && val < SimpleSerializer
    return { key => val[obj.send(key)] }
  end

  { val => nest(obj.send(key)) }
end
to_h_single(object) click to toggle source
# File lib/simple_serializer.rb, line 68
def to_h_single(object)
  self.class.fields&.merge(Hash[@include.zip])&.reduce({}) do |h, (k, v)|
    next h if @exclude.include?(k)

    h.merge!(serialize_key_val(object, k, v))
  end
end