Class: Groupy::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/rugroupy/entity.rb

Instance Method Summary collapse

Constructor Details

#initialize(database, name, entity_id, create = true) ⇒ Entity

Returns a new instance of Entity.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rugroupy/entity.rb', line 6

def initialize(database, name, entity_id, create = true)
  @database = database
  @name = name
  @entity_id = entity_id
  @database[@name].create unless @database.collection_names.member?(@name)

  @database["#{@name}_count"].indexes.create_many([
                                                    { key: { '_id.e' => -1 }, background: false },
                                                    { key: { '_id.tag' => -1 }, background: false },
                                                    { key: { 'value.count' => -1 }, background: false }
                                                  ])

  if create
    begin
      doc = Hash['_id' => @entity_id, 'tags' => {}]
      @database[@name].insert_one(doc, safe: true)
    rescue Mongo::Error => e
    end
  end
end

Instance Method Details

#apply_tag(tag, value, add = true) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rugroupy/entity.rb', line 58

def apply_tag(tag, value, add = true)
  op = add ? '$addToSet' : '$pull'
  doc = {}
  field = "tags.#{tag}"
  if value.is_a?(Array)
    op = '$pullAll' unless add
    doc[op] = add ? { field => { '$each' => value } } : { field => value }
  else
    doc[op] = { field => value }
  end
  spec = Hash['_id' => @entity_id]
  @database[@name].update_one(spec, doc, safe: true)
  nil
end

#clear_tagsObject



36
37
38
39
40
41
# File 'lib/rugroupy/entity.rb', line 36

def clear_tags
  spec = { '_id' => @entity_id }
  doc = { '$set' => { 'tags' => {} } }
  @database[@name].update_one(spec, doc, safe: true)
  nil
end

#deleteObject



31
32
33
34
# File 'lib/rugroupy/entity.rb', line 31

def delete
  @database[@name].delete_one({ '_id' => @entity_id }, safe: true)
  nil
end

#getObject



27
28
29
# File 'lib/rugroupy/entity.rb', line 27

def get
  @database[@name].find('_id' => @entity_id).first
end

#has_tag(tag, value) ⇒ Object



43
44
45
46
# File 'lib/rugroupy/entity.rb', line 43

def has_tag(tag, value)
  e = get
  e['tags'].member?(tag) && e['tags'][tag].member?(value)
end

#similiar(tag = nil, skip = nil, limit = nil, reverse = false) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rugroupy/entity.rb', line 73

def similiar(tag = nil, skip = nil, limit = nil, reverse = false)
  q = BSON::Document.new
  q['_id.e'] = @entity_id
  q['_id.tag'] = tag ? tag : { '$exists' => false }
  opts = {
    projection: { '_id.e' => 1 },
    sort: { 'value.count' => reverse ? 1 : -1 }
  }
  opts[:skip] = skip if skip
  opts[:limit] = limit if limit
  cursor = @database["#{@name}_count"].find(q, opts)
  cursor.collect do |r|
    pair = r['_id']['e']
    pair[0] == @entity_id ? pair[1] : pair[0]
  end
end

#tag(tag, value) ⇒ Object



48
49
50
51
# File 'lib/rugroupy/entity.rb', line 48

def tag(tag, value)
  apply_tag(tag, value)
  nil
end

#untag(tag, value) ⇒ Object



53
54
55
56
# File 'lib/rugroupy/entity.rb', line 53

def untag(tag, value)
  apply_tag(tag, value, add = false)
  nil
end