Module: Grip::ClassMethods

Defined in:
lib/grip.rb

Instance Method Summary collapse

Instance Method Details

#_root_documentObject



43
44
45
# File 'lib/grip.rb', line 43

def _root_document
  
end

#attachment(name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/grip.rb', line 6

def attachment(name)
  write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
  attachment_definitions[name] = {}

  after_save     :save_attachments
  before_destroy :destroy_attached_files

  key "#{name}_id".to_sym,   ObjectId
  key "#{name}_name".to_sym, String
  key "#{name}_size".to_sym, Integer
  key "#{name}_type".to_sym, String

  define_method(name) do
    self.class.grid.get(self["#{name}_id"])
  end

  define_method("#{name}=") do |file|
    self["#{name}_id"]   = BSON::ObjectID.new
    self["#{name}_size"] = File.size(file)
    self["#{name}_type"] = Wand.wave(file.path)
    self["#{name}_name"] = if file.respond_to?(:original_filename)
      file.original_filename
    else
      File.basename(file.path)
    end
    self.class.attachment_definitions[name] = file
  end
end

#attachment_definitionsObject



39
40
41
# File 'lib/grip.rb', line 39

def attachment_definitions
  read_inheritable_attribute(:attachment_definitions)
end

#gridObject



35
36
37
# File 'lib/grip.rb', line 35

def grid
  @grid ||= Mongo::Grid.new(MongoMapper.database)
end