Module: Mongoid::Document::ClassMethods

Defined in:
lib/ext/mongoid.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ext/mongoid.rb', line 25

def method_missing(name,*args)
  if name.to_s =~ /^remove_(.*)$/
    # if name is pluralize
    if name.to_s.pluralize == name.to_s
      return self.class_eval(%Q{
        define_method("remove_#{$1}") do
          grid_files=self.#{$1}
          grid_files.each do |grid_file|
            id = BSON::ObjectId.from_string(grid_file['grid_id'])
            MongoGrid.grid.delete(id)
          end
        end
      })
    else
      # name is singlular
      return self.class_eval(%Q{
        define_method("remove_#{$1}") do
          grid_file=self.#{$1}
          unless grid_file.blank?
            id = BSON::ObjectId.from_string(grid_file['grid_id'])
            MongoGrid.grid.delete(id)
          end
        end
      })
    end
  else
    puts "No this method"
  end
end