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
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.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



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

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

#attribute_will_change!(attr) ⇒ Object



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

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

#changed?Boolean

Returns:

  • (Boolean)


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

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

#contentObject



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

def content
  local_or_remote_content(true)
end

#content=(string_or_io) ⇒ Object



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

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

#content_changed?Boolean

Returns:

  • (Boolean)


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

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



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

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



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

def freeze
  @frozen = true
end

#frozen?Boolean

Returns:

  • (Boolean)


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

def frozen?
  !!@frozen
end

#inspectObject



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

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



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

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)



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

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)


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

def readonly?
  false
end

#refreshObject



103
104
105
106
107
108
109
110
# 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
  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



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

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

#serialize!Object

serializes any changed data into the content field



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

def serialize!
end

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



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

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