Class: Junkfood::PaperclipStringIo

Inherits:
StringIO
  • Object
show all
Defined in:
lib/junkfood/paperclip_string_io.rb

Overview

Adapter to save blobs of in-memory data into paperclip enabled ActiveRecord models without requiring the use of temporary files.

Examples:


class FaxDocument < ActiveRecord::Base
  has_attached_file :pdf
end

def incoming_fax_handler()
  attachment = 'Blob of Faxed Data in PDF form'
  fax_number = '555-1234'

  fax_document = FaxDocument.create(
    :caption => 'Look at this Document!',
    :pdf => PaperclipStringIo.new(
      attachment,
      :filename => "#{fax_number}.pdf",
      :content_type => 'application/pdf'))
end

See Also:

Constant Summary collapse

DEFAULT_FILENAME =

Default filename

'unnamed'
DEFAULT_CONTENT_TYPE =

Default file content_type

'application/octet-stream'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, options = {}) ⇒ PaperclipStringIo

Returns a new instance of PaperclipStringIo.

Parameters:

  • string (String)

    blob of to save into paperclip.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :filename (String) — default: 'unnamed'

    set the filename component for the paperclip object.

  • :content_type (String) — default: 'application/octet-stream'

    set the paperclip object’s mime content type.



60
61
62
63
64
# File 'lib/junkfood/paperclip_string_io.rb', line 60

def initialize(string, options={})
  super(string)
  @original_filename = options[:filename] || DEFAULT_FILENAME
  @content_type = options[:content_type] || DEFAULT_CONTENT_TYPE
end

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



45
46
47
# File 'lib/junkfood/paperclip_string_io.rb', line 45

def content_type
  @content_type
end

#original_filenameObject (readonly)

Returns the value of attribute original_filename.



45
46
47
# File 'lib/junkfood/paperclip_string_io.rb', line 45

def original_filename
  @original_filename
end