Class: Vines::Services::CouchModels::Service

Inherits:
CouchRest::Model::Base
  • Object
show all
Extended by:
Storage::CouchDB::ClassMethods
Defined in:
lib/vines/services/storage/couchdb/service.rb

Constant Summary collapse

KEYS =
%w[_id name code accounts users jid created_at updated_at].freeze
VIEW_ID =
"_design/System".freeze
VIEW_NAME =
"System/memberships".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Storage::CouchDB::ClassMethods

build_from_database, to_s

Instance Attribute Details

#sizeObject

Return the number of members in this service’s view. This is faster than calling Service#members#size because this reduces the view, so all members aren’t loaded from the database. The size is cached so subsequent calls to this method do not query the view.



97
98
99
100
101
102
103
# File 'lib/vines/services/storage/couchdb/service.rb', line 97

def size
  unless @size
    rows = database.view(VIEW_NAME, reduce: true, key: [0, id])['rows'] rescue []
    @size = rows.first ? rows.first['value'] : 0
  end
  @size
end

Class Method Details

.find_allObject



133
134
135
136
137
138
139
# File 'lib/vines/services/storage/couchdb/service.rb', line 133

def self.find_all
  sizes = find_sizes
  by_name.map do |doc|
    doc.size = sizes[doc.id] || 0
    doc
  end
end

.find_by_jid(jid) ⇒ Object



125
126
127
# File 'lib/vines/services/storage/couchdb/service.rb', line 125

def self.find_by_jid(jid)
  first_from_view('by_jid', jid.to_s.downcase)
end

.find_by_name(name) ⇒ Object



121
122
123
# File 'lib/vines/services/storage/couchdb/service.rb', line 121

def self.find_by_name(name)
  first_from_view('by_name', name)
end

.find_by_user(jid) ⇒ Object



129
130
131
# File 'lib/vines/services/storage/couchdb/service.rb', line 129

def self.find_by_user(jid)
  by_user.key(jid.to_s.downcase).to_a
end

.find_sizesObject

Return a Hash of service ID to member count.



142
143
144
145
146
147
148
149
# File 'lib/vines/services/storage/couchdb/service.rb', line 142

def self.find_sizes
  {}.tap do |hash|
    rows = database.view(VIEW_NAME, reduce: true, group: true, startkey: [0], endkey: [1])['rows'] rescue []
    rows.each do |row|
      hash[row['key'][1]] = row['value']
    end
  end
end

Instance Method Details

#add_user(jid) ⇒ Object

Allow the user, specified by their JID, to access the members of this service. Adds the JID to the list and ensures the list stays sorted and unique.



82
83
84
85
86
# File 'lib/vines/services/storage/couchdb/service.rb', line 82

def add_user(jid)
  users << jid.to_s.downcase
  users.sort!
  users.uniq!
end

#membersObject

Query the members view and return an Array of Hashes like this: [‘www.wonderland.lit’, os: ‘linux’]. The members are cached so subsequent calls to this method do not query the view.



108
109
110
111
112
113
114
# File 'lib/vines/services/storage/couchdb/service.rb', line 108

def members
  unless @members
    rows = database.view(VIEW_NAME, reduce: false, key: [0, id])['rows'] rescue []
    @members = rows.map {|row| row['value'] }
  end
  @members
end

#remove_user(jid) ⇒ Object

Remove this user’s permission to access this service.



89
90
91
# File 'lib/vines/services/storage/couchdb/service.rb', line 89

def remove_user(jid)
  users.delete(jid.to_s.downcase)
end

#to_resultObject



116
117
118
119
# File 'lib/vines/services/storage/couchdb/service.rb', line 116

def to_result
  to_hash.clone.keep_if {|k, v| KEYS.include?(k) }
    .tap {|h| h['id'] = h.delete('_id') }
end

#user?(jid) ⇒ Boolean

Return true if this JID is allowed access to this service.

Returns:

  • (Boolean)


75
76
77
# File 'lib/vines/services/storage/couchdb/service.rb', line 75

def user?(jid)
  users.include?(jid.to_s.downcase)
end