Module: ASpaceExport

Defined in:
backend/app/exporters/lib/utils.rb,
backend/app/exporters/lib/exporter.rb,
backend/app/exporters/lib/streaming_xml.rb,
backend/app/exporters/lib/export_helpers.rb

Defined Under Namespace

Modules: ArchivalObjectDescriptionHelpers, ExportModelHelpers, LazyChildEnumerations, Utils Classes: ExportModel, ExportModelNotFoundError, RawXMLHandler, Serializer, SerializerNotFoundError, StreamHandler

Constant Summary collapse

@@initialized =
false

Class Method Summary collapse

Class Method Details

.get_serializer_for(model, opts) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'backend/app/exporters/lib/exporter.rb', line 77

def self.get_serializer_for(model, opts)
  key = if opts[:serializer]
          opts[:serializer]
        else
          model.class.instance_variable_get(:@model_for)
        end

  serializer(key).new
end

.initObject



12
13
14
15
16
17
18
19
20
21
# File 'backend/app/exporters/lib/exporter.rb', line 12

def self.init
  return if @@initialized

  @@serializers = []
  @@models = []
  Dir.glob(File.dirname(__FILE__) + '/../serializers/*.rb', &method(:require))
  Dir.glob(File.dirname(__FILE__) + '/../models/*.rb', &method(:require))

  @@initialized = true
end

.model(name, &block) ⇒ Object

Define or get an export model



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'backend/app/exporters/lib/exporter.rb', line 56

def self.model(name, &block)
  if block_given?
    model = Class.new(ExportModel, &block)
    model.instance_variable_set(:@model_for, name)
    model.class_eval do
      def self.model_for?(name_queried)
        name_queried == @model_for
      end
    end

    register_model(model)
  else
    @@models.each do |model|
      return model if model.model_for? name
    end

    raise ExportModelNotFoundError.new("Can't find a model for: #{name}")
  end
end

.register_model(model_class) ⇒ Object



50
51
52
# File 'backend/app/exporters/lib/exporter.rb', line 50

def self.register_model(model_class)
  @@models << model_class
end

.register_serializer(serializer_class) ⇒ Object



24
25
26
# File 'backend/app/exporters/lib/exporter.rb', line 24

def self.register_serializer(serializer_class)
  @@serializers << serializer_class
end

.serialize(model, opts = {}) ⇒ Object



88
89
90
91
# File 'backend/app/exporters/lib/exporter.rb', line 88

def self.serialize(model, opts = {})
  s = get_serializer_for(model, opts)
  s.serialize(model, opts)
end

.serializer(name, &block) ⇒ Object

Define or get a serializer



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'backend/app/exporters/lib/exporter.rb', line 29

def self.serializer(name, &block)
  if block_given?
    serializer = Class.new(Serializer, &block)
    serializer.instance_variable_set(:@serializer_for, name)
    serializer.class_eval do
      def self.serializer_for?(name_queried)
        name_queried == @serializer_for
      end
    end

    register_serializer(serializer)
  else
    @@serializers.each do |serializer|
      return serializer if serializer.serializer_for? name
    end

    raise SerializerNotFoundError.new("Can't find a serializer for: #{name}")
  end
end

.stream(model, opts = {}) ⇒ Object



94
95
96
97
# File 'backend/app/exporters/lib/exporter.rb', line 94

def self.stream(model, opts = {})
  s = get_serializer_for(model, opts)
  s.stream(model)
end