Class: AdequateSerialization::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/adequate_serialization/serializer.rb

Defined Under Namespace

Classes: ClassNotFoundError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute(*names, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/adequate_serialization/serializer.rb', line 21

def attribute(*names, &block)
  options =
    if names.last.is_a?(Hash)
      names.pop
    else
      {}
    end

  additions =
    names.map! { |name| Attribute.from(name, options.dup, &block) }

  @attributes = attributes + additions
end

.attributesObject



17
18
19
# File 'lib/adequate_serialization/serializer.rb', line 17

def attributes
  @attributes ||= []
end

.serializesObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/adequate_serialization/serializer.rb', line 35

def serializes
  return @serializes if defined?(@serializes)

  class_name = name.gsub(/Serializer\z/, '')

  begin
    @serializes = const_get(class_name)
  rescue NameError
    raise ClassNotFoundError.new(name, class_name)
  end
end

Instance Method Details

#serialize(model, opts = Options.null) ⇒ Object



48
49
50
51
52
# File 'lib/adequate_serialization/serializer.rb', line 48

def serialize(model, opts = Options.null)
  self.class.attributes.each_with_object({}) do |attribute, response|
    attribute.serialize_to(self, response, model, opts.includes)
  end
end