Class: Vayacondios::ConfigDocument

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

Overview

The configuration model

Configuration 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.config"

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 = {}) ⇒ ConfigDocument

Returns a new instance of ConfigDocument.



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

def initialize(mongodb, options = {})
  super options
  @mongo = mongodb
  options = sanitize_options(options)
  @id = options[:id]

  @body         = nil
  @field        = options[:field] ||= options[:id]
  @mongo        = mongodb

  collection_name = [organization.to_s, 'config'].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/config_document.rb', line 13

def body
  @body
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#organizationObject (readonly)

Returns the value of attribute organization.



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

def organization
  @organization
end

#topicObject (readonly)

Returns the value of attribute topic.



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

def topic
  @topic
end

Class Method Details

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



29
30
31
# File 'lib/vayacondios/server/model/config_document.rb', line 29

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

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



33
34
35
# File 'lib/vayacondios/server/model/config_document.rb', line 33

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

Instance Method Details

#destroy(document) ⇒ Object



65
66
67
# File 'lib/vayacondios/server/model/config_document.rb', line 65

def destroy(document)
  super
end

#findObject



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

def find
  result = @collection.find_one({_id: @topic})

  if result.present?
    result.delete("_id")
    @body = result
    # @body = @field.split('.').inject(result){|acc, attr| acc = acc[attr]} if @field.present?
    self
  else
    nil
  end
end

#update(document) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vayacondios/server/model/config_document.rb', line 50

def update(document)
  raise Vayacondios::Error::BadRequest.new if !document.is_a?(Hash)

  # Merge ourselves
  document = body.deep_merge(document) if body

  fields = document
  # fields = {@field => document} if @field.present?

  @body = document
  @collection.update({:_id => @topic}, {'$set' => fields}, {upsert: true})

  self
end