Module: ActiveFedora::Core

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/active_fedora/core.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#inner_objectObject (readonly)

Returns the value of attribute inner_object.



5
6
7
# File 'lib/active_fedora/core.rb', line 5

def inner_object
  @inner_object
end

Instance Method Details

#==(comparison_object) ⇒ Object



70
71
72
73
74
75
# File 'lib/active_fedora/core.rb', line 70

def ==(comparison_object)
     comparison_object.equal?(self) ||
       (comparison_object.instance_of?(self.class) &&
         comparison_object.pid == pid &&
         !comparison_object.new_record?)
end

#adapt_to(klass) ⇒ Object

This method adapts the inner_object to a new ActiveFedora::Base implementation This is intended to minimize redundant interactions with Fedora



115
116
117
118
119
120
# File 'lib/active_fedora/core.rb', line 115

def adapt_to(klass)
  unless klass.ancestors.include? ActiveFedora::Base
    raise "Cannot adapt #{self.class.name} to #{klass.name}: Not a ActiveFedora::Base subclass"
  end
  klass.allocate.init_with_object(inner_object)
end

#adapt_to_cmodelObject

Adapts the inner object to the best known model Raise ActiveFedora::ModelNotAsserted if unable to adapt object (i.e, best model is nil)



125
126
127
128
129
130
131
132
133
# File 'lib/active_fedora/core.rb', line 125

def adapt_to_cmodel
  best_model_match = ActiveFedora::ContentModel.best_model_for(self)

  if best_model_match.nil?
    raise ActiveFedora::ModelNotAsserted, "Unable to adapt #{self.inspect} to a model asserted for the resource."
  end

  self.instance_of?(best_model_match) ? self : self.adapt_to(best_model_match)
end

#cloneObject



77
78
79
80
# File 'lib/active_fedora/core.rb', line 77

def clone
  new_object = self.class.create
  clone_into(new_object)
end

#clone_into(new_object) ⇒ Object

Clone the datastreams from this object into the provided object, while preserving the pid of the provided object

Parameters:

  • new_object (Base)

    clone into this object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/active_fedora/core.rb', line 84

def clone_into(new_object)
  rels = Nokogiri::XML( rels_ext.content)
  rels.xpath("//rdf:Description/@rdf:about").first.value = new_object.internal_uri
  new_object.rels_ext.content = rels.to_xml

  datastreams.each do |k, v|
    next if k == 'RELS-EXT'
    new_object.datastreams[k].content = v.content
  end
  new_object if new_object.save
end

#freezeObject



96
97
98
99
# File 'lib/active_fedora/core.rb', line 96

def freeze
  datastreams.freeze
  self
end

#frozen?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/active_fedora/core.rb', line 101

def frozen?
  datastreams.frozen?
end

#init_with_object(inner_obj) ⇒ Object

Initialize an empty model object and set the inner_obj example:

class Post < ActiveFedora::Base
   :name => "properties", :type => ActiveFedora::SimpleDatastream
end

post = Post.allocate
post.init_with_object(DigitalObject.find(pid))
post.properties.title # => 'hello world'


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_fedora/core.rb', line 51

def init_with_object(inner_obj)
  @association_cache = {}
  @inner_object = inner_obj
  unless @inner_object.is_a? SolrDigitalObject
    @inner_object.original_class = self.class
    ## Replace existing unchanged datastreams with the definitions found in this class if they have a different type.
    ## Any datastream that is deleted here will cause a reload from fedora, so avoid it whenever possible
    ds_specs.keys.each do |key|
      if @inner_object.datastreams[key] != nil && !@inner_object.datastreams[key].content_changed? && @inner_object.datastreams[key].class != self.class.ds_specs[key][:type]
        @inner_object.datastreams.delete(key)
      end
    end
  end
  load_datastreams
  run_callbacks :find
  run_callbacks :initialize
  self
end

#initialize(attrs = nil) ⇒ Object

Constructor. You may supply a custom :pid, or we call the Fedora Rest API for the next available Fedora pid, and mark as new object. Also, if attrs does not contain :pid but does contain :namespace it will pass the :namespace value to Fedora::Repository.nextid to generate the next pid available within the given namespace.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_fedora/core.rb', line 20

def initialize(attrs = nil)
  attrs = {} if attrs.nil?
  @association_cache = {}
  attributes = attrs.dup
  @inner_object = UnsavedDigitalObject.new(self.class, attributes.delete(:namespace), attributes.delete(:pid))
  self.relationships_loaded = true
  load_datastreams

  [:new_object,:create_date, :modified_date].each { |k| attributes.delete(k)}
  self.attributes=attributes
  run_callbacks :initialize
end

#pretty_pidObject



105
106
107
108
109
110
111
# File 'lib/active_fedora/core.rb', line 105

def pretty_pid
  if self.pid == UnsavedDigitalObject::PLACEHOLDER
    nil
  else
    self.pid
  end
end

#reifyObject

** EXPERIMENTAL ** This method returns a new object of the same class, with the internal SolrDigitalObject replaced with an actual DigitalObject.



138
139
140
141
142
143
# File 'lib/active_fedora/core.rb', line 138

def reify
  if self.inner_object.is_a? DigitalObject
    raise "#{self.inspect} is already a full digital object"
  end
  self.class.find self.pid
end

#reify!Object

** EXPERIMENTAL ** This method reinitializes a lightweight, loaded-from-solr object with an actual DigitalObject inside.



148
149
150
151
152
153
# File 'lib/active_fedora/core.rb', line 148

def reify!
  if self.inner_object.is_a? DigitalObject
    raise "#{self.inspect} is already a full digital object"
  end
  self.init_with_object DigitalObject.find(self.class,self.pid)
end

#reloadObject

Reloads the object from Fedora.



34
35
36
37
38
39
# File 'lib/active_fedora/core.rb', line 34

def reload
  raise ActiveFedora::ObjectNotFoundError, "Can't reload an object that hasn't been saved" unless persisted?
  clear_association_cache
  clear_relationships
  init_with_object(self.class.find(self.pid).inner_object)
end