Class: Mingo

Inherits:
Hash
  • Object
show all
Extended by:
ActiveModel::Translation, Connection, Finders, Many
Includes:
ActiveModel::Conversion, Callbacks, Changes, Persistence, Properties
Defined in:
lib/mingo.rb,
lib/mingo/cursor.rb,
lib/mingo/changes.rb,
lib/mingo/finders.rb,
lib/mingo/callbacks.rb,
lib/mingo/connection.rb,
lib/mingo/many_proxy.rb,
lib/mingo/properties.rb,
lib/mingo/timestamps.rb,
lib/mingo/persistence.rb

Defined Under Namespace

Modules: Callbacks, Changes, Connection, Finders, Many, Persistence, Properties, Timestamps Classes: Cursor, ManyProxy

Instance Attribute Summary

Attributes included from Connection

#collection, #db

Attributes included from Changes

#changes

Instance Method Summary collapse

Methods included from Connection

collection_name, connect, connected?

Methods included from Finders

find, first

Methods included from Many

many

Methods included from Changes

#[]=, #changed?, included

Methods included from Callbacks

#destroy, included, #save

Methods included from Persistence

#destroy, #destroyed?, #persisted?, #reload, #save, #update

Methods included from Properties

#[], #[]=, #inspect, #stringify_keys, #stringify_keys!, #to_hash

Constructor Details

#initialize(obj = nil) ⇒ Mingo

Returns a new instance of Mingo.



35
36
37
38
39
40
41
42
43
# File 'lib/mingo.rb', line 35

def initialize(obj = nil)
  super()
  if obj
    # a doc loaded straight from the db?
    if obj['_id'].is_a? BSON::ObjectId then merge!(obj)
    else obj.each { |prop, value| self.send("#{prop}=", value) }
    end
  end
end

Instance Method Details

#==(other) ⇒ Object



54
55
56
# File 'lib/mingo.rb', line 54

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

#cache_keyObject



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

def cache_key
  model_key = self.class.model_name.cache_key
  if not persisted?
    "#{model_key}/new"
  elsif timestamp = self['updated_at']
    "#{model_key}/#{id}-#{timestamp.utc.strftime("%Y%m%d%H%M%S")}"
  else
    "#{model_key}/#{id}"
  end
end

#idObject



50
51
52
# File 'lib/mingo.rb', line 50

def id
  self['_id']
end