Class: ActiveRecordSpannerAdapter::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_spanner_adapter/index.rb,
lib/activerecord_spanner_adapter/index/column.rb

Defined Under Namespace

Classes: Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, name, columns, type: nil, unique: false, null_filtered: false, interleave_in: nil, storing: nil, state: nil) ⇒ Index

Returns a new instance of Index.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/activerecord_spanner_adapter/index.rb', line 14

def initialize \
    table,
    name,
    columns,
    type: nil,
    unique: false,
    null_filtered: false,
    interleave_in: nil,
    storing: nil,
    state: nil
  @table = table.to_s
  @name = name.to_s
  @columns = Array(columns)
  @type = type
  @unique = unique
  @null_filtered = null_filtered
  @interleave_in = interleave_in unless interleave_in.to_s.empty?
  @storing = storing || []
  @state = state
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



11
12
13
# File 'lib/activerecord_spanner_adapter/index.rb', line 11

def columns
  @columns
end

#interleave_inObject

Returns the value of attribute interleave_in.



11
12
13
# File 'lib/activerecord_spanner_adapter/index.rb', line 11

def interleave_in
  @interleave_in
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/activerecord_spanner_adapter/index.rb', line 11

def name
  @name
end

#null_filteredObject

Returns the value of attribute null_filtered.



11
12
13
# File 'lib/activerecord_spanner_adapter/index.rb', line 11

def null_filtered
  @null_filtered
end

#stateObject

Returns the value of attribute state.



11
12
13
# File 'lib/activerecord_spanner_adapter/index.rb', line 11

def state
  @state
end

#storingObject

Returns the value of attribute storing.



11
12
13
# File 'lib/activerecord_spanner_adapter/index.rb', line 11

def storing
  @storing
end

#tableObject

Returns the value of attribute table.



11
12
13
# File 'lib/activerecord_spanner_adapter/index.rb', line 11

def table
  @table
end

#typeObject

Returns the value of attribute type.



11
12
13
# File 'lib/activerecord_spanner_adapter/index.rb', line 11

def type
  @type
end

#uniqueObject

Returns the value of attribute unique.



11
12
13
# File 'lib/activerecord_spanner_adapter/index.rb', line 11

def unique
  @unique
end

Instance Method Details

#column_namesObject



45
46
47
# File 'lib/activerecord_spanner_adapter/index.rb', line 45

def column_names
  columns_by_position.map(&:name)
end

#columns_by_positionObject



39
40
41
42
43
# File 'lib/activerecord_spanner_adapter/index.rb', line 39

def columns_by_position
  @columns.select(&:ordinal_position).sort do |c1, c2|
    c1.ordinal_position <=> c2.ordinal_position
  end
end

#optionsObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/activerecord_spanner_adapter/index.rb', line 55

def options
  {
    name: name,
    order: orders,
    unique: unique,
    interleave_in: interleave_in,
    null_filtered: null_filtered,
    storing: storing
  }.delete_if { |_, v| v.nil? }
end

#ordersObject



49
50
51
52
53
# File 'lib/activerecord_spanner_adapter/index.rb', line 49

def orders
  columns_by_position.each_with_object({}) do |c, r|
    r[c.name] = c.desc? ? :desc : :asc
  end
end

#primary?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/activerecord_spanner_adapter/index.rb', line 35

def primary?
  @type == "PRIMARY_KEY"
end

#rename_column_options(old_column, new_column) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/activerecord_spanner_adapter/index.rb', line 66

def rename_column_options old_column, new_column
  opts = options

  opts[:order].transform_keys do |key|
    key.to_s == new_column.to_s
  end

  columns = column_names.map do |c|
    c.to_s == old_column.to_s ? new_column : c
  end

  { options: opts, columns: columns }
end