Class: ActiveFedora::File

Inherits:
Object
  • Object
show all
Extended by:
Querying, ActiveModel::Callbacks, ActiveSupport::Autoload, ActiveTriples::Properties, Deprecation
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

Datastream, 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 Attribute Summary

Attributes included from Attributes

#mime_type

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=, #pid, #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

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

Methods included from Streaming

#headers, #stream

Methods included from Attributes

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

Methods included from Common

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

Constructor Details

#initialize(parent_or_url_or_hash = nil, path = nil, _options = {}) {|self| ... } ⇒ File

Returns a new instance of File.

Parameters:

  • parent_or_url_or_hash (ActiveFedora::Base, RDF::URI, String, Hash, NilClass) (defaults to: nil)

    the parent resource or the URI of this resource

  • path (String) (defaults to: nil)

    the path partial relative to the resource

  • _options (Hash) (defaults to: {})

Yields:

  • (self)

    Yields self

Yield Parameters:

  • self (File)

    the newly created file



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_fedora/file.rb', line 37

def initialize(parent_or_url_or_hash = nil, path = nil, _options = {}, &_block)
  run_callbacks(:initialize) do
    case parent_or_url_or_hash
    when Hash
      @ldp_source = build_ldp_resource_via_uri
    when nil
      @ldp_source = build_ldp_resource_via_uri nil
    when String, ::RDF::URI
      id = ActiveFedora::Associations::IDComposite.new([parent_or_url_or_hash], translate_uri_to_id).first
      @ldp_source = build_ldp_resource id
    when ActiveFedora::Base
      Deprecation.warn File, "Initializing a file by passing a container is deprecated. Initialize with a uri instead. This capability will be removed in active-fedora 10.0"
      uri = if parent_or_url_or_hash.uri.is_a?(::RDF::URI) && parent_or_url_or_hash.uri.value.empty?
              nil
            else
              "#{parent_or_url_or_hash.uri}/#{path}"
            end
      @ldp_source = build_ldp_resource_via_uri(uri, nil)

    else
      raise "The first argument to #{self} must be a String or an ActiveFedora::Base. You provided a #{parent_or_url_or_hash.class}"
    end

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

Instance Method Details

#attribute_will_change!(attr) ⇒ Object



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

def attribute_will_change!(attr)
  if attr == 'content'
    changed_attributes['content'] = true
  else
    super
  end
end

#changed?Boolean

Returns:

  • (Boolean)


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

def changed?
  super || content_changed?
end

#check_fixityObject



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

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

#checksumObject



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

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

#contentObject



167
168
169
# File 'lib/active_fedora/file.rb', line 167

def content
  local_or_remote_content(true)
end

#content=(string_or_io) ⇒ Object



162
163
164
165
# File 'lib/active_fedora/file.rb', line 162

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

#content_changed?Boolean

Returns:

  • (Boolean)


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

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



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

def datastream_will_change!
  attribute_will_change! :profile
end

#described_byObject



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

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



86
87
88
# File 'lib/active_fedora/file.rb', line 86

def exists!
  @exists = true
end

#inspectObject



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

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

#ldp_connectionObject



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

def ldp_connection
  ActiveFedora.fedora.connection
end

#metadataObject



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

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)



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

def metadata?
  false
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)


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

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

#refreshObject



96
97
98
99
100
101
102
103
104
# File 'lib/active_fedora/file.rb', line 96

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



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

def reload
  return if new_record?
  refresh
end

#remote_contentObject



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

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

#serialize!Object

serializes any changed data into the content field



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

def serialize!
end

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



158
159
160
# File 'lib/active_fedora/file.rb', line 158

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

#uri=(uri) ⇒ Object



81
82
83
# File 'lib/active_fedora/file.rb', line 81

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