class GalterIrExporter::Migration::Survey::FedoraIdService

Attributes

model_registry[RW]

Public Class Methods

new() click to toggle source

initialize the service with the default models (GenericFile & Collection) registered

# File lib/galter_ir_exporter/migration/survey/fedora_id_service.rb, line 10
def initialize
  @model_registry = default_registry
end

Public Instance Methods

call(limit = :all) click to toggle source

returns a list of ids for all the registered classes in the repository

@param [Number] limit limits the number of results (default is all)

# File lib/galter_ir_exporter/migration/survey/fedora_id_service.rb, line 27
def call(limit = :all)
  ids = all_ids.select { |id| registered_model?(id) }
  return ids if limit == :all
  ids.take(limit)
end
register_model(model_class) click to toggle source

register an additional ActiveFedora Model to extract ids for

@param [Class] model_class additional class that you would like to be in the output @raise [RegistryError] if the class is not an ActiveFedora based class

# File lib/galter_ir_exporter/migration/survey/fedora_id_service.rb, line 18
def register_model(model_class)
  raise(RegistryError, "Model (#{model_class.name}) for conversion must be an ActiveFedora::Base") unless model_class.ancestors.include?(ActiveFedora::Base)
  return if @model_registry.include? model_class
  @model_registry << model_class
end

Private Instance Methods

active_fedora_model(id) click to toggle source
# File lib/galter_ir_exporter/migration/survey/fedora_id_service.rb, line 48
def active_fedora_model(id)
  query = 'id:"' + id + '"'
  matches = ActiveFedora::SolrService.query(query)
  return nil if matches.count == 0
  model_str = matches.first["has_model_ssim"]
  model_str = model_str.first if model_str.is_a?(Array)
  if model_str.blank? || !Object.const_defined?(model_str)
    Rails.logger.error("Invalid model #{id} #{model_str}")
    return nil
  end
  Object.const_get(model_str)
end
all_ids() click to toggle source
# File lib/galter_ir_exporter/migration/survey/fedora_id_service.rb, line 39
def all_ids
  root_uri = ActiveFedora.fedora.host + ActiveFedora.fedora.base_path
  # Fetches all the Fedora 4 descendant URIs for a given URI.
  # Stolen from: https://github.com/projecthydra/active_fedora/blob/master/lib/active_fedora/indexing.rb#L72-L79
  resource = Ldp::Resource::RdfSource.new(ActiveFedora.fedora.connection, root_uri)
  children = resource.graph.query(predicate: ::RDF::Vocab::LDP.contains).map { |descendant| descendant.object.to_s }
  children.map { |uri| uri.split("/").last }
end
default_registry() click to toggle source
# File lib/galter_ir_exporter/migration/survey/fedora_id_service.rb, line 35
def default_registry
  [::GenericFile, ::Collection]
end
registered_model?(id) click to toggle source
# File lib/galter_ir_exporter/migration/survey/fedora_id_service.rb, line 61
def registered_model?(id)
  model_registry.include?(active_fedora_model(id))
end