Class: OnlineMigrations::IndexesCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/online_migrations/indexes_collector.rb

Constant Summary collapse

COLUMN_TYPES =
[:bigint, :binary, :boolean, :date, :datetime, :decimal,
:float, :integer, :json, :string, :text, :time, :timestamp, :virtual]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIndexesCollector

Returns a new instance of IndexesCollector.



11
12
13
# File 'lib/online_migrations/indexes_collector.rb', line 11

def initialize
  @indexes = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args, **options) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/online_migrations/indexes_collector.rb', line 33

def method_missing(method_name, *_args, **options)
  # Check for type-based methods, where we can also specify an index:
  # t.string :email, index: true
  if COLUMN_TYPES.include?(method_name)
    index = options.fetch(:index, false)

    if index
      using = index.is_a?(Hash) ? index[:using].to_s : nil
      @indexes << IndexDefinition.new(using: using)
    end
  end
end

Instance Attribute Details

#indexesObject (readonly)

Returns the value of attribute indexes.



9
10
11
# File 'lib/online_migrations/indexes_collector.rb', line 9

def indexes
  @indexes
end

Instance Method Details

#collect {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



15
16
17
# File 'lib/online_migrations/indexes_collector.rb', line 15

def collect
  yield self
end

#index(_column_name, **options) ⇒ Object



19
20
21
# File 'lib/online_migrations/indexes_collector.rb', line 19

def index(_column_name, **options)
  @indexes << IndexDefinition.new(using: options[:using].to_s)
end

#references(*_ref_names, **options) ⇒ Object Also known as: belongs_to



23
24
25
26
27
28
29
30
# File 'lib/online_migrations/indexes_collector.rb', line 23

def references(*_ref_names, **options)
  index = options.fetch(:index, true)

  if index
    using = index.is_a?(Hash) ? index[:using].to_s : nil
    @indexes << IndexDefinition.new(using: using)
  end
end