Class: LittleWeasel::Metadata::InvalidWordsMetadata

Overview

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

Instance Attribute Summary collapse

Attributes included from LittleWeasel::Modules::DictionaryMetadataServicable

#dictionary_cache, #dictionary_key, #dictionary_metadata

Attributes included from LittleWeasel::Modules::DictionaryKeyable

#dictionary_key

Attributes included from LittleWeasel::Modules::DictionaryCacheServicable

#dictionary_cache, #dictionary_key

Attributes included from Metadatable

#metadata

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LittleWeasel::Modules::DictionaryMetadataServicable

#dictionary_metadata_service

Methods included from LittleWeasel::Modules::DictionaryMetadataValidatable

#validate_dictionary_metadata, validate_dictionary_metadata

Methods included from LittleWeasel::Modules::DictionaryCacheValidatable

#validate_dictionary_cache, validate_dictionary_cache

Methods included from LittleWeasel::Modules::DictionaryKeyValidatable

#validate_dictionary_key, validate_dictionary_key

Methods included from LittleWeasel::Modules::DictionaryCacheServicable

#dictionary_cache_service

Methods included from LittleWeasel::Modules::Configurable

#config, included

Methods included from LittleWeasel::Modules::ClassNameToSymbol

included, #to_sym

Methods included from MetadataObserverable

included, #observe?, #refresh_local_metadata

Methods included from Metadatable

included, #metadata_key, #refresh_local_metadata

Constructor Details

#initialize(dictionary_metadata_object:, dictionary_metadata:, dictionary_cache:, dictionary_key:, dictionary_words:) ⇒ InvalidWordsMetadata

Returns a new instance of InvalidWordsMetadata.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

   dictionary_metadata: 
  self. = 

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

  .add_observer self
  self. = 

  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

Instance Attribute Details

#dictionary_metadata_objectObject

Returns the value of attribute dictionary_metadata_object.



26
27
28
# File 'lib/LittleWeasel/metadata/invalid_words_metadata.rb', line 26

def 
  @dictionary_metadata_object
end

#dictionary_wordsObject (private)

Returns the value of attribute dictionary_words.



115
116
117
# File 'lib/LittleWeasel/metadata/invalid_words_metadata.rb', line 115

def dictionary_words
  @dictionary_words
end

Class Method Details

.metadata_keyObject



56
57
58
# File 'lib/LittleWeasel/metadata/invalid_words_metadata.rb', line 56

def 
  to_sym
end

.observe?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/LittleWeasel/metadata/invalid_words_metadata.rb', line 60

def observe?
  config.max_invalid_words_bytesize?
end

Instance Method Details

#actions_whitelistObject



109
110
111
# File 'lib/LittleWeasel/metadata/invalid_words_metadata.rb', line 109

def actions_whitelist
  %i[init refresh word_search]
end

#cache_word?(word) ⇒ Boolean (private)

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
# File 'lib/LittleWeasel/metadata/invalid_words_metadata.rb', line 118

def cache_word?(word)
  return false unless .cache_invalid_words?

  if .value >= (word.bytesize + .current_invalid_word_bytesize)
    .current_invalid_word_bytesize += word.bytesize
    true
  else
    false
  end
end

#init(params: nil) ⇒ Object

rubocop: disable Lint/UnusedMethodArgument



66
67
68
69
70
# File 'lib/LittleWeasel/metadata/invalid_words_metadata.rb', line 66

def init(params: nil)
  .init(metadata_key: )
  self. = Services::InvalidWordsService.new(dictionary_words).execute
  self
end

#refresh(params: nil) ⇒ Object

rubocop: disable Lint/UnusedMethodArgument



74
75
76
77
78
# File 'lib/LittleWeasel/metadata/invalid_words_metadata.rb', line 74

def refresh(params: nil)
  
  init unless .present?
  self
end

#update(action, params) ⇒ Object



99
100
101
102
103
104
105
106
107
# 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

#update_dictionary_metadata(value:) ⇒ Object (private)



129
130
131
# File 'lib/LittleWeasel/metadata/invalid_words_metadata.rb', line 129

def (value:)
  .(value: value, metadata_key: )
end

#word_search(params:) ⇒ Object

This method is called when a word is being searched in the dictionary.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/LittleWeasel/metadata/invalid_words_metadata.rb', line 83

def word_search(params:)
  word_results = params[:word_results]

  # TODO: NOW: Should we be returning #word_valid? or #success?
  return word_results.word_valid? if word_results.word_cached?

  # If we get here, we know that the word is NOT in the dictionary either
  # as a valid word OR as a cached, INVALID word.

  # If caching is supposed to take place, cache the word as invalid
  # (not found).
  word = word_results.preprocessed_word_or_original_word
  dictionary_words[word] = false if cache_word? word
  false
end