Class: MySQLExpectations::Matchers::HaveKey
- Inherits:
-
Object
- Object
- MySQLExpectations::Matchers::HaveKey
- Defined in:
- lib/mysql_expectations/matchers/table_have_key.rb
Overview
A matcher that checks if a MySQLDumpExpectations::Table has a field. Optionally, checks the field type and nullability
Instance Attribute Summary collapse
-
#expected_key ⇒ Object
readonly
Returns the value of attribute expected_key.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #current_keys(keys) ⇒ Object
- #description ⇒ Object
- #failure_message ⇒ Object
-
#initialize(expected_key) ⇒ HaveKey
constructor
A new instance of HaveKey.
- #matches?(table) ⇒ Boolean
Constructor Details
#initialize(expected_key) ⇒ HaveKey
Returns a new instance of HaveKey.
13 14 15 16 |
# File 'lib/mysql_expectations/matchers/table_have_key.rb', line 13 def initialize(expected_key) @expected_key = expected_key @table = nil end |
Instance Attribute Details
#expected_key ⇒ Object (readonly)
Returns the value of attribute expected_key.
11 12 13 |
# File 'lib/mysql_expectations/matchers/table_have_key.rb', line 11 def expected_key @expected_key end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
11 12 13 |
# File 'lib/mysql_expectations/matchers/table_have_key.rb', line 11 def table @table end |
Instance Method Details
#current_keys(keys) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/mysql_expectations/matchers/table_have_key.rb', line 29 def current_keys(keys) result = '' keys.each_with_index do |key, index| result << key.to_s result << ",\n" unless index == keys.size - 1 end result end |
#description ⇒ Object
24 25 26 27 |
# File 'lib/mysql_expectations/matchers/table_have_key.rb', line 24 def description description = "have key #{expected_key}" description end |
#failure_message ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mysql_expectations/matchers/table_have_key.rb', line 38 def result = "expected '#{table.name}' table to " \ "have key #{expected_key}" keys = table.keys if keys.empty? result << ' but it has no keys.' else result << " but it has keys:\n" result << current_keys(keys) end result end |
#matches?(table) ⇒ Boolean
18 19 20 21 22 |
# File 'lib/mysql_expectations/matchers/table_have_key.rb', line 18 def matches?(table) @table = table @table.key?(expected_key.name) && @table.key(expected_key.name) == expected_key end |