Class: MySQLExpectations::Database

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

Overview

Allows assertions on a database

Instance Method Summary collapse

Constructor Details

#initialize(database_element) ⇒ Database

Returns a new instance of Database.



13
14
15
# File 'lib/mysql_expectations/database.rb', line 13

def initialize(database_element)
  @database_element = database_element
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/mysql_expectations/database.rb', line 43

def method_missing(method, *arguments, &block)
  if arguments.empty? && block.nil?
    table_name = method.to_s
    return table(table_name) if table?(table_name)
  end
  super
end

Instance Method Details

#nameObject



17
18
19
# File 'lib/mysql_expectations/database.rb', line 17

def name
  @database_element.attributes['name']
end

#respond_to_missing?(method) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/mysql_expectations/database.rb', line 51

def respond_to_missing?(method, *)
  table?(method.to_s) || super
end

#table(name) ⇒ Object



28
29
30
31
32
# File 'lib/mysql_expectations/database.rb', line 28

def table(name)
  query = "table_structure[@name='#{name}']"
  table_element = @database_element.elements[query]
  Table.new table_element if table_element
end

#table?(name) ⇒ Boolean Also known as: has_table?

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/mysql_expectations/database.rb', line 21

def table?(name)
  query = "table_structure[@name='#{name}']"
  !@database_element.elements[query].nil?
end

#tablesObject



34
35
36
37
38
39
40
41
# File 'lib/mysql_expectations/database.rb', line 34

def tables
  query = 'table_structure'
  tables = []
  @database_element.elements.each(query) do |table_element|
    tables << Table.new(table_element)
  end
  tables
end