Module: ActiveFedora::Core
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#inner_object ⇒ Object
readonly
Returns the value of attribute inner_object.
Instance Method Summary collapse
- #==(comparison_object) ⇒ Object
-
#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.
-
#adapt_to_cmodel ⇒ Object
Examines the :has_model assertions in the RELS-EXT.
- #clone ⇒ Object
-
#clone_into(new_object) ⇒ Object
Clone the datastreams from this object into the provided object, while preserving the pid of the provided object.
- #freeze ⇒ Object
- #frozen? ⇒ Boolean
-
#init_with(inner_obj) ⇒ Object
Initialize an empty model object and set the
inner_obj
example:. -
#initialize(attrs = nil) ⇒ Object
Constructor.
- #pretty_pid ⇒ Object
-
#reify ⇒ Object
** EXPERIMENTAL ** This method returns a new object of the same class, with the internal SolrDigitalObject replaced with an actual DigitalObject.
-
#reify! ⇒ Object
** EXPERIMENTAL ** This method reinitializes a lightweight, loaded-from-solr object with an actual DigitalObject inside.
-
#reload ⇒ Object
Reloads the object from Fedora.
Instance Attribute Details
#inner_object ⇒ Object (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
62 63 64 65 66 67 |
# File 'lib/active_fedora/core.rb', line 62 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
107 108 109 110 111 112 |
# File 'lib/active_fedora/core.rb', line 107 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(inner_object) end |
#adapt_to_cmodel ⇒ Object
Examines the :has_model assertions in the RELS-EXT.
If the object is of type ActiveFedora::Base, then use the first :has_model in the RELS-EXT for this object. Due to how the RDF writer works, this is currently just the first alphabetical model.
If the object was instantiated with any other class, then use that class as the base of the type the user wants rather than casting to the first :has_model found on the object.
In either case, if an extended model of that initial base model of the two cases above exists in the :has_model, then instantiate as that extended model type instead.
127 128 129 130 131 |
# File 'lib/active_fedora/core.rb', line 127 def adapt_to_cmodel best_model_match = ActiveFedora::ContentModel.best_model_for(self) self.instance_of?(best_model_match) ? self : self.adapt_to(best_model_match) end |
#clone ⇒ Object
69 70 71 72 |
# File 'lib/active_fedora/core.rb', line 69 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
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/active_fedora/core.rb', line 76 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 |
#freeze ⇒ Object
88 89 90 91 |
# File 'lib/active_fedora/core.rb', line 88 def freeze datastreams.freeze self end |
#frozen? ⇒ Boolean
93 94 95 |
# File 'lib/active_fedora/core.rb', line 93 def frozen? datastreams.frozen? end |
#init_with(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(DigitalObject.find(pid))
post.properties.title # => 'hello world'
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/active_fedora/core.rb', line 43 def init_with(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.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/active_fedora/core.rb', line 12 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_pid ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/active_fedora/core.rb', line 97 def pretty_pid if self.pid == UnsavedDigitalObject::PLACEHOLDER nil else self.pid end end |
#reify ⇒ Object
** EXPERIMENTAL ** This method returns a new object of the same class, with the internal SolrDigitalObject replaced with an actual DigitalObject.
136 137 138 139 140 141 |
# File 'lib/active_fedora/core.rb', line 136 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.
146 147 148 149 150 151 |
# File 'lib/active_fedora/core.rb', line 146 def reify! if self.inner_object.is_a? DigitalObject raise "#{self.inspect} is already a full digital object" end self.init_with DigitalObject.find(self.class,self.pid) end |
#reload ⇒ Object
Reloads the object from Fedora.
26 27 28 29 30 31 |
# File 'lib/active_fedora/core.rb', line 26 def reload raise ActiveFedora::ObjectNotFoundError, "Can't reload an object that hasn't been saved" unless persisted? clear_association_cache clear_relationships init_with(self.class.find(self.pid).inner_object) end |