Module: ActiveFedora::AttachedFiles

Extended by:
ActiveSupport::Concern, Deprecation
Included in:
Base
Defined in:
lib/active_fedora/attached_files.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#add_datastream(datastream, opts = {}) ⇒ Object



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

def add_datastream(datastream, opts = {})
  attach_file(datastream, opts)
end

#add_file(file, *args) ⇒ Object

Attach the given stream/string to object

Parameters:

  • file (IO, String)

    the file to add

  • args (Hash)

    options: :dsid, :prefix, :mime_type

  • opts (Hash)

    a customizable set of options



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/active_fedora/attached_files.rb', line 101

def add_file(file, *args)
  opts = if args.size == 1
           args.first
         else
           Deprecation.warn AttachedFiles, "The second option to add_file should be a hash. Passing the file path is deprecated and will be removed in active-fedora 10.0.", caller
           { path: args[0], original_name: args[1], mime_type: args[2] }
         end

  if opts[:dsid]
    Deprecation.warn AttachedFiles, "The :dsid option to add_file is deprecated and will be removed in active-fedora 10.0. Use :path instead", caller
    opts[:path] = opts[:dsid]
  end

  find_or_create_child_resource(opts[:path], opts[:prefix]).tap do |node|
    node.content = file
    node.mime_type = if opts[:mimeType]
                       Deprecation.warn AttachedFiles, "The :mimeType option to add_file is deprecated and will be removed in active-fedora 10.0. Use :mime_type instead", caller
                       opts[:mimeType]
                     else
                       opts[:mime_type]
                     end
    node.original_name = opts[:original_name]
  end
end

#add_file_datastream(file, opts = {}) ⇒ Object

File Management



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

def add_file_datastream(file, opts = {})
  Deprecation.warn AttachedFiles, "add_file_datastream is deprecated and will be removed in active-fedora 10.0. Use add_file instead"
  add_file(file, opts)
end

#attach_file(file, file_path, _opts = {}) ⇒ String

Add an ActiveFedora::File to the object.

Parameters:

Returns:

  • (String)

    path of the added datastream



68
69
70
71
72
# File 'lib/active_fedora/attached_files.rb', line 68

def attach_file(file, file_path, _opts = {})
  create_singleton_association(file_path)
  attached_files[file_path] = file
  file_path
end

#attached_filesObject

Returns all known attached files for the object. If the object has been saved to fedora, the persisted files will be included. Attached files that have been modified in memory are given preference over the copy in Fedora.



33
34
35
# File 'lib/active_fedora/attached_files.rb', line 33

def attached_files
  @attached_files ||= FilesHash.new(self)
end

#clear_attached_filesObject



42
43
44
# File 'lib/active_fedora/attached_files.rb', line 42

def clear_attached_files
  @attached_files = nil
end

#clear_datastreamsObject



46
47
48
# File 'lib/active_fedora/attached_files.rb', line 46

def clear_datastreams
  clear_attached_files
end

#contains_assertionsObject



51
52
53
# File 'lib/active_fedora/attached_files.rb', line 51

def contains_assertions
  resource.query(subject: resource, predicate: ::RDF::Vocab::LDP.contains).objects.map(&:to_s)
end

#datastreamsObject



37
38
39
# File 'lib/active_fedora/attached_files.rb', line 37

def datastreams
  attached_files
end

#declared_attached_filesObject

Returns only the attached_files that are declared with ‘contains’



19
20
21
22
23
# File 'lib/active_fedora/attached_files.rb', line 19

def declared_attached_files
  attached_files.reflections.keys.each_with_object({}) do |k, h|
    h[k] = attached_files[k]
  end
end

#ds_specsObject



9
10
11
# File 'lib/active_fedora/attached_files.rb', line 9

def ds_specs
  self.class.child_resource_reflections
end

#load_attached_filesObject



55
56
57
58
59
60
61
# File 'lib/active_fedora/attached_files.rb', line 55

def load_attached_files
  contains_assertions.each do |file_uri|
    path = file_uri.to_s.sub(uri + '/', '')
    next if association(path.to_sym)
    create_singleton_association(path)
  end
end

#metadata_streamsArray

Returns all attached files that return true for ‘metadata?` and are not Rels-ext.

Returns:

  • (Array)

    all attached files that return true for ‘metadata?` and are not Rels-ext



80
81
82
# File 'lib/active_fedora/attached_files.rb', line 80

def 
  attached_files.select { |_k, ds| ds.metadata? }.values
end

#serialize_attached_filesObject



14
15
16
# File 'lib/active_fedora/attached_files.rb', line 14

def serialize_attached_files
  declared_attached_files.each_value(&:serialize!)
end

#undeclared_filesObject



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

def undeclared_files
  @undeclared_files ||= []
end