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.

Direct Known Subclasses

NomDatastream, OmDatastream, RDFDatastream

Defined Under Namespace

Modules: Attributes, Streaming

Constant Summary

Constants included from AttributeMethods

AttributeMethods::AttrNames, AttributeMethods::BLACKLISTED_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

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

Methods included from Streaming

#headers, #stream

Methods included from Attributes

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

Methods included from Common

#<=>, #==, #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
# File 'lib/active_fedora/file.rb', line 32

def initialize(identifier = nil, &_block)
  identifier = identifier.delete(:id) if identifier.is_a? Hash
  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 #{uri.class}"
    end

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

Instance Method Details

#attribute_will_change!(attr) ⇒ Object



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

def attribute_will_change!(attr)
  return super unless attr == 'content'
  changed_attributes['content'] = true
end

#changed?Boolean

Returns:

  • (Boolean)


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

def changed?
  super || content_changed? || 
end

#check_fixityObject



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

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

#checksumObject



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

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

#contentObject



154
155
156
# File 'lib/active_fedora/file.rb', line 154

def content
  local_or_remote_content(true)
end

#content=(string_or_io) ⇒ Object



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

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

#content_changed?Boolean

Returns:

  • (Boolean)


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

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

#datastream_will_change!Object



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

def datastream_will_change!
  attribute_will_change! :profile
end

#described_byObject



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

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



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

def exists!
  @exists = true
end

#inspectObject



131
132
133
# File 'lib/active_fedora/file.rb', line 131

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

#ldp_connectionObject



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

def ldp_connection
  ActiveFedora.fedora.connection
end

#metadataObject



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

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)



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

def metadata?
  false
end

#metadata_changed?Boolean

Returns:

  • (Boolean)


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

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)


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

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

#refreshObject



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

def refresh
  @ldp_source = build_ldp_resource_via_uri(uri)
  @original_name = nil
  @mime_type = nil
  @content = nil
  @metadata = nil
  @ds_content = nil
  changed_attributes.clear
end

#reloadObject

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



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

def reload
  return if new_record?
  refresh
end

#remote_contentObject



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

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

#serialize!Object

serializes any changed data into the content field



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

def serialize!
end

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



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

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

#uri=(uri) ⇒ Object



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

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