Module: ActiveFedora::ContentModel

Defined in:
lib/active_fedora/content_model.rb

Constant Summary collapse

CMODEL_NAMESPACE =
"afmodel"
CMODEL_PID_SUFFIX =
""

Class Method Summary collapse

Class Method Details

.best_model_for(obj) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_fedora/content_model.rb', line 16

def self.best_model_for(obj)
  best_model_match = nil

  known_models_for(obj).each do |model_value|
    if model_value <= obj.class
      # If there is an inheritance structure, use the most specific case.
      if best_model_match.nil? || best_model_match > model_value
        best_model_match = model_value
      end
    end
  end

  best_model_match
end

.default_model(obj) ⇒ Object

Returns a ruby class to use if no other class could be find to instantiate Override this method if you need something other than the default strategy



53
54
55
# File 'lib/active_fedora/content_model.rb', line 53

def self.default_model(obj)
  ActiveFedora::Base
end

.known_models_for(obj) ⇒ Object

returns an array of the model classes that are defined in the current application that the given object asserts (ie. if the object asserts a StreamingVideo model but the application doesn’t define a StreamingVideo model, it will be excluded from this list.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_fedora/content_model.rb', line 35

def self.known_models_for(obj)
  models_array = []
  models_asserted_by( obj ).each do |model_uri|
    m = uri_to_model_class(model_uri)
    if m
      models_array << m
    end
  end
  
  if models_array.empty?
    models_array = [default_model(obj)]
  end
  
  return models_array
end

.models_asserted_by(obj) ⇒ Object

list all of the models asserted by the provided object



12
13
14
# File 'lib/active_fedora/content_model.rb', line 12

def self.models_asserted_by(obj)
  obj.relationships(:has_model)
end

.sanitized_class_name(klass) ⇒ Object

Override this, if you prefer your class names serialized some other way



7
8
9
# File 'lib/active_fedora/content_model.rb', line 7

def self.sanitized_class_name(klass)
  klass.name.gsub(/(::)/, '_')
end

.uri_to_model_class(uri) ⇒ Object

Returns an ActiveFedora Model class corresponding to the given uri if one can be found. Returns false if no corresponding model can be found.



60
61
62
63
64
65
66
67
# File 'lib/active_fedora/content_model.rb', line 60

def self.uri_to_model_class( uri )
  rc = Model.from_class_uri(uri)
  if rc && (rc.superclass == ActiveFedora::Base || rc.ancestors.include?(ActiveFedora::Base))
    rc
  else
    false
  end
end