Class: LittleWeasel::Services::InvalidWordsService

Inherits:
Object
  • Object
show all
Defined in:
lib/LittleWeasel/services/invalid_words_service.rb

Overview

This class calculates the total amount of bytes cached invalid words take up in the given dictionary and returns the results. In addition to this, metadata is also compiled to determine how many more bytes of invalid word data can be cached before the cache is depleted and shutdown.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dictionary) ⇒ InvalidWordsService

Returns a new instance of InvalidWordsService.



12
13
14
15
# File 'lib/LittleWeasel/services/invalid_words_service.rb', line 12

def initialize(dictionary)
  self.dictionary = dictionary
  self.current_bytesize = 0
end

Instance Attribute Details

#current_bytesizeObject (private)

Returns the value of attribute current_bytesize.



26
27
28
# File 'lib/LittleWeasel/services/invalid_words_service.rb', line 26

def current_bytesize
  @current_bytesize
end

#dictionaryObject (private)

Returns the value of attribute dictionary.



26
27
28
# File 'lib/LittleWeasel/services/invalid_words_service.rb', line 26

def dictionary
  @dictionary
end

Instance Method Details

#build_returnObject (private)



38
39
40
41
42
43
44
# File 'lib/LittleWeasel/services/invalid_words_service.rb', line 38

def build_return
  Metadata::InvalidWordsServiceResults.new(
    max_invalid_words_bytesize_on: max_invalid_words_bytesize?,
    current_invalid_word_bytesize: current_bytesize,
    max_invalid_words_bytesize: max_invalid_words_bytesize
  )
end

#calculate_current_bytesizeObject (private)



28
29
30
31
32
33
34
35
36
# File 'lib/LittleWeasel/services/invalid_words_service.rb', line 28

def calculate_current_bytesize
  dictionary.reduce(0) do |bytesize, word_and_found|
    unless word_and_found.last
      bytesize += word_and_found.first.bytesize
      break unless bytesize < max_invalid_words_bytesize
    end
    bytesize
  end
end

#configObject (private)



54
55
56
# File 'lib/LittleWeasel/services/invalid_words_service.rb', line 54

def config
  @config ||= LittleWeasel.configuration
end

#executeObject



17
18
19
20
21
22
# File 'lib/LittleWeasel/services/invalid_words_service.rb', line 17

def execute
  return build_return unless max_invalid_words_bytesize?

  self.current_bytesize = calculate_current_bytesize
  build_return
end

#max_invalid_words_bytesizeObject (private)



46
47
48
# File 'lib/LittleWeasel/services/invalid_words_service.rb', line 46

def max_invalid_words_bytesize
  @max_invalid_words_bytesize ||= config.max_invalid_words_bytesize
end

#max_invalid_words_bytesize?Boolean (private)

Returns:

  • (Boolean)


50
51
52
# File 'lib/LittleWeasel/services/invalid_words_service.rb', line 50

def max_invalid_words_bytesize?
  config.max_invalid_words_bytesize?
end