Class: ActiveRecord::Embedded::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/embedded/index.rb,
lib/active_record/embedded/index/collection.rb

Overview

Indexes allow for fast searching of embedded model data, taking advantage of faster iteration times over arrays than hashes in the database.

Defined Under Namespace

Classes: Collection

Constant Summary collapse

DEFAULT_DIRECTION =
:asc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes:, direction: DEFAULT_DIRECTION, unique: false) ⇒ Index

Returns a new instance of Index

Parameters:

  • attributes (Array)
  • direction (Symbol)
    • Either :desc or :asc.

  • unique (Boolean)
    • Error when index is already taken


16
17
18
19
20
# File 'lib/active_record/embedded/index.rb', line 16

def initialize(attributes:, direction: DEFAULT_DIRECTION, unique: false)
  @attributes = Array(attributes)
  @direction = direction
  @unique = unique
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes


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

def attributes
  @attributes
end

#directionObject (readonly)

Returns the value of attribute direction


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

def direction
  @direction
end

#uniqueObject (readonly)

Returns the value of attribute unique


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

def unique
  @unique
end

Instance Method Details

#build(data = []) ⇒ Object


28
29
30
31
32
33
34
35
36
# File 'lib/active_record/embedded/index.rb', line 28

def build(data = [])
  {
    options: {
      direction: direction,
      unique: unique
    },
    values: values_for(data)
  }
end

#nameObject


22
23
24
25
26
# File 'lib/active_record/embedded/index.rb', line 22

def name
  return attributes.first.to_s if attributes.one?

  attributes.join('_and_')
end