Class: Rmap::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/rmap/database.rb

Instance Method Summary collapse

Constructor Details

#initialize(conf = {:username => 'root'}, &block) ⇒ Database

Returns a new instance of Database.



5
6
7
8
9
10
# File 'lib/rmap/database.rb', line 5

def initialize(conf={:username => 'root'}, &block)
  @conf = conf
  if !block.nil?
    instance_eval(&block)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/rmap/database.rb', line 60

def method_missing name, *args
  if table_names.include? name.to_s
    Table.new(self, name)
  else
    super(name, *args)
  end
end

Instance Method Details

#bindingsObject



43
44
45
# File 'lib/rmap/database.rb', line 43

def bindings
  binding
end

#clientObject



12
13
14
15
16
17
# File 'lib/rmap/database.rb', line 12

def client
  if @client.nil?
    @client = Mysql2::Client.new(@conf)
  end
  @client
end

#create(name) ⇒ Object



68
69
70
71
# File 'lib/rmap/database.rb', line 68

def create(name)
  @table_names = nil
  client.query("create table `#{name}`(id int unsigned not null auto_increment primary key)")
end

#database(database) ⇒ Object



37
38
39
40
41
# File 'lib/rmap/database.rb', line 37

def database(database)
  @client = nil
  @table_names = nil
  @conf[:database] = database
end

#host(host) ⇒ Object



19
20
21
22
23
# File 'lib/rmap/database.rb', line 19

def host(host)
  @client = nil
  @table_names = nil
  @conf[:host] = host
end

#password(password) ⇒ Object



31
32
33
34
35
# File 'lib/rmap/database.rb', line 31

def password(password)
  @client = nil
  @table_names = nil
  @conf[:password] = password
end

#run(file_path = nil, &block) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/rmap/database.rb', line 47

def run(file_path = nil, &block)
  if !file_path.nil?
    instance_eval(::File.open(file_path).read, file_path)
  end
  if !block.nil?
    instance_eval(&block)
  end
end

#table?(name) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/rmap/database.rb', line 56

def table?(name)
  table_names.include?(name.to_s)
end

#table_namesObject



73
74
75
76
77
78
# File 'lib/rmap/database.rb', line 73

def table_names
  if @table_names.nil?
    @table_names = client.query("show tables", :as => :array).map{|a| a[0]}
  end
  @table_names
end

#username(username) ⇒ Object



25
26
27
28
29
# File 'lib/rmap/database.rb', line 25

def username(username)
  @client = nil
  @table_names = nil
  @conf[:username] = username
end