Class: ActiveFedora::File
- Inherits:
-
Object
- Object
- ActiveFedora::File
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.
Defined Under Namespace
Modules: Attributes, Streaming
Constant Summary
AttributeMethods::AttrNames
Instance Attribute Summary
Attributes included from Attributes
#mime_type
Instance Method Summary
collapse
Methods included from Querying
default_sort_params, extended
#id, #id=, #pid, #uri
#[], #[]=, #attribute_names, #attributes
#create_version, #has_versions?, #model_type, #restore_version, #versions
#delete, #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.
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
59
60
|
# File 'lib/active_fedora/file.rb', line 34
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
yield self if block_given?
end
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.
64
65
66
67
68
69
|
# File 'lib/active_fedora/file.rb', line 64
def ==(other)
super ||
other.instance_of?(self.class) &&
uri.value.present? &&
other.uri == uri
end
|
#attribute_will_change!(attr) ⇒ Object
123
124
125
126
127
128
129
|
# File 'lib/active_fedora/file.rb', line 123
def attribute_will_change!(attr)
if attr == 'content'
changed_attributes['content'] = true
else
super
end
end
|
#changed? ⇒ Boolean
149
150
151
|
# File 'lib/active_fedora/file.rb', line 149
def changed?
super || content_changed?
end
|
#check_fixity ⇒ Object
115
116
117
|
# File 'lib/active_fedora/file.rb', line 115
def check_fixity
FixityService.new(@ldp_source.subject).check
end
|
#content ⇒ Object
185
186
187
|
# File 'lib/active_fedora/file.rb', line 185
def content
local_or_remote_content(true)
end
|
#content=(string_or_io) ⇒ Object
180
181
182
183
|
# File 'lib/active_fedora/file.rb', line 180
def content=(string_or_io)
content_will_change! unless @content == string_or_io
@content = string_or_io
end
|
#content_changed? ⇒ Boolean
144
145
146
147
|
# File 'lib/active_fedora/file.rb', line 144
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
119
120
121
|
# File 'lib/active_fedora/file.rb', line 119
def datastream_will_change!
attribute_will_change! :profile
end
|
#described_by ⇒ Object
75
76
77
78
|
# File 'lib/active_fedora/file.rb', line 75
def described_by
raise "#{self} isn't persisted yet" if new_record?
links['describedby'].first
end
|
#destroy ⇒ Object
193
194
195
|
# File 'lib/active_fedora/file.rb', line 193
def destroy(*)
run_callbacks(:destroy) { super }
end
|
#exists! ⇒ Object
If we know the record to exist (parent has LDP:contains), we can avoid unnecessary HEAD requests
95
96
97
|
# File 'lib/active_fedora/file.rb', line 95
def exists!
@exists = true
end
|
#freeze ⇒ Object
Freeze datastreams such that they can be loaded from Fedora, but can’t be changed
164
165
166
|
# File 'lib/active_fedora/file.rb', line 164
def freeze
@frozen = true
end
|
#frozen? ⇒ Boolean
168
169
170
|
# File 'lib/active_fedora/file.rb', line 168
def frozen?
@frozen.present?
end
|
#inspect ⇒ Object
153
154
155
|
# File 'lib/active_fedora/file.rb', line 153
def inspect
"#<#{self.class} uri=\"#{uri}\" >"
end
|
#ldp_connection ⇒ Object
80
81
82
|
# File 'lib/active_fedora/file.rb', line 80
def ldp_connection
ActiveFedora.fedora.connection
end
|
#ldp_source ⇒ Object
71
72
73
|
# File 'lib/active_fedora/file.rb', line 71
def ldp_source
@ldp_source || raise("NO source")
end
|
This method is abstract.
Override this in your concrete datastream class.
Returns does this datastream contain metadata (not file data).
159
160
161
|
# File 'lib/active_fedora/file.rb', line 159
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.
86
87
88
|
# File 'lib/active_fedora/file.rb', line 86
def new_record?
!@exists && ldp_source.new?
end
|
#readonly? ⇒ Boolean
189
190
191
|
# File 'lib/active_fedora/file.rb', line 189
def readonly?
false
end
|
#refresh ⇒ Object
105
106
107
108
109
110
111
112
113
|
# File 'lib/active_fedora/file.rb', line 105
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
|
#reload ⇒ Object
When restoring from previous versions, we need to reload certain attributes from Fedora
100
101
102
103
|
# File 'lib/active_fedora/file.rb', line 100
def reload
return if new_record?
refresh
end
|
#remote_content ⇒ Object
131
132
133
134
|
# File 'lib/active_fedora/file.rb', line 131
def remote_content
return if new_record?
@ds_content ||= retrieve_content
end
|
#serialize! ⇒ Object
serializes any changed data into the content field
173
174
|
# File 'lib/active_fedora/file.rb', line 173
def serialize!
end
|
#to_solr(solr_doc = {}, _opts = {}) ⇒ Object
176
177
178
|
# File 'lib/active_fedora/file.rb', line 176
def to_solr(solr_doc = {}, _opts = {})
solr_doc
end
|
#uri=(uri) ⇒ Object
90
91
92
|
# File 'lib/active_fedora/file.rb', line 90
def uri=(uri)
@ldp_source = build_ldp_resource_via_uri(uri)
end
|