Module: RDF::Serializers::Renderers

Defined in:
lib/rdf/serializers/renderers.rb

Class Method Summary collapse

Class Method Details

.add_renderer(ext, content_type, symbol, opts = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/rdf/serializers/renderers.rb', line 26

def self.add_renderer(ext, content_type, symbol, opts = {})
  ActionController::Renderers.add ext do |resource, options|
    self.content_type = content_type
    serializer_opts = RDF::Serializers::Renderers.transform_opts(
      options,
      respond_to?(:serializer_params, true) ? serializer_params : {}
    )
    RDF::Serializers.serializer_for(resource)&.new(resource, serializer_opts)&.dump(symbol, **opts)
  end
end

.register(symbols, opts = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/rdf/serializers/renderers.rb', line 15

def self.register(symbols, opts = {})
  symbols = [symbols] unless symbols.respond_to?(:each)
  symbols.each do |symbol|
    format = RDF::Format.for(symbol)
    raise "#{symbol} is not a known rdf format" if format.nil?

    Mime::Type.register format.content_type.first, format.file_extension.first
    add_renderer(format.file_extension.first, format.content_type.first, format.symbols.first, opts)
  end
end

.transform_include(include, root = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rdf/serializers/renderers.rb', line 37

def self.transform_include(include, root = nil)
  return root if include.blank?
  return [root, include].compact.join('.') if include.is_a?(Symbol) || include.is_a?(String)

  if include.is_a?(Hash)
    include.flat_map do |k, v|
      transform_include(v, [root, k].compact.join('.'))
    end
  elsif include.is_a?(Array)
    include.flat_map do |v|
      transform_include(v, root)
    end
  end.compact
end

.transform_opts(options, params) ⇒ Object



52
53
54
55
56
57
# File 'lib/rdf/serializers/renderers.rb', line 52

def self.transform_opts(options, params)
  (options || {}).merge(
    include: transform_include(options[:include]),
    params: params
  )
end