Class: Vayacondios::ConfigDocument
- 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
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#organization ⇒ Object
readonly
Returns the value of attribute organization.
-
#topic ⇒ Object
readonly
Returns the value of attribute topic.
Class Method Summary collapse
Instance Method Summary collapse
- #destroy(document) ⇒ Object
- #find ⇒ Object
-
#initialize(mongodb, options = {}) ⇒ ConfigDocument
constructor
A new instance of ConfigDocument.
- #update(document) ⇒ Object
Methods inherited from Document
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, = {}) super @mongo = mongodb = () @id = [:id] @body = nil @field = [:field] ||= [:id] @mongo = mongodb collection_name = [organization.to_s, 'config'].join('.') @collection = @mongo.collection(collection_name) end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
13 14 15 |
# File 'lib/vayacondios/server/model/config_document.rb', line 13 def body @body end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
13 14 15 |
# File 'lib/vayacondios/server/model/config_document.rb', line 13 def id @id end |
#organization ⇒ Object (readonly)
Returns the value of attribute organization.
13 14 15 |
# File 'lib/vayacondios/server/model/config_document.rb', line 13 def organization @organization end |
#topic ⇒ Object (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, ={}) self.new(mongodb, ).update(document) end |
.find(mongodb, options = {}) ⇒ Object
33 34 35 |
# File 'lib/vayacondios/server/model/config_document.rb', line 33 def self.find(mongodb, ={}) self.new(mongodb, ).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 |
#find ⇒ Object
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 |