Class: Rubotnik::UserStore

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rubotnik/user_store.rb

Overview

In-memory storage for users

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUserStore

Returns a new instance of UserStore.



8
9
10
# File 'lib/rubotnik/user_store.rb', line 8

def initialize
  @users = []
end

Instance Attribute Details

#usersObject (readonly)

Returns the value of attribute users.



6
7
8
# File 'lib/rubotnik/user_store.rb', line 6

def users
  @users
end

Instance Method Details

#add(user) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rubotnik/user_store.rb', line 16

def add(user)
  @users << user
  user = @users.last
  if user
    Rubotnik.logger.info "user #{user.inspect} added to store"
    Rubotnik.logger.info "we got #{@users.count} users: #{@users}"
  else
    Rubotnik.logger.info 'user not found in store yet'
  end
  user
end

#find(id) ⇒ Object



28
29
30
31
32
33
# File 'lib/rubotnik/user_store.rb', line 28

def find(id)
  user = @users.find { |u| u.id == id }
  Rubotnik.logger.info "user #{user} found in store" if user
  Rubotnik.logger.info "we got #{@users.count} users: #{@users}" if user
  user
end

#find_or_create_user(id) ⇒ Object



12
13
14
# File 'lib/rubotnik/user_store.rb', line 12

def find_or_create_user(id)
  find(id) || add(Rubotnik::User.new(id))
end