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 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!, #update

Methods included from Streaming

#headers, #stream

Methods included from 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(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



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

def attribute_will_change!(attr)
  if attr == 'content'
    changed_attributes['content'] = true
  elsif attr == 'type'
  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



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

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



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

def content
  local_or_remote_content(true)
end

#content=(string_or_io) ⇒ Object



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

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



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

def datastream_will_change!
  attribute_will_change! :profile
end

#described_byObject



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

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



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

def exists!
  @exists = true
end

#inspectObject



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

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

#ldp_connectionObject



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

def ldp_connection
  ActiveFedora.fedora.connection
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)



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

def metadata?
  false
end

#metadata_changed?Boolean

Returns:

  • (Boolean)


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

def 
  if new_record? || links['describedby'].blank?
    false
  else
    .changed?
  end
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)


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

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

#refreshObject



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

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



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

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

#saveObject



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

def save
  super.tap do
    .save if .changed?
  end
end

#serialize!Object

serializes any changed data into the content field



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

def serialize!
end

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



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

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

#uri=(uri) ⇒ Object



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

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