class LittleWeasel::Metadata::InvalidWordsMetadata

This class provides the ability to cache words not found in the associated dictionary.

Attributes

dictionary_metadata_object[RW]
dictionary_words[RW]

Public Class Methods

metadata_key() click to toggle source
# File lib/LittleWeasel/metadata/invalid_words_metadata.rb, line 56
def metadata_key
  to_sym
end
new(dictionary_metadata_object:, dictionary_metadata:, dictionary_cache:, dictionary_key:, dictionary_words:) click to toggle source
# File lib/LittleWeasel/metadata/invalid_words_metadata.rb, line 28
def initialize(dictionary_metadata_object:, dictionary_metadata:,
  dictionary_cache:, dictionary_key:, dictionary_words:)
  validate_dictionary_key dictionary_key: dictionary_key
  self.dictionary_key = dictionary_key

  validate_dictionary_cache dictionary_cache: dictionary_cache
  self.dictionary_cache = dictionary_cache

  validate_dictionary_metadata dictionary_metadata: dictionary_metadata
  self.dictionary_metadata = dictionary_metadata

  unless dictionary_metadata_object.is_a? Observable
    raise ArgumentError,
      "Argument dictionary_metadata_object is not an Observable: #{dictionary_metadata_object.class}."
  end

  dictionary_metadata_object.add_observer self
  self.dictionary_metadata_object = dictionary_metadata_object

  unless dictionary_words.is_a? Hash
    raise ArgumentError,
      "Argument dictionary_words is not a Hash: #{dictionary_words.class}."
  end

  self.dictionary_words = dictionary_words
end
observe?() click to toggle source
# File lib/LittleWeasel/metadata/invalid_words_metadata.rb, line 60
def observe?
  config.max_invalid_words_bytesize?
end

Public Instance Methods

actions_whitelist() click to toggle source
# File lib/LittleWeasel/metadata/invalid_words_metadata.rb, line 109
def actions_whitelist
  %i[init refresh word_search]
end
init(params: nil) click to toggle source

rubocop: disable Lint/UnusedMethodArgument

# File lib/LittleWeasel/metadata/invalid_words_metadata.rb, line 66
def init(params: nil)
  dictionary_metadata_service.init(metadata_key: metadata_key)
  self.metadata = Services::InvalidWordsService.new(dictionary_words).execute
  self
end
refresh(params: nil) click to toggle source

rubocop: disable Lint/UnusedMethodArgument

# File lib/LittleWeasel/metadata/invalid_words_metadata.rb, line 74
def refresh(params: nil)
  refresh_local_metadata
  init unless metadata.present?
  self
end
update(action, params) click to toggle source
# File lib/LittleWeasel/metadata/invalid_words_metadata.rb, line 99
def update(action, params)
  unless actions_whitelist.include? action
    raise ArgumentError,
      "Argument action is not in the actions_whitelist: #{action}"
  end

  send(action, params: params)
  self
end

Private Instance Methods

cache_word?(word) click to toggle source
# File lib/LittleWeasel/metadata/invalid_words_metadata.rb, line 118
def cache_word?(word)
  return false unless metadata.cache_invalid_words?

  if metadata.value >= (word.bytesize + metadata.current_invalid_word_bytesize)
    metadata.current_invalid_word_bytesize += word.bytesize
    true
  else
    false
  end
end
update_dictionary_metadata(value:) click to toggle source
# File lib/LittleWeasel/metadata/invalid_words_metadata.rb, line 129
def update_dictionary_metadata(value:)
  dictionary_metadata_service.set_dictionary_metadata(value: value, metadata_key: metadata_key)
end