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


64
65
66
# File 'lib/active_fedora/attached_files.rb', line 64

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

#add_file(file, *args) ⇒ Object

Attach the given file to object

Parameters:

  • file (File)

    the file to add

  • opts (Hash)

    options: :dsid, :prefix, :mime_type


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/active_fedora/attached_files.rb', line 91

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


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

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

Adds datastream to the object.

Returns:

  • (String)

    path of the added datastream


58
59
60
61
62
# File 'lib/active_fedora/attached_files.rb', line 58

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.


26
27
28
# File 'lib/active_fedora/attached_files.rb', line 26

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

#clear_attached_filesObject


35
36
37
# File 'lib/active_fedora/attached_files.rb', line 35

def clear_attached_files
  @attached_files = nil
end

#clear_datastreamsObject


39
40
41
# File 'lib/active_fedora/attached_files.rb', line 39

def clear_datastreams
  clear_attached_files
end

#contains_assertionsObject


44
45
46
# File 'lib/active_fedora/attached_files.rb', line 44

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

#datastreamsObject


30
31
32
# File 'lib/active_fedora/attached_files.rb', line 30

def datastreams
  attached_files
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


48
49
50
51
52
53
54
# File 'lib/active_fedora/attached_files.rb', line 48

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


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

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
  attached_files.each_value {|file| file.serialize! }
end

#undeclared_filesObject


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

def undeclared_files
  @undeclared_files ||= []
end