Module: HALDecorator

Includes:
Attributes, Collection, Curies, Deserializer, Embedded, Links, Model, Policy, SerializeHooks, Serializer
Defined in:
lib/hal_decorator.rb,
lib/hal_decorator/links.rb,
lib/hal_decorator/model.rb,
lib/hal_decorator/curies.rb,
lib/hal_decorator/policy.rb,
lib/hal_decorator/embedded.rb,
lib/hal_decorator/property.rb,
lib/hal_decorator/attributes.rb,
lib/hal_decorator/collection.rb,
lib/hal_decorator/pagination.rb,
lib/hal_decorator/policy/dsl.rb,
lib/hal_decorator/serializer.rb,
lib/hal_decorator/deserializer.rb,
lib/hal_decorator/serialize_hooks.rb

Defined Under Namespace

Modules: Attributes, Collection, Curies, Deserializer, Embedded, Links, Model, Policy, SerializeHooks, Serializer Classes: Pagination, Property

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Policy

#policy

Methods included from Deserializer

#from_hal

Methods included from Serializer

#to_collection, #to_hal

Methods included from Model

#inherited, #model

Methods included from SerializeHooks

#post_serialize

Methods included from Collection

#collection

Methods included from Embedded

#embed

Methods included from Curies

#curie

Methods included from Links

#link

Methods included from Attributes

#attribute

Class Attribute Details

.paginateObject

Returns the value of attribute paginate.



3
4
5
# File 'lib/hal_decorator/pagination.rb', line 3

def paginate
  @paginate
end

Class Method Details

.base_href=(base) ⇒ Object



5
6
7
# File 'lib/hal_decorator/links.rb', line 5

def self.base_href=(base)
  @base_href = base&.sub(%r(/*$), '')
end

.from_hal(decorator, payload, resource = nil) ⇒ Object



5
6
7
# File 'lib/hal_decorator/deserializer.rb', line 5

def self.from_hal(decorator, payload, resource = nil)
  decorator.from_hal(payload, resource)
end

.href(href) ⇒ Object



9
10
11
12
13
# File 'lib/hal_decorator/links.rb', line 9

def self.href(href)
  return href if (@base_href ||= '').empty?
  return href if href =~ %r(\A(\w+://)?[^/])
  @base_href + href
end

.lookup_decorator(model) ⇒ Object



16
17
18
19
20
# File 'lib/hal_decorator/model.rb', line 16

def HALDecorator.lookup_decorator(model)
  clazz = model.is_a?(Class) ? model : model.class
  decorators = @decorators.select { |d, m| m == clazz }.keys.compact
  decorators.empty? ? nil : decorators
end

.lookup_model(decorator) ⇒ Object



12
13
14
# File 'lib/hal_decorator/model.rb', line 12

def HALDecorator.lookup_model(decorator)
  @decorators[decorator]
end

.register(model:, decorator:) ⇒ Object



4
5
6
# File 'lib/hal_decorator/model.rb', line 4

def HALDecorator.register(model:, decorator:)
  @decorators[decorator] = model
end

.to_collection(resources, options = {}) ⇒ Object

Raises:



14
15
16
17
18
19
20
# File 'lib/hal_decorator/serializer.rb', line 14

def self.to_collection(resources, options = {})
  raise Serializer::Error, "resources is nil" if resources.nil?
  decorator = options.delete(:decorator)
  decorator ||= HALDecorator.lookup_decorator(resources.first)&.last
  raise Serializer::Error, "No decorator for #{resources.first}" unless decorator
  decorator.to_collection(resources, options)
end

.to_hal(resource, options = {}) ⇒ Object

Raises:



6
7
8
9
10
11
12
# File 'lib/hal_decorator/serializer.rb', line 6

def self.to_hal(resource, options = {})
  raise Serializer::Error, "Resource is nil" if resource.nil?
  decorator = options.delete(:decorator)
  decorator ||= HALDecorator.lookup_decorator(resource)&.last
  raise Serializer::Error, "No decorator for #{resource}" unless decorator
  decorator.to_hal(resource, options)
end

.unregister(decorator) ⇒ Object



8
9
10
# File 'lib/hal_decorator/model.rb', line 8

def HALDecorator.unregister(decorator)
  @decorators.delete_if { |d,_| d == decorator }
end