Class: DbSchema::DSL::TableYielder
- Inherits:
-
Object
- Object
- DbSchema::DSL::TableYielder
- Defined in:
- lib/db_schema/dsl.rb
Instance Attribute Summary collapse
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Class Method Summary collapse
- .build_foreign_key(fkey_fields, table_name:, references:, name: nil, on_update: :no_action, on_delete: :no_action, deferrable: false) ⇒ Object
- .build_index(columns, table_name:, name: nil, unique: false, using: :btree, where: nil, **ordered_fields) ⇒ Object
Instance Method Summary collapse
- #array(name, of:, **options) ⇒ Object
- #check(name, condition) ⇒ Object
- #checks ⇒ Object
- #field(name, type, unique: false, index: false, references: nil, check: nil, **options) ⇒ Object
- #fields ⇒ Object
- #foreign_key(*fkey_fields, **fkey_options) ⇒ Object
- #foreign_keys ⇒ Object
- #index(*columns, **index_options) ⇒ Object
- #indexes ⇒ Object
-
#initialize(table_name, block) ⇒ TableYielder
constructor
A new instance of TableYielder.
- #method_missing(method_name, name, *args, &block) ⇒ Object
- #primary_key(name) ⇒ Object
Constructor Details
#initialize(table_name, block) ⇒ TableYielder
Returns a new instance of TableYielder.
41 42 43 44 |
# File 'lib/db_schema/dsl.rb', line 41 def initialize(table_name, block) @table_name = table_name block.call(self) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, name, *args, &block) ⇒ Object
58 59 60 |
# File 'lib/db_schema/dsl.rb', line 58 def method_missing(method_name, name, *args, &block) field(name, method_name, args.first || {}) end |
Instance Attribute Details
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
39 40 41 |
# File 'lib/db_schema/dsl.rb', line 39 def table_name @table_name end |
Class Method Details
.build_foreign_key(fkey_fields, table_name:, references:, name: nil, on_update: :no_action, on_delete: :no_action, deferrable: false) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/db_schema/dsl.rb', line 165 def build_foreign_key(fkey_fields, table_name:, references:, name: nil, on_update: :no_action, on_delete: :no_action, deferrable: false) fkey_name = name || :"#{table_name}_#{fkey_fields.first}_fkey" if references.is_a?(Array) # [:table, :field] referenced_table, *referenced_keys = references Definitions::ForeignKey.new( name: fkey_name, fields: fkey_fields, table: referenced_table, keys: referenced_keys, on_delete: on_delete, on_update: on_update, deferrable: deferrable ) else # :table Definitions::ForeignKey.new( name: fkey_name, fields: fkey_fields, table: references, on_delete: on_delete, on_update: on_update, deferrable: deferrable ) end end |
.build_index(columns, table_name:, name: nil, unique: false, using: :btree, where: nil, **ordered_fields) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/db_schema/dsl.rb', line 121 def build_index(columns, table_name:, name: nil, unique: false, using: :btree, where: nil, **ordered_fields) if columns.last.is_a?(Hash) *ascending_columns, ordered_expressions = columns else ascending_columns = columns ordered_expressions = {} end columns_data = ascending_columns.each_with_object({}) do |column_name, columns| columns[column_name] = :asc end.merge(ordered_fields).merge(ordered_expressions) index_columns = columns_data.map do |column_name, | = case when :asc {} when :desc { order: :desc } when :asc_nulls_first { nulls: :first } when :desc_nulls_last { order: :desc, nulls: :last } else raise ArgumentError, 'Only :asc, :desc, :asc_nulls_first and :desc_nulls_last options are supported.' end if column_name.is_a?(String) Definitions::Index::Expression.new(column_name, **) else Definitions::Index::TableField.new(column_name, **) end end index_name = name || "#{table_name}_#{index_columns.map(&:index_name_segment).join('_')}_index" Definitions::Index.new( name: index_name, columns: index_columns, unique: unique, type: using, condition: where ) end |
Instance Method Details
#array(name, of:, **options) ⇒ Object
54 55 56 |
# File 'lib/db_schema/dsl.rb', line 54 def array(name, of:, **) field(name, :array, element_type: of, **) end |
#check(name, condition) ⇒ Object
74 75 76 |
# File 'lib/db_schema/dsl.rb', line 74 def check(name, condition) checks << Definitions::CheckConstraint.new(name: name, condition: condition) end |
#checks ⇒ Object
112 113 114 |
# File 'lib/db_schema/dsl.rb', line 112 def checks @checks ||= [] end |
#field(name, type, unique: false, index: false, references: nil, check: nil, **options) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/db_schema/dsl.rb', line 86 def field(name, type, unique: false, index: false, references: nil, check: nil, **) fields << Definitions::Field.build(name, type, ) if unique index(name, unique: true) elsif index index(name) end if references foreign_key(name, references: references) end if check check("#{table_name}_#{name}_check", check) end end |
#fields ⇒ Object
104 105 106 |
# File 'lib/db_schema/dsl.rb', line 104 def fields @fields ||= [] end |
#foreign_key(*fkey_fields, **fkey_options) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/db_schema/dsl.rb', line 78 def foreign_key(*fkey_fields, **) foreign_keys << TableYielder.build_foreign_key( fkey_fields, table_name: table_name, ** ) end |
#foreign_keys ⇒ Object
116 117 118 |
# File 'lib/db_schema/dsl.rb', line 116 def foreign_keys @foreign_keys ||= [] end |
#index(*columns, **index_options) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/db_schema/dsl.rb', line 66 def index(*columns, **) indexes << TableYielder.build_index( columns, table_name: table_name, ** ) end |
#indexes ⇒ Object
108 109 110 |
# File 'lib/db_schema/dsl.rb', line 108 def indexes @indexes ||= [] end |
#primary_key(name) ⇒ Object
62 63 64 |
# File 'lib/db_schema/dsl.rb', line 62 def primary_key(name) field(name, :integer, primary_key: true) end |