Class: DbSchema::Definitions::Table
- Inherits:
-
Object
- Object
- DbSchema::Definitions::Table
- Defined in:
- lib/db_schema/definitions/table.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#checks ⇒ Object
readonly
Returns the value of attribute checks.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#foreign_keys ⇒ Object
readonly
Returns the value of attribute foreign_keys.
-
#indexes ⇒ Object
readonly
Returns the value of attribute indexes.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #check(check_name) ⇒ Object
- #field(field_name) ⇒ Object (also: #[])
- #foreign_key(fkey_name) ⇒ Object
- #has_check?(check_name) ⇒ Boolean
- #has_expressions? ⇒ Boolean
- #has_field?(field_name) ⇒ Boolean
- #has_foreign_key?(fkey_name) ⇒ Boolean
- #has_foreign_key_to?(other_table_name) ⇒ Boolean
- #has_index?(index_name) ⇒ Boolean
- #has_index_on?(*field_names) ⇒ Boolean
- #has_unique_index_on?(*field_names) ⇒ Boolean
- #index(index_name) ⇒ Object
-
#initialize(name, fields: [], indexes: [], checks: [], foreign_keys: []) ⇒ Table
constructor
A new instance of Table.
- #with_fields(new_fields) ⇒ Object
- #with_foreign_keys(new_foreign_keys) ⇒ Object
- #with_indexes(new_indexes) ⇒ Object
- #with_name(new_name) ⇒ Object
Constructor Details
#initialize(name, fields: [], indexes: [], checks: [], foreign_keys: []) ⇒ Table
Returns a new instance of Table.
7 8 9 10 11 12 13 |
# File 'lib/db_schema/definitions/table.rb', line 7 def initialize(name, fields: [], indexes: [], checks: [], foreign_keys: []) @name = name.to_sym @fields = fields @indexes = indexes @checks = checks @foreign_keys = foreign_keys end |
Instance Attribute Details
#checks ⇒ Object (readonly)
Returns the value of attribute checks.
5 6 7 |
# File 'lib/db_schema/definitions/table.rb', line 5 def checks @checks end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
5 6 7 |
# File 'lib/db_schema/definitions/table.rb', line 5 def fields @fields end |
#foreign_keys ⇒ Object (readonly)
Returns the value of attribute foreign_keys.
5 6 7 |
# File 'lib/db_schema/definitions/table.rb', line 5 def foreign_keys @foreign_keys end |
#indexes ⇒ Object (readonly)
Returns the value of attribute indexes.
5 6 7 |
# File 'lib/db_schema/definitions/table.rb', line 5 def indexes @indexes end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/db_schema/definitions/table.rb', line 5 def name @name end |
Instance Method Details
#check(check_name) ⇒ Object
50 51 52 |
# File 'lib/db_schema/definitions/table.rb', line 50 def check(check_name) checks.find { |check| check.name == check_name } || NullCheckConstraint.new end |
#field(field_name) ⇒ Object Also known as: []
21 22 23 |
# File 'lib/db_schema/definitions/table.rb', line 21 def field(field_name) fields.find { |field| field.name == field_name } || NullField.new end |
#foreign_key(fkey_name) ⇒ Object
58 59 60 |
# File 'lib/db_schema/definitions/table.rb', line 58 def foreign_key(fkey_name) foreign_keys.find { |fkey| fkey.name == fkey_name } || NullForeignKey.new end |
#has_check?(check_name) ⇒ Boolean
54 55 56 |
# File 'lib/db_schema/definitions/table.rb', line 54 def has_check?(check_name) !check(check_name).is_a?(NullCheckConstraint) end |
#has_expressions? ⇒ Boolean
15 16 17 18 19 |
# File 'lib/db_schema/definitions/table.rb', line 15 def has_expressions? fields.any?(&:default_is_expression?) || indexes.any?(&:has_expressions?) || checks.any? end |
#has_field?(field_name) ⇒ Boolean
26 27 28 |
# File 'lib/db_schema/definitions/table.rb', line 26 def has_field?(field_name) !field(field_name).is_a?(NullField) end |
#has_foreign_key?(fkey_name) ⇒ Boolean
62 63 64 |
# File 'lib/db_schema/definitions/table.rb', line 62 def has_foreign_key?(fkey_name) !foreign_key(fkey_name).is_a?(NullForeignKey) end |
#has_foreign_key_to?(other_table_name) ⇒ Boolean
66 67 68 |
# File 'lib/db_schema/definitions/table.rb', line 66 def has_foreign_key_to?(other_table_name) foreign_keys.any? { |fkey| fkey.table == other_table_name } end |
#has_index?(index_name) ⇒ Boolean
34 35 36 |
# File 'lib/db_schema/definitions/table.rb', line 34 def has_index?(index_name) !index(index_name).is_a?(NullIndex) end |
#has_index_on?(*field_names) ⇒ Boolean
38 39 40 41 42 |
# File 'lib/db_schema/definitions/table.rb', line 38 def has_index_on?(*field_names) indexes.any? do |index| index.columns.none?(&:expression?) && index.columns.map(&:name) == field_names end end |
#has_unique_index_on?(*field_names) ⇒ Boolean
44 45 46 47 48 |
# File 'lib/db_schema/definitions/table.rb', line 44 def has_unique_index_on?(*field_names) indexes.any? do |index| index.unique? && index.columns.none?(&:expression?) && index.columns.map(&:name) == field_names end end |
#index(index_name) ⇒ Object
30 31 32 |
# File 'lib/db_schema/definitions/table.rb', line 30 def index(index_name) indexes.find { |index| index.name == index_name } || NullIndex.new end |
#with_fields(new_fields) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/db_schema/definitions/table.rb', line 80 def with_fields(new_fields) Table.new( name, fields: new_fields, indexes: indexes, checks: checks, foreign_keys: foreign_keys ) end |
#with_foreign_keys(new_foreign_keys) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/db_schema/definitions/table.rb', line 100 def with_foreign_keys(new_foreign_keys) Table.new( name, fields: fields, indexes: indexes, checks: checks, foreign_keys: new_foreign_keys ) end |
#with_indexes(new_indexes) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/db_schema/definitions/table.rb', line 90 def with_indexes(new_indexes) Table.new( name, fields: fields, indexes: new_indexes, checks: checks, foreign_keys: foreign_keys ) end |
#with_name(new_name) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/db_schema/definitions/table.rb', line 70 def with_name(new_name) Table.new( new_name, fields: fields, indexes: indexes, checks: checks, foreign_keys: foreign_keys ) end |