Class: ActiveKit::Search::Searching

Inherits:
Bedrock::Bedrocking show all
Defined in:
lib/active_kit/search/searching.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(describer:, current_class:) ⇒ Searching

Returns a new instance of Searching.



6
7
8
9
10
11
12
# File 'lib/active_kit/search/searching.rb', line 6

def initialize(describer:, current_class:)
  super

  @index = Index.new(current_class: @current_class, describer: describer)
  @key = Key.new(index: @index, describer: describer)
  @suggestion = Suggestion.new(current_class: @current_class, describer: describer)
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



4
5
6
# File 'lib/active_kit/search/searching.rb', line 4

def current_page
  @current_page
end

#next_pageObject (readonly)

Returns the value of attribute next_page.



4
5
6
# File 'lib/active_kit/search/searching.rb', line 4

def next_page
  @next_page
end

#previous_pageObject (readonly)

Returns the value of attribute previous_page.



4
5
6
# File 'lib/active_kit/search/searching.rb', line 4

def previous_page
  @previous_page
end

Instance Method Details

#add_attribute(name:, options:) ⇒ Object



59
60
61
# File 'lib/active_kit/search/searching.rb', line 59

def add_attribute(name:, options:)
  @index.add_attribute_to_schema(name: name, options: options)
end

#attributes_present?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/active_kit/search/searching.rb', line 63

def attributes_present?
  @index.schema.present?
end

#clear(record: nil) ⇒ Object



19
20
21
22
# File 'lib/active_kit/search/searching.rb', line 19

def clear(record: nil)
  record ? @key.clear(record: record) : @current_class.all.each { |rec| @key.clear(record: rec) }
  @index.reload
end

#dropObject



44
45
46
47
48
49
# File 'lib/active_kit/search/searching.rb', line 44

def drop
  total_count = @index.fetch(offset: 0, limit: 0).count
  keys = @index.fetch(offset: 0, limit: total_count).keys
  @key.drop(keys: keys)
  @index.drop
end

#fetch(**options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_kit/search/searching.rb', line 24

def fetch(**options)
  search_result = @index.fetch(**options)

  if search_result.keys.any?
    @suggestion.add(term: search_result.term)
  else
    @suggestion.del(term: search_result.term)
  end

  @current_page = search_result.current_page
  @previous_page = search_result.previous_page
  @next_page = search_result.next_page

  search_result
end

#next_page?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/active_kit/search/searching.rb', line 55

def next_page?
  !!@next_page
end

#previous_page?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/active_kit/search/searching.rb', line 51

def previous_page?
  !!@previous_page
end

#reload(record: nil) ⇒ Object



14
15
16
17
# File 'lib/active_kit/search/searching.rb', line 14

def reload(record: nil)
  record ? @key.reload(record: record) : @current_class.all.each { |rec| @key.reload(record: rec) }
  @index.reload
end

#suggestions(prefix:) ⇒ Object



40
41
42
# File 'lib/active_kit/search/searching.rb', line 40

def suggestions(prefix:)
  @suggestion.fetch(prefix: prefix)
end