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
# 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



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

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



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

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

#dateObject

Creation date



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

def date
  bson_id.generation_time rescue nil
end