Class: Vayacondios::EventDocument

Inherits:
Document
  • Object
show all
Defined in:
lib/vayacondios/server/model/event_document.rb

Overview

The event model

Event documents are key-value pairs, represented in JSON. A document consists of a primary key called the topic (_id in mongodb). It belongs to a collection named "#organization_name.##topic.events"

Note: mongodb is passed in beacuse Goliath makes Thread lookups will not work while Goliath is in a streaming context.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Document

update

Constructor Details

#initialize(mongodb, options = {}) ⇒ EventDocument

Returns a new instance of EventDocument.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vayacondios/server/model/event_document.rb', line 15

def initialize(mongodb, options = {})
  super options
  @mongo = mongodb
  options = sanitize_options(options)

  @body   = nil
  @id     = format_id(options[:id])
  @mongo  = mongodb

  collection_name = [organization, topic, 'events'].join('.')
  @collection = @mongo.collection(collection_name)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



13
14
15
# File 'lib/vayacondios/server/model/event_document.rb', line 13

def body
  @body
end

#organizationObject (readonly)

Returns the value of attribute organization.



13
14
15
# File 'lib/vayacondios/server/model/event_document.rb', line 13

def organization
  @organization
end

#topicObject (readonly)

Returns the value of attribute topic.



13
14
15
# File 'lib/vayacondios/server/model/event_document.rb', line 13

def topic
  @topic
end

Class Method Details

.create(mongodb, document, options = {}) ⇒ Object



28
29
30
# File 'lib/vayacondios/server/model/event_document.rb', line 28

def self.create(mongodb, document, options={})
  self.new(mongodb, options).update(document)
end

.find(mongodb, options = {}) ⇒ Object



32
33
34
# File 'lib/vayacondios/server/model/event_document.rb', line 32

def self.find(mongodb, options={})
  self.new(mongodb, options).find
end

Instance Method Details

#destroy(document) ⇒ Object



62
63
64
# File 'lib/vayacondios/server/model/event_document.rb', line 62

def destroy(document)
  super
end

#findObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vayacondios/server/model/event_document.rb', line 36

def find
  result = @collection.find_one({_id: @id})
  if result.present?
    result.delete("_id")
    result['_timestamp'] = result.delete("t")
    result.merge! result.delete("d") if result["d"].present?
    @body = result
    self
  else
    nil
  end
end

#update(document) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vayacondios/server/model/event_document.rb', line 49

def update(document)
  document = to_mongo(document)

  @body = document[:d]
  if @id
    @collection.update({:_id => @id}, document, {upsert: true})
  else
    @collection.insert(document)
  end

  self
end