Class: Chewy::Index::Specification

Inherits:
Object
  • Object
show all
Defined in:
lib/chewy/index/specification.rb

Overview

Index specification is a combination of index settings and mappings. The idea behind this class is that specification can be locked in the Chewy::Stash::Specification between resets, so it is possible to track changes. In the future it is planned to be way smarter but right now rake chewy:deploy checks if there were changes and resets the index only if anything was changed. Otherwise, the index reset is skipped.

Instance Method Summary collapse

Constructor Details

#initialize(index) ⇒ Specification

Returns a new instance of Specification.

Parameters:

See Also:



15
16
17
# File 'lib/chewy/index/specification.rb', line 15

def initialize(index)
  @index = index
end

Instance Method Details

#changed?true, false

Compares previously locked and current specifications.

Returns:

  • (true, false)

    the result of comparison



55
56
57
# File 'lib/chewy/index/specification.rb', line 55

def changed?
  current != locked
end

#currentHash

Simply returns Chewy::Index.specification_hash, but prepared for JSON with as_json method. This means all the keys are strings and there are only values of types handled in JSON.

Returns:

  • (Hash)

    a JSON-ready hash

See Also:



48
49
50
# File 'lib/chewy/index/specification.rb', line 48

def current
  @index.specification_hash.as_json
end

#lock!true

Stores the current index specification to the Chewy::Stash::Specification as json.

Returns:

  • (true)

    if everything is fine

Raises:



24
25
26
27
28
29
# File 'lib/chewy/index/specification.rb', line 24

def lock!
  Chewy::Stash::Specification.import!([
    id: @index.derivable_name,
    specification: Base64.encode64(current.to_json)
  ], journal: false)
end

#lockedHash

Returns the last locked specification as ruby hash. Returns empty hash if nothing is stored yet.

Returns:

  • (Hash)

    hash produced with JSON parser



35
36
37
38
39
40
# File 'lib/chewy/index/specification.rb', line 35

def locked
  filter = {ids: {values: [@index.derivable_name]}}
  document = Chewy::Stash::Specification.filter(filter).first
  return {} unless document
  JSON.load(Base64.decode64(document.specification)) # rubocop:disable Security/JSONLoad
end