Module: ActiveFedora::Model

Defined in:
lib/active_fedora/model.rb

Overview

ActiveFedora

This module mixes various methods into the including class, much in the way ActiveRecord does.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.classname_from_uri(uri) ⇒ Object

Takes a Fedora URI for a cModel and returns classname, namespace



13
14
15
16
17
# File 'lib/active_fedora/model.rb', line 13

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

.from_class_uri(uri) ⇒ Class, False

Takes a Fedora URI for a cModel, and returns a corresponding Model if available This method should reverse ClassMethods#to_class_uri

Returns:

  • (Class, False)

    the class of the model or false, if it does not exist



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_fedora/model.rb', line 23

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)
    logger.warn "#{model_value} is not a real class"
    return false
  end
  if model_value.include?("::")
    result = eval(model_value)
  else
    result = Kernel.const_get(model_value)
  end
  unless result.nil?
    model_ns = (result.respond_to? :pid_namespace) ? result.pid_namespace : ContentModel::CMODEL_NAMESPACE
    if model_ns != pid_ns
      logger.warn "Model class namespace '#{model_ns}' does not match uri: '#{uri}'"
    end
  end
  result
end

.included(klass) ⇒ Object

:nodoc:



8
9
10
# File 'lib/active_fedora/model.rb', line 8

def self.included(klass) # :nodoc:
  klass.extend(ClassMethods)
end