Sha256: 8ddb195bd9e962b0b7a6246941c54fd29675acb6c569a9f80febdce7c48de767

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

# Define Instance Methods
module Associo
  def grid
    @grid ||= Mongo::Grid.new(database)
  end

  private

  def assigned_attachments
    @assigned_attachments ||= {}
  end

  def nil_attachments
    @nil_attachments ||= {}
  end

  # IO must respond to read and rewind
  def save_attachments
    assigned_attachments.each_pair do |name, io|
      next unless io.respond_to?(:read)

      io.rewind if io.respond_to?(:rewind)

      grid.delete send(name).id

      grid.put io, save_attachment_hash(name)
    end

    assigned_attachments.clear
  end

  def save_attachment_hash(name)
    {
      _id:          send(name).id,
      filename:     send(name).name,
      content_type: send(name).type,
      chunk_size:   send(name).chunk_size
    }
  end

  def nullify_nil_attachments_attributes
    nil_attachments.each_key do |name|
      send :"#{name}_id=",   nil
      send :"#{name}_size=", nil
      send :"#{name}_type=", nil
      send :"#{name}_name=", nil
    end
  end

  def destroy_nil_attachments
    nil_attachments.each_value { |id| grid.delete id }

    nil_attachments.clear
  end

  def destroy_all_attachments
    self.class.attachment_names.map { |name| grid.delete send(name).id }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
associo-0.1.0 lib/associo/instance_methods.rb