Module: MongoLight::Document::InstanceMethods

Defined in:
lib/mongo_light/document.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject



85
86
87
# File 'lib/mongo_light/document.rb', line 85

def collection
  self.class.collection
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


69
70
71
# File 'lib/mongo_light/document.rb', line 69

def eql?(other)
  other.is_a?(self.class) && id == other.id
end

#hashObject



73
74
75
# File 'lib/mongo_light/document.rb', line 73

def hash
  id.hash
end

#idObject



76
77
78
# File 'lib/mongo_light/document.rb', line 76

def id
  @attributes[:_id] || @attributes['_id']
end

#id=(value) ⇒ Object



79
80
81
# File 'lib/mongo_light/document.rb', line 79

def id=(value)
  @attributes[:_id] = value
end

#initialize(attributes = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mongo_light/document.rb', line 58

def initialize(attributes = {})
  attributes = {} unless attributes
  @attributes = {:_id => (attributes['_id'] || attributes[:_id] || Id.new)}
  attributes.each do |k,v|
    if self.class.map_include?(k)
      @attributes[k] = v
    elsif k != :_id && k != '_id'
      send("#{k}=", v)
    end
  end
end

#save(options = nil) ⇒ Object



88
89
90
91
92
# File 'lib/mongo_light/document.rb', line 88

def save(options = nil)
  opts = !options || options.include?(:safe) ? options : {:safe => options}
  opts[:safe].delete(:w) if MongoLight.configuration.skip_replica_concern && opts && opts.include?(:safe) && opts[:safe].is_a?(Hash)
  collection.save(self.class.map(@attributes), opts || {})
end

#save!(options = {}) ⇒ Object



93
94
95
# File 'lib/mongo_light/document.rb', line 93

def save!(options = {})
  save({:safe => true}.merge(options))
end