Module: ActiveFedora::Persistence::ClassMethods

Defined in:
lib/active_fedora/persistence.rb

Instance Method Summary collapse

Instance Method Details

#create(attributes = nil, &block) ⇒ Object

Creates an object (or multiple objects) and saves it to the repository, if validations pass. The resulting object is returned whether the object was saved successfully to the repository or not.

The attributes parameter can be either be a Hash or an Array of Hashes. These Hashes describe the attributes on the objects that are to be created.

Examples

# Create a single new object
User.create(:first_name => 'Jamie')

# Create an Array of new objects
User.create([{ :first_name => 'Jamie' }, { :first_name => 'Jeremy' }])

# Create a single object and pass it into a block to set other attributes.
User.create(:first_name => 'Jamie') do |u|
  u.is_admin = false
end

# Creating an Array of new objects using a block, where the block is executed for each object:
User.create([{ :first_name => 'Jamie' }, { :first_name => 'Jeremy' }]) do |u|
  u.is_admin = false
end


131
132
133
134
135
136
137
138
139
# File 'lib/active_fedora/persistence.rb', line 131

def create(attributes = nil, &block)
  if attributes.is_a?(Array)
    attributes.collect { |attr| create(attr, &block) }
  else
    object = new(attributes, &block)
    object.save
    object
  end
end

#eradicate(uri) ⇒ Object

Removes an object’s tombstone so another object with the same uri may be created. NOTE: this is in violation of the linked data platform and is only here as a convience method. It shouldn’t be used in the general course of repository operations.



144
145
146
# File 'lib/active_fedora/persistence.rb', line 144

def eradicate(uri)
  gone?(uri) ? delete_tombstone(uri) : false
end

#gone?(uri) ⇒ Boolean

Allows the user to find out if an id has been used in the system and then been deleted

Parameters:

  • uri

    id in fedora that may or may not have been deleted

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
# File 'lib/active_fedora/persistence.rb', line 150

def gone?(uri)
  ActiveFedora::Base.find(uri)
  false
rescue Ldp::Gone
  true
rescue ActiveFedora::ObjectNotFoundError
  false
end