Class: ActiveKit::Search::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/active_kit/search/key.rb

Instance Method Summary collapse

Constructor Details

#initialize(index:, describer:) ⇒ Key

Returns a new instance of Key.



4
5
6
7
8
# File 'lib/active_kit/search/key.rb', line 4

def initialize(index:, describer:)
  @redis = ActiveKit::Search.redis
  @index = index
  @describer = describer
end

Instance Method Details

#clear(record:) ⇒ Object



26
27
28
29
# File 'lib/active_kit/search/key.rb', line 26

def clear(record:)
  hash_key = key(record: record)
  drop(keys: hash_key)
end

#drop(keys:) ⇒ Object



31
32
33
34
35
36
# File 'lib/active_kit/search/key.rb', line 31

def drop(keys:)
  return unless keys.present?
  if @redis.del(keys) > 0
    Rails.logger.info "ActiveKit::Search | Keys Removed: " + keys.to_s
  end
end

#reload(record:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_kit/search/key.rb', line 10

def reload(record:)
  clear(record: record)

  hash_key = key(record: record)
  hash_value = { "database" => @describer.database.call, "id" => record.id }
  @index.schema.each do |field_name, field_value|
    attribute_name = field_name
    attribute_value = @index.attribute_value_parser[field_name]&.call(record) || record.public_send(field_name)
    attribute_value = field_value.downcase.include?("tag") ? attribute_value : @index.escape_separators(attribute_value)
    hash_value.store(attribute_name, attribute_value)
  end
  @redis.hset(hash_key, hash_value)
  Rails.logger.info "ActiveKit::Search | Key Reloaded: " + hash_key
  Rails.logger.debug "=> " + @redis.hgetall("#{hash_key}").to_s
end