Class: Easymongo::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/easymongo/document.rb

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ Document

Takes a BSON::Document



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/easymongo/document.rb', line 7

def initialize(doc)

  # Replace _id with id
  doc['id'] = doc.delete('_id')

  # Convert all BSON::ObjectId to string
  doc.each{|k, v| doc[k] = v.to_s if v.is_a?(BSON::ObjectId)}

  # Write variables
  doc.each{|k, v| attr(k, v)}

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Dynamically write value



31
32
33
# File 'lib/easymongo/document.rb', line 31

def method_missing(name, *args, &block)
  return attr(name[0..-2], args.first) if args.size == 1 and name[-1] == '='
end

Instance Method Details

#bson_idObject

Get bson id



21
22
23
# File 'lib/easymongo/document.rb', line 21

def bson_id
  @bson_id ||= BSON::ObjectId.from_string(@id)
end

#dateObject

Creation date



26
27
28
# File 'lib/easymongo/document.rb', line 26

def date
  bson_id.generation_time rescue nil
end