Class: MongoMapper::File

Inherits:
GridFS::GridStore
  • Object
show all
Defined in:
lib/mm-files/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, attrs = {}) ⇒ File

Returns a new instance of File.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mm-files/file.rb', line 5

def initialize(owner, attrs = {})
  @owner = owner
  @id = attrs.delete("_id")

  class_eval do
    attrs.each do |k,v|
      define_method(k) do
        v
      end
    end
  end

  super(@owner.class.database, attrs["filename"], "r", :root => @owner.collection.name)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/mm-files/file.rb', line 3

def attributes
  @attributes
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/mm-files/file.rb', line 3

def id
  @id
end

Class Method Details

.fetch(owner, filename) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/mm-files/file.rb', line 24

def self.fetch(owner, filename)
  db = owner.class.database
  criteria, options = FinderOptions.new(:filename => filename, :metadata => {:_id => owner.id}, :limit => 1).to_a

  obj = db.collection("#{owner.collection.name}.files").find(criteria, options).next_object
  if obj
    self.new(owner, obj)
  end
end

Instance Method Details

#[](name) ⇒ Object



20
21
22
# File 'lib/mm-files/file.rb', line 20

def [](name)
  @attributes[name.to_s]
end