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
Public Class Methods
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
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 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
# File lib/galter_ir_exporter/export/actor.rb, line 84 def default_registry { ::GenericFile => GalterIrExporter::Export::GenericFileConverter, ::Collection => GalterIrExporter::Export::CollectionConverter } end
# 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
# 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
# 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 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 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
# 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
# File lib/galter_ir_exporter/export/actor.rb, line 40 def parse_options(opts) @limit = opts[:limit] || -1 @ids = opts[:ids] end
# 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