Class: ActiveRecord::ConnectionAdapters::Elasticsearch::CreateTableDefinition

Inherits:
TableDefinition
  • Object
show all
Defined in:
lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb

Instance Method Summary collapse

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!

Raises:

  • (ArgumentError)


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, **options, &block)
  raise ArgumentError, "you cannot define an already defined alias '#{name}'." if @aliases.key?(name) && !force?(force)

  @aliases[name] = new_alias_definition(name, **options, &block)

  self
end

#aliasesArray

returns an array with all +TableAliasDefinition+.

Returns:

  • (Array)


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

Raises:

  • (ArgumentError)


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, **options, &block)
  raise ArgumentError, "you cannot define an already defined mapping '#{name}'!" if @mappings.key?(name) && !force?(force)

  mapping = new_mapping_definition(name, type, **options, &block)
  @mappings[name] = mapping


  # check if the mapping is assigned as primary_key
  if mapping.primary_key?
    meta :primary_key, mapping.name
    meta(:auto_increment, mapping.auto_increment) if mapping.auto_increment?
  end

  self
end

#mappingsArray Also known as: columns

returns an array with all +TableMappingDefinition+.

Returns:

  • (Array)


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

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 59

def meta(name, value, force: false, **options)
  raise ArgumentError, "you cannot define an already defined meta '#{name}'!" if @metas.key?(name) && !force?(force)

  @metas[name] = new_meta_definition(name, value, **options)

  self
end

#metasArray

returns an array with all +TableMetaDefinition+.

Returns:

  • (Array)


36
37
38
# File 'lib/active_record/connection_adapters/elasticsearch/schema_definitions/create_table_definition.rb', line 36

def metas
  @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, **options)
  args.each do |ref_name|
    ActiveRecord::ConnectionAdapters::ReferenceDefinition.new(ref_name, **options).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 remove_meta(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

Raises:

  • (ArgumentError)


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, **options, &block)
  raise ArgumentError, "you cannot define an already defined setting '#{name}'!" if @settings.key?(name) && !force?(force)

  @settings[name] = new_setting_definition(name, value, **options, &block)

  self
end

#settingsArray

returns an array with all +TableSettingDefinition+.

Returns:

  • (Array)


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 timestamps(**options)
  column(:created_at, :datetime, **options)
  column(:updated_at, :datetime, **options)
end