class EntityStore::ExternalStore

Attributes

connect_timeout[W]
connection_profile[RW]

Public Class Methods

connection() click to toggle source
# File lib/mongo_entity_store/external_store.rb, line 12
def connection
  @_connection ||= Mongo::MongoClient.from_uri(ExternalStore.connection_profile, :connect_timeout => EntityStore::Config.connect_timeout)
end
database() click to toggle source
# File lib/mongo_entity_store/external_store.rb, line 16
def database
  @_database ||= ExternalStore.connection_profile.split('/').last
end

Public Instance Methods

add_event(entity_type, event) click to toggle source
# File lib/mongo_entity_store/external_store.rb, line 33
def add_event(entity_type, event)
  collection.insert({
    '_entity_type' => entity_type, '_type' => event.class.name 
    }.merge(event.attributes)
  )
end
collection() click to toggle source
# File lib/mongo_entity_store/external_store.rb, line 25
def collection
  @_collection ||= open['events']
end
ensure_indexes() click to toggle source
# File lib/mongo_entity_store/external_store.rb, line 29
def ensure_indexes
  collection.ensure_index([['_type', Mongo::ASCENDING], ['_id', Mongo::ASCENDING]])      
end
get_events(since, type=nil, max_items=100) click to toggle source

Public - get events since a Time or ID

since - Time or String id to filter events from type - String optionally filter the event type to return (default=nil) max_items - Fixnum max items to return (default=100)

Returns Enumerable EventDataObject

# File lib/mongo_entity_store/external_store.rb, line 47
def get_events(since, type=nil, max_items=100)
  since_id = since.is_a?(Time) ? BSON::ObjectId.from_time(since) : BSON::ObjectId.from_string(since)

  query = { '_id' => { '$gt' => since_id } }
  query['_type'] = type if type
  
  options = {
    :sort => [['_id', Mongo::ASCENDING]],
    :limit => max_items
  }
  
  collection.find(query, options).collect { |e| EventDataObject.new(e)}
end
open() click to toggle source
# File lib/mongo_entity_store/external_store.rb, line 21
def open
  ExternalStore.connection.db(ExternalStore.database)
end