class GalterIrExporter::Export::Actor

Convert a GenericFile including metadata, permissions and version metadata into a PORO so that the metadata can be exported in json format using to_json

Attributes

conversion_count[RW]
converter_registry[R]
ids[R]
limit[R]

Public Class Methods

new() click to toggle source

initialize the class with the default registry

# File lib/galter_ir_exporter/export/actor.rb, line 11
def initialize
  @converter_registry = default_registry
end

Public Instance Methods

call(model_class_list = converter_registry.keys, opts = {}) click to toggle source

Convert the classes using the registered converters from ActiveFedora to json files

@param [Array] model_class_list list of classes to be converter @param [Hash] opts @option opts [Number] :limit Limits the number of conversion done (defaults to -1 or all) @option opts [Array] :ids List of ids to be converted. Can be from any model

# File lib/galter_ir_exporter/export/actor.rb, line 31
def call(model_class_list = converter_registry.keys, opts = {})
  @conversion_count = 0
  validate_class_list(model_class_list)
  parse_options(opts)
  export_models(model_class_list)
end
register_converter(model_class, converter_class) click to toggle source

register a converter for a new class or overwrite the converter for a default class

@param [Class] model_class The ActiveFedora model class to be converter to json @param [Class] converter_class The class that will convert from ActiveFedora to json

# File lib/galter_ir_exporter/export/actor.rb, line 19
def register_converter(model_class, converter_class)
  raise(RegistryError, "Model (#{model_class.name}) for conversion must be an ActiveFedora::Base") unless model_class.ancestors.include?(ActiveFedora::Base)
  raise(RegistryError, "Converter (#{converter_class.name}) for conversion must be an GalterIrExporter::Export::Converter") unless converter_class.ancestors.include?(GalterIrExporter::Export::Converter)
  converter_registry[model_class] = converter_class
end

Private Instance Methods

default_registry() click to toggle source
# File lib/galter_ir_exporter/export/actor.rb, line 84
def default_registry
  { ::GenericFile => GalterIrExporter::Export::GenericFileConverter, ::Collection => GalterIrExporter::Export::CollectionConverter }
end
export_model_list(model_list, converter_class) click to toggle source
# File lib/galter_ir_exporter/export/actor.rb, line 66
def export_model_list(model_list, converter_class)
  model_list.each do |model|
    export_one(converter_class, model)
  end
end
export_models(model_class_list) click to toggle source
# File lib/galter_ir_exporter/export/actor.rb, line 59
def export_models(model_class_list)
  model_class_list.each do |model_class|
    converter_class = converter_registry[model_class]
    export_model_list(model_scope(model_class), converter_class)
  end
end
export_one(converter_class, model) click to toggle source
# File lib/galter_ir_exporter/export/actor.rb, line 72
def export_one(converter_class, model)
  converter = converter_class.new(model)
  path = file_name(model)
  json = converter.to_json(pretty: true)
  File.write(path, json)
  @conversion_count += 1
end
file_name(model) click to toggle source
# File lib/galter_ir_exporter/export/actor.rb, line 80
def file_name(model)
  File.join(file_path, "#{model.class.name.underscore}_#{model.id}.json")
end
file_path() click to toggle source
# File lib/galter_ir_exporter/export/actor.rb, line 88
def file_path
  return @file_path unless @file_path.blank?
  @file_path = "tmp/export"
  FileUtils.mkdir_p @file_path
  @file_path
end
model_scope(model_class) click to toggle source
# File lib/galter_ir_exporter/export/actor.rb, line 45
def model_scope(model_class)
  scope = model_class.all
  scope  = scope.where(id: ids) unless ids.blank?
  scope  = scope.limit(limit - conversion_count) unless limit == -1
  scope
end
parse_options(opts) click to toggle source
# File lib/galter_ir_exporter/export/actor.rb, line 40
def parse_options(opts)
  @limit = opts[:limit] || -1
  @ids = opts[:ids]
end
validate_class_list(model_class_list) click to toggle source
# File lib/galter_ir_exporter/export/actor.rb, line 52
def validate_class_list(model_class_list)
  model_class_list.each do |model_class|
    converter_class = converter_registry[model_class]
    raise(RegistryError, "Undefined Model for conversion (#{model_class.name})") if converter_class.blank?
  end
end