Class: Baza::Driver::Mysql::Users

Inherits:
Object
  • Object
show all
Defined in:
lib/baza/driver/mysql/users.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Users

Returns a new instance of Users.



4
5
6
7
# File 'lib/baza/driver/mysql/users.rb', line 4

def initialize(args)
  @args = args
  @db = @args.fetch(:db)
end

Instance Method Details

#create(data) ⇒ Object



35
36
37
38
# File 'lib/baza/driver/mysql/users.rb', line 35

def create(data)
  @db.query("CREATE USER '#{@db.esc(data.fetch(:name))}'@'#{@db.esc(data.fetch(:host))}' IDENTIFIED BY '#{data.fetch(:password)}'")
  find_by_name(data.fetch(:name))
end

#find_by_name(name) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/baza/driver/mysql/users.rb', line 27

def find_by_name(name)
  list do |user|
    return user if user.name == name.to_s
  end

  raise Baza::Errors::UserNotFound, "Could not find a user by that name: #{name}"
end

#listObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/baza/driver/mysql/users.rb', line 9

def list
  result = []
  @db.query("SELECT * FROM mysql.user") do |user_data|
    user = Baza::Driver::Mysql::User.new(
      db: @db,
      data: user_data
    )

    if block_given?
      yield user
    else
      result << user
    end
  end

  result unless block_given?
end