module ActiveFedora::Core

Public Class Methods

new(attributes = nil) { |self| ... } click to toggle source

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

# File lib/active_fedora/core.rb, line 31
def initialize(attributes = nil, &_block)
  init_internals
  attributes = attributes.dup if attributes # can't dup nil in Ruby 2.3
  id = attributes && (attributes.delete(:id) || attributes.delete('id'))
  @ldp_source = build_ldp_resource(id)
  raise IllegalOperation, "Attempting to recreate existing ldp_source: `#{ldp_source.subject}'" unless ldp_source.new?
  assign_attributes(attributes) if attributes
  assert_content_model
  load_attached_files

  yield self if block_given?
  _run_initialize_callbacks
end

Public Instance Methods

freeze() click to toggle source
# File lib/active_fedora/core.rb, line 86
def freeze
  @resource.freeze
  # @attributes = @attributes.clone.freeze
  attached_files.freeze
  self
end
init_with_resource(rdf_resource) click to toggle source

Initialize an empty model object and set its resource example:

class Post < ActiveFedora::Base
end

post = Post.allocate
post.init_with_resource(Ldp::Resource.new('http://example.com/post/1'))
post.title # => 'hello world'
# File lib/active_fedora/core.rb, line 77
def init_with_resource(rdf_resource)
  init_internals
  @ldp_source = rdf_resource
  load_attached_files
  run_callbacks :find
  run_callbacks :initialize
  self
end
inspect() click to toggle source

Returns the contents of the record as a nicely formatted string.

# File lib/active_fedora/core.rb, line 96
def inspect
  inspection = ["id: #{id.inspect}"]
  inspection += self.class.attribute_names.collect do |name|
    "#{name}: #{attribute_for_inspect(name)}" if has_attribute?(name)
  end

  "#<#{self.class} #{inspection.compact.join(', ')}>"
end
logger() click to toggle source

All instances default to the class-level logger

# File lib/active_fedora/core.rb, line 21
def logger
  self.class.logger
end
reload() click to toggle source

Reloads the object from Fedora.

# File lib/active_fedora/core.rb, line 56
def reload
  check_persistence unless persisted?
  clear_association_cache
  clear_attached_files
  refresh
  load_attached_files
  self
rescue Ldp::Gone => err
  logger.error("Tried to reload an object that has been destroyed.\n\t#{err.message}")
  raise ActiveFedora::ObjectNotFoundError
end
uri=(uri) click to toggle source

@param [#to_s] uri a full fedora URI or relative ID to set this resource

to.

@note This can only be run on an unpersisted resource.

# File lib/active_fedora/core.rb, line 49
def uri=(uri)
  raise AlreadyPersistedError, "You can not set a URI for a persisted ActiveFedora object." if persisted?

  @ldp_source = build_ldp_resource(self.class.uri_to_id(uri))
end

Protected Instance Methods

assert_content_model() click to toggle source

This method is normally called once in the lifecycle, by create#

# File lib/active_fedora/core.rb, line 108
def assert_content_model
  self.has_model = self.class.to_rdf_representation
end

Private Instance Methods

build_ldp_resource(id = nil) click to toggle source
# File lib/active_fedora/core.rb, line 153
def build_ldp_resource(id = nil)
  ActiveFedora.fedora.ldp_resource_service.build(self.class, id)
end
check_persistence() click to toggle source
# File lib/active_fedora/core.rb, line 157
def check_persistence
  raise ActiveFedora::ObjectNotFoundError, "Can't reload an object that has been destroyed" if destroyed?
  raise ActiveFedora::ObjectNotFoundError, "Can't reload an object that hasn't been saved"
end
init_internals() click to toggle source
# File lib/active_fedora/core.rb, line 147
def init_internals
  @resource          = nil
  @readonly          = false
  @association_cache = {}
end