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

#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) (defaults to: {})

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
# 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, String, ::RDF::URI
    @ldp_source = build_ldp_resource_via_uri parent_or_url_or_hash
  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.kind_of?(::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

#==(comparison_object) ⇒ 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



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

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

#attribute_will_change!(attr) ⇒ Object



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

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

#changed?Boolean

Returns:

  • (Boolean)


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

def changed?
  super || content_changed?
end

#check_fixityObject



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

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

#contentObject



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

def content
  local_or_remote_content(true)
end

#content=(string_or_io) ⇒ Object



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

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

#content_changed?Boolean

Returns:

  • (Boolean)


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

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

#datastream_will_change!Object



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

def datastream_will_change!
  attribute_will_change! :profile
end

#described_byObject



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

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



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

def exists!
  @exists = true
end

#freezeObject

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



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

def freeze
  @frozen = true
end

#frozen?Boolean

Returns:

  • (Boolean)


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

def frozen?
  !!@frozen
end

#inspectObject



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

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

#ldp_connectionObject



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

def ldp_connection
  ActiveFedora.fedora.connection
end

#ldp_sourceObject



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

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

#metadataObject



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

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)



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

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)


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

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

#readonly?Boolean

Returns:

  • (Boolean)


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

def readonly?
  false
end

#refreshObject



100
101
102
103
104
105
106
107
# File 'lib/active_fedora/file.rb', line 100

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

#reloadObject

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



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

def reload
  return if new_record?
  refresh
end

#remote_contentObject



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

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

#serialize!Object

serializes any changed data into the content field



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

def serialize!
end

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



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

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

#uri=(uri) ⇒ Object



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

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