Class: Roart::Attachment

Inherits:
File
  • Object
show all
Defined in:
lib/roart/attachment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, file) ⇒ Attachment

Returns a new instance of Attachment.



17
18
19
20
# File 'lib/roart/attachment.rb', line 17

def initialize(name, file)
  @name = name
  @file = file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



15
16
17
# File 'lib/roart/attachment.rb', line 15

def file
  @file
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/roart/attachment.rb', line 15

def name
  @name
end

Class Method Details

.detect(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/roart/attachment.rb', line 34

def self.detect(*args)
  AttachmentCollection.new Array(args.compact).flatten.map { |file|
    if file.is_a?(File)
      Attachment.new(File.basename(file.path), file)
    elsif file.is_a?(String)
      Attachment.new(File.basename(file), File.open(file, 'rb'))
    elsif file.respond_to?(:open, :original_filename)
      Attachment.new(file.original_filename, file.open)
    end
  }.compact
end

Instance Method Details

#mime_typeObject



30
31
32
# File 'lib/roart/attachment.rb', line 30

def mime_type
  file.content_type if file.respond_to?(:content_type)
end

#pathObject



22
23
24
# File 'lib/roart/attachment.rb', line 22

def path
  name
end

#readObject



26
27
28
# File 'lib/roart/attachment.rb', line 26

def read
  file.read
end