Class: EntityStore::ExternalStore

Inherits:
Object
  • Object
show all
Includes:
Mongo
Defined in:
lib/mongo_entity_store/external_store.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.connect_timeout=(value) ⇒ Object (writeonly)

Sets the attribute connect_timeout

Parameters:

  • value

    the value to set the attribute connect_timeout to.



10
11
12
# File 'lib/mongo_entity_store/external_store.rb', line 10

def connect_timeout=(value)
  @connect_timeout = value
end

.connection_profileObject

Returns the value of attribute connection_profile.



9
10
11
# File 'lib/mongo_entity_store/external_store.rb', line 9

def connection_profile
  @connection_profile
end

Class Method Details

.connectionObject



12
13
14
# 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

.databaseObject



16
17
18
# File 'lib/mongo_entity_store/external_store.rb', line 16

def database
  @_database ||= ExternalStore.connection_profile.split('/').last
end

Instance Method Details

#add_event(entity_type, event) ⇒ Object



33
34
35
36
37
38
# 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

#collectionObject



25
26
27
# File 'lib/mongo_entity_store/external_store.rb', line 25

def collection
  @_collection ||= open['events']
end

#ensure_indexesObject



29
30
31
# 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) ⇒ Object

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



47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

#openObject



21
22
23
# File 'lib/mongo_entity_store/external_store.rb', line 21

def open
  ExternalStore.connection.db(ExternalStore.database)
end