Class: Easymongo::Document
- Inherits:
-
Object
- Object
- Easymongo::Document
- Defined in:
- lib/easymongo/document.rb
Instance Attribute Summary collapse
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Read value.
-
#[]=(key, value) ⇒ Object
Write value.
-
#bson_id ⇒ Object
Get bson id.
-
#date ⇒ Object
Creation date.
-
#initialize(doc) ⇒ Document
constructor
Takes a BSON::Document.
-
#method_missing(name, *args, &block) ⇒ Object
Make the doc user friendly with dot notation Provides access to doc object methods too.
Constructor Details
#initialize(doc) ⇒ Document
Takes a BSON::Document
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/easymongo/document.rb', line 9 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)} # Symbolize keys self.doc = doc.symbolize_keys end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Make the doc user friendly with dot notation Provides access to doc object methods too
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/easymongo/document.rb', line 43 def method_missing(name, *args, &block) # Write value return doc[name[0..-2].to_sym] = args.first if args.size == 1 and name[-1] == '=' # Read value return doc[name] if doc.has_key?(name) # Run method on doc object return doc.send(name, *args) if doc.respond_to?(name) end |
Instance Attribute Details
#doc ⇒ Object
Returns the value of attribute doc.
6 7 8 |
# File 'lib/easymongo/document.rb', line 6 def doc @doc end |
#values ⇒ Object
Returns the value of attribute values.
6 7 8 |
# File 'lib/easymongo/document.rb', line 6 def values @values end |
Instance Method Details
#[](key) ⇒ Object
Read value
32 33 34 |
# File 'lib/easymongo/document.rb', line 32 def [](key) doc[key.to_sym] end |
#[]=(key, value) ⇒ Object
Write value
37 38 39 |
# File 'lib/easymongo/document.rb', line 37 def []=(key, value) doc[key.to_sym] = value end |
#bson_id ⇒ Object
Get bson id
22 23 24 |
# File 'lib/easymongo/document.rb', line 22 def bson_id @bson_id ||= BSON::ObjectId.from_string(doc[:id]) end |
#date ⇒ Object
Creation date
27 28 29 |
# File 'lib/easymongo/document.rb', line 27 def date bson_id.generation_time end |