Class: Gratan::Client

Inherits:
Object
  • Object
show all
Includes:
Logger::Helper
Defined in:
lib/gratan/client.rb

Instance Method Summary collapse

Methods included from Logger::Helper

#log

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



4
5
6
7
8
9
# File 'lib/gratan/client.rb', line 4

def initialize(options = {})
  @options = options
  @options[:identifier] ||= Gratan::Identifier::Null.new
  client = Mysql2::Client.new(options)
  @driver = Gratan::Driver.new(client, options)
end

Instance Method Details

#apply(file, options = {}) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/gratan/client.rb', line 58

def apply(file, options = {})
  options = @options.merge(options)

  in_progress do
    walk(file, options)
  end
end

#chunk_by_user(exported) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gratan/client.rb', line 35

def chunk_by_user(exported)
  chunked = {}

  exported.sort_by {|user_host, attrs|
    user_host[0]
  }.chunk {|user_host, attrs|
    user_host[0]
  }.each {|user, grants|
    merged_attrs = {}
    hosts = []

    grants.each do |user_host, attrs|
      hosts << user_host[1]
      merged_attrs.deep_merge!(attrs)
    end

    user_host = [user, hosts.sort]
    chunked[user_host] = merged_attrs
  }

  chunked
end

#export(options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gratan/client.rb', line 11

def export(options = {})
  options = @options.merge(options)
  exported = Gratan::Exporter.export(@driver, options)

  if options[:chunk_by_user]
    exported = chunk_by_user(exported)
  end

  if block_given?
    exported.sort_by {|user_host, attrs|
      user_host[0].empty? ? 'root' : user_host[0]
    }.chunk {|user_host, attrs|
      user_host[0].empty? ? 'root' : user_host[0]
    }.each {|user, grants|
      h = {}
      grants.sort_by {|k, v| k }.each {|k, v| h[k] = v }
      dsl = Gratan::DSL.convert(h, options)
      yield(user, dsl)
    }
  else
    Gratan::DSL.convert(exported, options)
  end
end