Class: ActiveRecord::ConnectionAdapters::Elasticsearch::CreateTableDefinition
- Inherits:
-
TableDefinition
- Object
- TableDefinition
- ActiveRecord::ConnectionAdapters::Elasticsearch::CreateTableDefinition
- Defined in:
- lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb
Instance Method Summary collapse
-
#[](name) ⇒ Object
Returns a MappingDefinition for the mapping with name +name+.
-
#alias(name, force: false, **options, &block) ⇒ Object
we can use +alias+ here, since the instance method is not a reserved keyword!.
-
#aliases ⇒ Array
returns an array with all +TableAliasDefinition+.
-
#initialize(conn, name, settings: nil, mappings: nil, aliases: nil, **opts) ⇒ CreateTableDefinition
constructor
A new instance of CreateTableDefinition.
-
#mapping(name, type, force: false, **options, &block) ⇒ Object
(also: #column)
adds a new mapping.
-
#mappings ⇒ Array
(also: #columns)
returns an array with all +TableMappingDefinition+.
-
#meta(name, value, force: false, **options) ⇒ Object
adds a new meta.
-
#metas ⇒ Array
returns an array with all +TableMetaDefinition+.
-
#references(*args, **options) ⇒ Object
(also: #belongs_to)
Adds a reference.
- #remove_alias(name) ⇒ Object
- #remove_mapping(name) ⇒ Object (also: #remove_column)
- #remove_meta(name) ⇒ Object
- #remove_setting(name) ⇒ Object
- #setting(name, value, force: false, **options, &block) ⇒ Object
-
#settings ⇒ Array
returns an array with all +TableSettingDefinition+.
-
#timestamps(**options) ⇒ Object
Appends :datetime columns :created_at and :updated_at to the table.
Constructor Details
#initialize(conn, name, settings: nil, mappings: nil, aliases: nil, **opts) ⇒ CreateTableDefinition
Returns a new instance of CreateTableDefinition.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 9 def initialize(conn, name, settings: nil, mappings: nil, aliases: nil, **opts) super(conn, name, **opts) @settings = HashWithIndifferentAccess.new @mappings = HashWithIndifferentAccess.new @aliases = HashWithIndifferentAccess.new @metas = HashWithIndifferentAccess.new transform_settings!(settings) if settings.present? transform_mappings!(mappings) if mappings.present? transform_aliases!(aliases) if aliases.present? end |
Instance Method Details
#[](name) ⇒ Object
Returns a MappingDefinition for the mapping with name +name+.
50 51 52 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 50 def [](name) @mappings[name] end |
#alias(name, force: false, **options, &block) ⇒ Object
we can use +alias+ here, since the instance method is not a reserved keyword!
112 113 114 115 116 117 118 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 112 def alias(name, force: false, **, &block) raise ArgumentError, "you cannot define an already defined alias '#{name}'." if @aliases.key?(name) && !force?(force) @aliases[name] = new_alias_definition(name, **, &block) self end |
#aliases ⇒ Array
returns an array with all +TableAliasDefinition+.
45 46 47 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 45 def aliases @aliases.values end |
#mapping(name, type, force: false, **options, &block) ⇒ Object Also known as: column
adds a new mapping
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 72 def mapping(name, type, force: false, **, &block) raise ArgumentError, "you cannot define an already defined mapping '#{name}'!" if @mappings.key?(name) && !force?(force) mapping = new_mapping_definition(name, type, **, &block) @mappings[name] = mapping # check if the mapping is assigned as primary_key if mapping.primary_key? :primary_key, mapping.name (:auto_increment, mapping.auto_increment) if mapping.auto_increment? end self end |
#mappings ⇒ Array Also known as: columns
returns an array with all +TableMappingDefinition+.
30 31 32 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 30 def mappings @mappings.values end |
#meta(name, value, force: false, **options) ⇒ Object
adds a new meta
59 60 61 62 63 64 65 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 59 def (name, value, force: false, **) raise ArgumentError, "you cannot define an already defined meta '#{name}'!" if @metas.key?(name) && !force?(force) @metas[name] = (name, value, **) self end |
#metas ⇒ Array
returns an array with all +TableMetaDefinition+.
36 37 38 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 36 def @metas.values end |
#references(*args, **options) ⇒ Object Also known as: belongs_to
Adds a reference.
t.references(:user) t.belongs_to(:supplier)
See ActiveRecord::ConnectionAdapters::Elasticsearch::CreateTableDefinition.connectionconnection.add_reference[rdoc-ref:SchemaStatements#add_reference] for details of the options you can use.
130 131 132 133 134 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 130 def references(*args, **) args.each do |ref_name| ActiveRecord::ConnectionAdapters::ReferenceDefinition.new(ref_name, **).add_to(self) end end |
#remove_alias(name) ⇒ Object
120 121 122 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 120 def remove_alias(name) @aliases.delete name end |
#remove_mapping(name) ⇒ Object Also known as: remove_column
91 92 93 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 91 def remove_mapping(name) @mappings.delete(name) end |
#remove_meta(name) ⇒ Object
67 68 69 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 67 def (name) @metas.delete(name) end |
#remove_setting(name) ⇒ Object
106 107 108 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 106 def remove_setting(name) @settings.delete name end |
#setting(name, value, force: false, **options, &block) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 98 def setting(name, value, force: false, **, &block) raise ArgumentError, "you cannot define an already defined setting '#{name}'!" if @settings.key?(name) && !force?(force) @settings[name] = new_setting_definition(name, value, **, &block) self end |
#settings ⇒ Array
returns an array with all +TableSettingDefinition+.
24 25 26 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 24 def settings @settings.values end |
#timestamps(**options) ⇒ Object
Appends :datetime columns :created_at and :updated_at to the table. See ActiveRecord::ConnectionAdapters::Elasticsearch::CreateTableDefinition.connectionconnection.add_timestamps[rdoc-ref:SchemaStatements#add_timestamps]
t.timestamps
142 143 144 145 |
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 142 def (**) column(:created_at, :datetime, **) column(:updated_at, :datetime, **) end |