Module: HALDecorator::Serializer

Included in:
HALDecorator
Defined in:
lib/hal_decorator/serializer.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Instance Method Details

#to_collection(resources = [], options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hal_decorator/serializer.rb', line 30

def to_collection(resources = [], options = {})
  parameters = collection_parameters
  if parameters.nil?
    raise Error,
      "Trying to serialize a collection using #{self} which has no collection info. " \
      "Add a 'collection' spec to the serializer or use another serializer"
  end
  links = parameters.links
  curies = parameters.curies
  serialized = _serialize_attributes(parameters.attributes, resources, nil, options)
  serialized.merge! _serialize_links(links, curies, resources, nil, options)

  serialized_resources = resources.map { |resource| to_hash(resource, options) }
  serialized[:_embedded] = { parameters.name => serialized_resources }
  JSON.generate(serialized)
end

#to_hal(resource = nil, options = {}) ⇒ Object



25
26
27
28
# File 'lib/hal_decorator/serializer.rb', line 25

def to_hal(resource = nil, options = {})
  hash = to_hash(resource, options)
  JSON.generate(hash)
end