Module: HALPresenter::Serializer

Included in:
HALPresenter
Defined in:
lib/hal_presenter/serializer.rb

Defined Under Namespace

Modules: ClassMethods Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



32
33
34
# File 'lib/hal_presenter/serializer.rb', line 32

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hal_presenter/serializer.rb', line 46

def to_collection(resources = [], **options)
  unless can_serialize_collection?
    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
  options = options.dup
  options[:paginate] = HALPresenter.paginate unless options.key? :paginate
  options[:_depth] ||= 0
  hash = to_collection_hash(resources, options)
  move_curies_to_root! hash
  return hash if options[:as_hash]

  JSON.generate(hash)
end

#to_hal(resource = nil, **options) ⇒ Object



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

def to_hal(resource = nil, **options)
  options = options.dup
  options[:_depth] ||= 0
  hash = to_hash(resource, options)
  move_curies_to_root! hash
  return hash if options[:as_hash]

  JSON.generate(hash)
end