Class: ActiveFedora::File

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

Instance Attribute Summary

Attributes included from Attributes

#mime_type

Instance Method Summary collapse

Methods included from Querying

default_sort_params, extended

Methods included from Identifiable

#id, #id=, #pid, #uri

Methods included from AttributeMethods

#[], #[]=, #attribute_names, #attributes

Methods included from Versionable

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

Methods included from Persistence

#delete, #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

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)

Yields:

  • (self)

    Yields self

Yield Parameters:

  • self (File)

    the newly created file



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_fedora/file.rb', line 34

def initialize(parent_or_url_or_hash = nil, path = nil, _options = {}, &_block)
  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
  yield self if block_given?
end

Instance Method Details

#==(other) ⇒ true, false

Returns true if the objects are equal or when the objects have uris and the uris are equal.

Returns:

  • (true, false)

    true if the objects are equal or when the objects have uris and the uris are equal



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

def ==(other)
  super ||
    other.instance_of?(self.class) &&
      uri.value.present? &&
      other.uri == uri
end

#attribute_will_change!(attr) ⇒ Object



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

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

#changed?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/active_fedora/file.rb', line 147

def changed?
  super || content_changed?
end

#check_fixityObject



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

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

#checksumObject



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

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

#contentObject



183
184
185
# File 'lib/active_fedora/file.rb', line 183

def content
  local_or_remote_content(true)
end

#content=(string_or_io) ⇒ Object



178
179
180
181
# File 'lib/active_fedora/file.rb', line 178

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

#content_changed?Boolean

Returns:

  • (Boolean)


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

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



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

def datastream_will_change!
  attribute_will_change! :profile
end

#described_byObject



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

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



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

def exists!
  @exists = true
end

#freezeObject

Freeze datastreams such that they can be loaded from Fedora, but can’t be changed



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

def freeze
  @frozen = true
end

#frozen?Boolean

Returns:

  • (Boolean)


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

def frozen?
  @frozen.present?
end

#inspectObject



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

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

#ldp_connectionObject



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

def ldp_connection
  ActiveFedora.fedora.connection
end

#ldp_sourceObject



69
70
71
# File 'lib/active_fedora/file.rb', line 69

def ldp_source
  @ldp_source || raise("NO source")
end

#metadataObject



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

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)



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

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)


84
85
86
# File 'lib/active_fedora/file.rb', line 84

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

#readonly?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/active_fedora/file.rb', line 187

def readonly?
  false
end

#refreshObject



103
104
105
106
107
108
109
110
111
# File 'lib/active_fedora/file.rb', line 103

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



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

def reload
  return if new_record?
  refresh
end

#remote_contentObject



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

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

#serialize!Object

serializes any changed data into the content field



171
172
# File 'lib/active_fedora/file.rb', line 171

def serialize!
end

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



174
175
176
# File 'lib/active_fedora/file.rb', line 174

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

#uri=(uri) ⇒ Object



88
89
90
# File 'lib/active_fedora/file.rb', line 88

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