Class: Vayacondios::ItemsetDocument

Inherits:
Document
  • Object
show all
Defined in:
lib/vayacondios/server/model/itemset_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 = {}) ⇒ ItemsetDocument

Returns a new instance of ItemsetDocument.



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

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

  @body         = nil
  @mongo        = mongodb

  @handler = Vayacondios.legacy_switch

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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/vayacondios/server/model/itemset_document.rb', line 14

def id
  @id
end

#organizationObject (readonly)

Returns the value of attribute organization.



14
15
16
# File 'lib/vayacondios/server/model/itemset_document.rb', line 14

def organization
  @organization
end

#topicObject (readonly)

Returns the value of attribute topic.



14
15
16
# File 'lib/vayacondios/server/model/itemset_document.rb', line 14

def topic
  @topic
end

Class Method Details

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



31
32
33
# File 'lib/vayacondios/server/model/itemset_document.rb', line 31

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

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



35
36
37
# File 'lib/vayacondios/server/model/itemset_document.rb', line 35

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

Instance Method Details

#bodyObject



39
40
41
# File 'lib/vayacondios/server/model/itemset_document.rb', line 39

def body
  @handler.wrap_contents(@body)
end

#destroy(document) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/vayacondios/server/model/itemset_document.rb', line 87

def destroy(document)
  raise Vayacondios::Error::BadRequest.new unless @handler.proper_request(document)

  @body -= extract_contents(document)
  
  @collection.update({:_id => @id}, {
    '$pullAll' => {
      'd' => extract_contents(document)
    }
  })
  
  self
end

#findObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vayacondios/server/model/itemset_document.rb', line 43

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

  if result
    result.delete("_id")
    @body = result["d"]
    self
  else
    nil
  end
end

#patch(document) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/vayacondios/server/model/itemset_document.rb', line 66

def patch(document)
  raise Vayacondios::Error::BadRequest.new unless @handler.proper_request(document)

  # Merge ourselves
  if @body
    @body = @body + extract_contents(document)
  else
    @body = extract_contents(document)
  end

  @collection.update({:_id => @id}, {
    '$addToSet' => {
      'd' => {
        '$each'=> extract_contents(document)
      }
    }
  }, {upsert: true})
  
  self
end

#update(document) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/vayacondios/server/model/itemset_document.rb', line 55

def update(document)
  raise Vayacondios::Error::BadRequest.new unless @handler.proper_request(document)

  @body = extract_contents(document)


  @collection.update({:_id => @id}, {:_id => @id, 'd' => @body }, {upsert: true})
  
  self
end