Class: ActiveFedora::File

Inherits:
Object
  • Object
show all
Extended by:
Querying, ActiveModel::Callbacks, ActiveSupport::Autoload, ActiveTriples::Properties
Includes:
AttributeMethods, Callbacks, Common, Attributes, Streaming, FilePersistence, Identifiable, Inheritance, Scoping, Versionable, ActiveModel::Dirty
Defined in:
lib/active_fedora/file.rb

Overview

An LDP NonRDFSource. The base class for a bytestream stored in the repository.

Defined Under Namespace

Modules: Attributes, Streaming

Constant Summary

Constants included from AttributeMethods

AttributeMethods::AttrNames, AttributeMethods::RESTRICTED_CLASS_METHODS

Constants included from Callbacks

Callbacks::CALLBACKS

Instance Method Summary collapse

Methods included from Querying

default_sort_params, extended

Methods included from Scoping

#initialize_internals_callback, #populate_with_current_scope_attributes

Methods included from Identifiable

#id, #id=, #uri

Methods included from AttributeMethods

#[], #[]=, #attribute_for_inspect, #attribute_names, #attribute_present?, #attributes, #has_attribute?

Methods included from Callbacks

#destroy, #update_index

Methods included from Versionable

#create_version, #has_versions?, #model_type, #restore_version, #versions

Methods included from Persistence

#base_path_for_resource=, #delete, #destroy, #destroy!, #destroyed?, #eradicate, #persisted?, #save, #save!, #update, #update!

Methods included from Streaming

#headers, #stream

Methods included from Attributes

#assign_attributes, #create_date, #digest, #dirty_size, #empty?, #has_content?, #mime_type, #modified_date, #original_name, #original_name=, #persisted_size, #size

Methods included from Common

#<=>, #==, #etag, #freeze, #frozen?, #ldp_source, #readonly!, #readonly?

Constructor Details

#initialize(identifier = nil) {|self| ... } ⇒ File

Returns a new instance of File.

Parameters:

  • identifier (Hash, RDF::URI, String, NilClass) (defaults to: nil)

    the id (path) or URI of this resource. The hash gets passed when calling Reflection#build_association, but currently we don’t do anything with it.

Yields:

  • (self)

    Yields self

Yield Parameters:

  • self (File)

    the newly created file



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/active_fedora/file.rb', line 32

def initialize(identifier = nil, &_block)
  identifier = identifier.delete(:id) if identifier.is_a? Hash
  identifier = identifier.uri if identifier.respond_to? :uri
  run_callbacks(:initialize) do
    case identifier
    when nil, ::RDF::URI
      @ldp_source = build_ldp_resource_via_uri identifier
    when String
      id = ActiveFedora::Associations::IDComposite.new([identifier], translate_uri_to_id).first
      @ldp_source = build_ldp_resource id
    else
      raise "The first argument to #{self} must be a Hash, String or RDF::URI. You provided a #{identifier.class}"
    end

    @local_attributes = {}.with_indifferent_access
    @readonly = false
    yield self if block_given?
  end
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/active_fedora/file.rb', line 123

def changed?
  super || content_changed? || 
end

#check_fixityObject



92
93
94
# File 'lib/active_fedora/file.rb', line 92

def check_fixity
  FixityService.new(@ldp_source.subject).check
end

#checksumObject



109
110
111
# File 'lib/active_fedora/file.rb', line 109

def checksum
  ActiveFedora::Checksum.new(self)
end

#contentObject



149
150
151
# File 'lib/active_fedora/file.rb', line 149

def content
  local_or_remote_content(true)
end

#content=(string_or_io) ⇒ Object



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

def content=(string_or_io)
  content_will_change! unless @content == string_or_io
  @content = string_or_io
end

#content_changed?Boolean

Returns:

  • (Boolean)


113
114
115
116
# File 'lib/active_fedora/file.rb', line 113

def content_changed?
  return true if new_record? && local_or_remote_content(false).present?
  local_or_remote_content(false) != @ds_content
end

#datastream_will_change!Object



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

def datastream_will_change!
  attribute_will_change! :ldp_source
end

#described_byObject



52
53
54
55
# File 'lib/active_fedora/file.rb', line 52

def described_by
  raise "#{self} isn't persisted yet" if new_record?
  links['describedby'].first
end

#exists!Object

If we know the record to exist (parent has LDP:contains), we can avoid unnecessary HEAD requests



72
73
74
# File 'lib/active_fedora/file.rb', line 72

def exists!
  @exists = true
end

#inspectObject



127
128
129
# File 'lib/active_fedora/file.rb', line 127

def inspect
  "#<#{self.class} uri=\"#{uri}\" >"
end

#ldp_connectionObject



57
58
59
# File 'lib/active_fedora/file.rb', line 57

def ldp_connection
  ActiveFedora.fedora.connection
end

#metadataObject



105
106
107
# File 'lib/active_fedora/file.rb', line 105

def 
  @metadata ||= ActiveFedora::WithMetadata::MetadataNode.new(self)
end

#metadata?boolean

This method is abstract.

Override this in your concrete datastream class.

Returns does this datastream contain metadata (not file data).

Returns:

  • (boolean)

    does this datastream contain metadata (not file data)



133
134
135
# File 'lib/active_fedora/file.rb', line 133

def metadata?
  false
end

#metadata_changed?Boolean

Returns:

  • (Boolean)


118
119
120
121
# File 'lib/active_fedora/file.rb', line 118

def 
  return false if new_record? || links['describedby'].blank?
  .changed?
end

#new_record?Boolean

If this file has a parent with ldp#contains, we know it is not new. By tracking exists we prevent an unnecessary HEAD request.

Returns:

  • (Boolean)


63
64
65
# File 'lib/active_fedora/file.rb', line 63

def new_record?
  !@exists && ldp_source.new?
end

#refreshObject



82
83
84
85
86
87
88
89
90
# File 'lib/active_fedora/file.rb', line 82

def refresh
  @ldp_source = build_ldp_resource_via_uri(uri)
  @original_name = nil
  @mime_type = nil
  @content = nil
  @metadata = nil
  @ds_content = nil
  clear_attribute_changes(changes.keys)
end

#reloadObject

When restoring from previous versions, we need to reload certain attributes from Fedora



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

def reload
  return if new_record?
  refresh
end

#remote_contentObject



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

def remote_content
  return if new_record?
  @ds_content ||= retrieve_content
end

#serialize!Object

serializes any changed data into the content field



138
# File 'lib/active_fedora/file.rb', line 138

def serialize!; end

#to_solr(solr_doc = {}, _opts = {}) ⇒ Object



140
141
142
# File 'lib/active_fedora/file.rb', line 140

def to_solr(solr_doc = {}, _opts = {})
  solr_doc
end

#uri=(uri) ⇒ Object



67
68
69
# File 'lib/active_fedora/file.rb', line 67

def uri=(uri)
  @ldp_source = build_ldp_resource_via_uri(uri)
end