Sha256: 864e73e78b1eea7262d4aeb74659797f4cfa20960e14b8de6960070d8498c627

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

module MongoidExt
  class FileList < EmbeddedHash
    attr_accessor :parent_document

    def put(id, io, metadata = {})
      if !parent_document.new_record?
        filename = id
        if io.respond_to?(:original_filename)
          filename = io.original_filename
        elsif io.respond_to?(:path) && io.path
          filename = ::File.basename(io.path)
        elsif io.kind_of?(String)
          io = StringIO.new(io)
        end

        get(id).put(filename, io, metadata)
      else
        (@_pending_files ||= {})[id] = [io, metadata]
      end
    end

    def files
      ids = self.keys
      ids.delete("_id")
      ids.map {|v| get(v) }
    end

    def each_file(&block)
      (self.keys-["_id"]).each do |key|
        file = self.get(key)
        yield key, file
      end
    end

    def get(id)
      if id.kind_of?(MongoidExt::File)
        self[id.id] = id
        return id
      end

      id = id.to_s.gsub(".", "_")
      file = self[id]
      if file.nil?
        file = self[id] = MongoidExt::File.new
      elsif file.class == BSON::OrderedHash
        file = self[id] = MongoidExt::File.new(file)
      end

      file._root_document = parent_document
      file
    end

    def sync_files
      if @_pending_files
        @_pending_files.each do |filename, data|
          put(filename, data[0], data[1])
        end
        @_pending_files = nil
      end
    end

    def delete(id)
      file = self.get(id)
      super(id)
      file.delete
    end

    def destroy_files
      each_file do |id, file|
        get(id).delete
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid_ext-0.6.2 lib/mongoid_ext/file_list.rb
mongoid_ext-0.6.1 lib/mongoid_ext/file_list.rb