summaryrefslogtreecommitdiff
path: root/lib/tapicero/user_event_handler.rb
blob: 38cf8f82d2740c227595b57686a0fe4bdf629216 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'tapicero/user_database'
module Tapicero
  class UserEventHandler
    def initialize(users)
      users.created do |hash|
        prepare_db(hash['id'])
      end

      # Sometimes changes log starts with rev 2. So the
      # detection of is currently not working properly
      # Working around this until a new version of
      # couchrest changes takes this into account.
      users.updated do |hash|
        prepare_db(hash['id'])
      end

      users.deleted do |hash|
        destroy_db(hash['id'])
      end
    end

    protected

    def prepare_db(id)
      db = user_database(id)
      db.create
      db.secure
      db.add_design_docs
      logger.info "Prepared storage " + db.name
    end

    def destroy_db(id)
      user_database(id).destroy
    end

    def logger
      Tapicero.logger
    end

    def user_database(id)
      UserDatabase.new(id)
    end
  end
end