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.
Class Method Summary collapse
-
.classname_from_uri(uri) ⇒ Object
Takes a Fedora URI for a cModel and returns classname, namespace.
-
.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.
Instance Method Summary collapse
-
#to_class_uri(attrs = {}) ⇒ Object
Returns a suitable uri object for :has_model Should reverse Model#from_class_uri.
Class Method Details
.classname_from_uri(uri) ⇒ Object
Takes a Fedora URI for a cModel and returns classname, namespace
9 10 11 12 13 |
# File 'lib/active_fedora/model.rb', line 9 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
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/active_fedora/model.rb', line 19 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 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 logger.warn "Model class namespace '#{model_ns}' does not match uri: '#{uri}'" end end result end |
Instance Method Details
#to_class_uri(attrs = {}) ⇒ Object
Returns a suitable uri object for :has_model Should reverse Model#from_class_uri
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/active_fedora/model.rb', line 39 def to_class_uri(attrs = {}) if self.respond_to? :pid_suffix pid_suffix = self.pid_suffix else pid_suffix = attrs.fetch(:pid_suffix, ContentModel::CMODEL_PID_SUFFIX) end if self.respond_to? :pid_namespace namespace = self.pid_namespace else namespace = attrs.fetch(:namespace, ContentModel::CMODEL_NAMESPACE) end "info:fedora/#{namespace}:#{ContentModel.sanitized_class_name(self)}#{pid_suffix}" end |