Sha256: 30e2e8cf93cb2d747b5523b9cefe71bb1164ba23d26730e4054ce9387555608e

Contents?: true

Size: 1.61 KB

Versions: 22

Compression:

Stored size: 1.61 KB

Contents

SOLR_DOCUMENT_ID = "id" unless defined?(SOLR_DOCUMENT_ID)

module ActiveFedora
  # = ActiveFedora
  # This module mixes various methods into the including class,
  # much in the way ActiveRecord does.  
  module Model 
    # Takes a Fedora URI for a cModel and returns classname, namespace
    def self.classname_from_uri(uri)
      local_path = uri.split('/')[1]
      parts = local_path.split(':')
      return parts[-1].split(/_/).map(&:camelize).join('::'), parts[0]
    end

    # Takes a Fedora URI for a cModel, and returns a 
    # corresponding Model if available
    # This method should reverse ClassMethods#to_class_uri
    # @return [Class, False] the class of the model or false, if it does not exist
    def self.from_class_uri(uri)
      model_value, pid_ns = classname_from_uri(uri)
      raise "model URI incorrectly formatted: #{uri}" unless model_value

      unless class_exists?(model_value)
        ActiveFedora::Base.logger.warn "#{model_value} is not a real class" if ActiveFedora::Base.logger
        return false
      end
      result = ActiveFedora.class_from_string(model_value)
      unless result.nil?
        model_ns = (result.respond_to? :pid_namespace) ? result.pid_namespace : ContentModel::CMODEL_NAMESPACE
        if model_ns != pid_ns
          ActiveFedora::Base.logger.warn "Model class namespace '#{model_ns}' does not match uri: '#{uri}'" if ActiveFedora::Base.logger
        end
      end
      result
    end

    private 
    
    def self.class_exists?(class_name)
      klass = class_name.constantize
      return klass.is_a?(Class)
    rescue NameError
      return false
    end
    
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
active-fedora-8.7.0 lib/active_fedora/model.rb
active-fedora-8.6.0 lib/active_fedora/model.rb
active-fedora-8.5.0 lib/active_fedora/model.rb
active-fedora-8.4.2 lib/active_fedora/model.rb
active-fedora-8.4.1 lib/active_fedora/model.rb
active-fedora-7.3.1 lib/active_fedora/model.rb
active-fedora-7.3.0 lib/active_fedora/model.rb
active-fedora-8.4.0 lib/active_fedora/model.rb
active-fedora-8.3.0 lib/active_fedora/model.rb
active-fedora-8.2.2 lib/active_fedora/model.rb
active-fedora-8.2.1 lib/active_fedora/model.rb
active-fedora-8.2.0 lib/active_fedora/model.rb
active-fedora-7.2.0 lib/active_fedora/model.rb
active-fedora-8.1.0 lib/active_fedora/model.rb
active-fedora-8.0.1 lib/active_fedora/model.rb
active-fedora-8.0.0 lib/active_fedora/model.rb
active-fedora-8.0.0.rc3 lib/active_fedora/model.rb
active-fedora-8.0.0.rc2 lib/active_fedora/model.rb
active-fedora-8.0.0.rc1 lib/active_fedora/model.rb
active-fedora-7.1.2 lib/active_fedora/model.rb