LittleWeasel 3.0.4 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +3 -0
  3. data/.reek.yml +17 -0
  4. data/.rspec +4 -2
  5. data/.rubocop.yml +187 -0
  6. data/.ruby-version +1 -1
  7. data/.yardopts +2 -0
  8. data/Gemfile +3 -1
  9. data/LittleWeasel.gemspec +31 -18
  10. data/README.md +408 -42
  11. data/Rakefile +296 -3
  12. data/lib/LittleWeasel.rb +5 -184
  13. data/lib/LittleWeasel/block_results.rb +81 -0
  14. data/lib/LittleWeasel/configure.rb +98 -0
  15. data/lib/LittleWeasel/dictionary.rb +125 -0
  16. data/lib/LittleWeasel/dictionary_key.rb +48 -0
  17. data/lib/LittleWeasel/dictionary_manager.rb +85 -0
  18. data/lib/LittleWeasel/errors/dictionary_file_already_loaded_error.rb +9 -0
  19. data/lib/LittleWeasel/errors/dictionary_file_empty_error.rb +8 -0
  20. data/lib/LittleWeasel/errors/dictionary_file_not_found_error.rb +8 -0
  21. data/lib/LittleWeasel/errors/dictionary_file_too_large_error.rb +16 -0
  22. data/lib/LittleWeasel/errors/language_required_error.rb +8 -0
  23. data/lib/LittleWeasel/errors/must_override_error.rb +8 -0
  24. data/lib/LittleWeasel/filters/en_us/currency_filter.rb +19 -0
  25. data/lib/LittleWeasel/filters/en_us/numeric_filter.rb +19 -0
  26. data/lib/LittleWeasel/filters/en_us/single_character_word_filter.rb +21 -0
  27. data/lib/LittleWeasel/filters/word_filter.rb +59 -0
  28. data/lib/LittleWeasel/filters/word_filter_managable.rb +80 -0
  29. data/lib/LittleWeasel/filters/word_filter_validatable.rb +31 -0
  30. data/lib/LittleWeasel/filters/word_filterable.rb +19 -0
  31. data/lib/LittleWeasel/filters/word_filters_validatable.rb +29 -0
  32. data/lib/LittleWeasel/metadata/dictionary_metadata.rb +145 -0
  33. data/lib/LittleWeasel/metadata/invalid_words_metadata.rb +134 -0
  34. data/lib/LittleWeasel/metadata/invalid_words_service_results.rb +45 -0
  35. data/lib/LittleWeasel/metadata/metadata_observable_validatable.rb +22 -0
  36. data/lib/LittleWeasel/metadata/metadata_observerable.rb +90 -0
  37. data/lib/LittleWeasel/metadata/metadatable.rb +136 -0
  38. data/lib/LittleWeasel/modules/class_name_to_symbol.rb +26 -0
  39. data/lib/LittleWeasel/modules/configurable.rb +26 -0
  40. data/lib/LittleWeasel/modules/deep_dup.rb +11 -0
  41. data/lib/LittleWeasel/modules/dictionary_cache_keys.rb +34 -0
  42. data/lib/LittleWeasel/modules/dictionary_cache_servicable.rb +26 -0
  43. data/lib/LittleWeasel/modules/dictionary_cache_validatable.rb +20 -0
  44. data/lib/LittleWeasel/modules/dictionary_creator_servicable.rb +27 -0
  45. data/lib/LittleWeasel/modules/dictionary_file_loader.rb +67 -0
  46. data/lib/LittleWeasel/modules/dictionary_key_validatable.rb +19 -0
  47. data/lib/LittleWeasel/modules/dictionary_keyable.rb +24 -0
  48. data/lib/LittleWeasel/modules/dictionary_loader_servicable.rb +27 -0
  49. data/lib/LittleWeasel/modules/dictionary_metadata_servicable.rb +29 -0
  50. data/lib/LittleWeasel/modules/dictionary_metadata_validatable.rb +17 -0
  51. data/lib/LittleWeasel/modules/dictionary_sourceable.rb +26 -0
  52. data/lib/LittleWeasel/modules/dictionary_validatable.rb +30 -0
  53. data/lib/LittleWeasel/modules/language.rb +23 -0
  54. data/lib/LittleWeasel/modules/language_validatable.rb +16 -0
  55. data/lib/LittleWeasel/modules/locale.rb +40 -0
  56. data/lib/LittleWeasel/modules/order_validatable.rb +18 -0
  57. data/lib/LittleWeasel/modules/orderable.rb +17 -0
  58. data/lib/LittleWeasel/modules/region.rb +23 -0
  59. data/lib/LittleWeasel/modules/region_validatable.rb +16 -0
  60. data/lib/LittleWeasel/modules/tag_validatable.rb +16 -0
  61. data/lib/LittleWeasel/modules/taggable.rb +31 -0
  62. data/lib/LittleWeasel/modules/word_results_validatable.rb +28 -0
  63. data/lib/LittleWeasel/preprocessors/en_us/capitalize_preprocessor.rb +22 -0
  64. data/lib/LittleWeasel/preprocessors/preprocessed_word.rb +28 -0
  65. data/lib/LittleWeasel/preprocessors/preprocessed_word_validatable.rb +55 -0
  66. data/lib/LittleWeasel/preprocessors/preprocessed_words.rb +55 -0
  67. data/lib/LittleWeasel/preprocessors/preprocessed_words_validatable.rb +27 -0
  68. data/lib/LittleWeasel/preprocessors/word_preprocessable.rb +19 -0
  69. data/lib/LittleWeasel/preprocessors/word_preprocessor.rb +122 -0
  70. data/lib/LittleWeasel/preprocessors/word_preprocessor_managable.rb +114 -0
  71. data/lib/LittleWeasel/preprocessors/word_preprocessor_validatable.rb +40 -0
  72. data/lib/LittleWeasel/preprocessors/word_preprocessors_validatable.rb +24 -0
  73. data/lib/LittleWeasel/services/dictionary_cache_service.rb +262 -0
  74. data/lib/LittleWeasel/services/dictionary_creator_service.rb +94 -0
  75. data/lib/LittleWeasel/services/dictionary_file_loader_service.rb +37 -0
  76. data/lib/LittleWeasel/services/dictionary_killer_service.rb +35 -0
  77. data/lib/LittleWeasel/services/dictionary_loader_service.rb +59 -0
  78. data/lib/LittleWeasel/services/dictionary_metadata_service.rb +114 -0
  79. data/lib/LittleWeasel/services/invalid_words_service.rb +59 -0
  80. data/lib/LittleWeasel/version.rb +3 -1
  81. data/lib/LittleWeasel/word_results.rb +146 -0
  82. data/spec/factories/dictionary.rb +43 -0
  83. data/spec/factories/dictionary_cache_service.rb +95 -0
  84. data/spec/factories/dictionary_creator_service.rb +16 -0
  85. data/spec/factories/dictionary_file_loader_service.rb +13 -0
  86. data/spec/factories/dictionary_hash.rb +39 -0
  87. data/spec/factories/dictionary_key.rb +14 -0
  88. data/spec/factories/dictionary_killer_service.rb +14 -0
  89. data/spec/factories/dictionary_loader_service.rb +14 -0
  90. data/spec/factories/dictionary_manager.rb +10 -0
  91. data/spec/factories/dictionary_metadata.rb +16 -0
  92. data/spec/factories/dictionary_metadata_service.rb +16 -0
  93. data/spec/factories/numeric_filter.rb +12 -0
  94. data/spec/factories/preprocessed_word.rb +16 -0
  95. data/spec/factories/preprocessed_words.rb +41 -0
  96. data/spec/factories/single_character_word_filter.rb +12 -0
  97. data/spec/factories/word_results.rb +16 -0
  98. data/spec/lib/LittleWeasel/block_results_spec.rb +248 -0
  99. data/spec/lib/LittleWeasel/configure_spec.rb +74 -0
  100. data/spec/lib/LittleWeasel/dictionary_key_spec.rb +118 -0
  101. data/spec/lib/LittleWeasel/dictionary_manager_spec.rb +116 -0
  102. data/spec/lib/LittleWeasel/dictionary_spec.rb +289 -0
  103. data/spec/lib/LittleWeasel/filters/en_us/currency_filter_spec.rb +80 -0
  104. data/spec/lib/LittleWeasel/filters/en_us/numeric_filter_spec.rb +66 -0
  105. data/spec/lib/LittleWeasel/filters/en_us/single_character_word_filter_spec.rb +58 -0
  106. data/spec/lib/LittleWeasel/filters/word_filter_managable_spec.rb +180 -0
  107. data/spec/lib/LittleWeasel/filters/word_filter_spec.rb +151 -0
  108. data/spec/lib/LittleWeasel/filters/word_filter_validatable_spec.rb +94 -0
  109. data/spec/lib/LittleWeasel/filters/word_filters_validatable_spec.rb +48 -0
  110. data/spec/lib/LittleWeasel/integraton_tests/dictionary_integration_spec.rb +201 -0
  111. data/spec/lib/LittleWeasel/metadata/dictionary_creator_servicable_spec.rb +54 -0
  112. data/spec/lib/LittleWeasel/metadata/dictionary_metadata_spec.rb +209 -0
  113. data/spec/lib/LittleWeasel/metadata/invalid_words_metadata_spec.rb +155 -0
  114. data/spec/lib/LittleWeasel/metadata/metadata_observerable_spec.rb +31 -0
  115. data/spec/lib/LittleWeasel/metadata/metadatable_spec.rb +35 -0
  116. data/spec/lib/LittleWeasel/modules/class_name_to_symbol_spec.rb +21 -0
  117. data/spec/lib/LittleWeasel/modules/dictionary_file_loader_spec.rb +125 -0
  118. data/spec/lib/LittleWeasel/modules/dictionary_sourceable_spec.rb +44 -0
  119. data/spec/lib/LittleWeasel/modules/language_spec.rb +52 -0
  120. data/spec/lib/LittleWeasel/modules/locale_spec.rb +140 -0
  121. data/spec/lib/LittleWeasel/modules/region_spec.rb +52 -0
  122. data/spec/lib/LittleWeasel/preprocessors/en_us/capitalize_preprocessor_spec.rb +34 -0
  123. data/spec/lib/LittleWeasel/preprocessors/preprocessed_word_spec.rb +105 -0
  124. data/spec/lib/LittleWeasel/preprocessors/preprocessed_word_validatable_spec.rb +143 -0
  125. data/spec/lib/LittleWeasel/preprocessors/preprocessed_words_spec.rb +77 -0
  126. data/spec/lib/LittleWeasel/preprocessors/preprocessed_words_validatable_spec.rb +58 -0
  127. data/spec/lib/LittleWeasel/preprocessors/word_preprocessor_managable_spec.rb +216 -0
  128. data/spec/lib/LittleWeasel/preprocessors/word_preprocessor_spec.rb +175 -0
  129. data/spec/lib/LittleWeasel/preprocessors/word_preprocessor_validatable_spec.rb +109 -0
  130. data/spec/lib/LittleWeasel/preprocessors/word_preprocessors_validatable_spec.rb +49 -0
  131. data/spec/lib/LittleWeasel/services/dictionary_cache_service_spec.rb +444 -0
  132. data/spec/lib/LittleWeasel/services/dictionary_creator_service_spec.rb +119 -0
  133. data/spec/lib/LittleWeasel/services/dictionary_file_loader_service_spec.rb +71 -0
  134. data/spec/lib/LittleWeasel/services/dictionary_loader_service_spec.rb +50 -0
  135. data/spec/lib/LittleWeasel/services/dictionary_metadata_service_spec.rb +279 -0
  136. data/spec/lib/LittleWeasel/word_results_spec.rb +275 -0
  137. data/spec/lib/LittleWeasel/workflow/workflow_spec.rb +20 -0
  138. data/spec/spec_helper.rb +117 -6
  139. data/spec/support/factory_bot.rb +15 -0
  140. data/spec/support/file_helpers.rb +32 -0
  141. data/spec/support/files/empty-dictionary.txt +0 -0
  142. data/{lib/dictionary → spec/support/files/en-US-big.txt} +262156 -31488
  143. data/spec/support/files/en-US-tagged.txt +26 -0
  144. data/spec/support/files/en-US.txt +26 -0
  145. data/spec/support/files/en.txt +26 -0
  146. data/spec/support/files/es-ES.txt +27 -0
  147. data/spec/support/files/es.txt +27 -0
  148. data/spec/support/general_helpers.rb +68 -0
  149. data/spec/support/shared_contexts.rb +108 -0
  150. data/spec/support/shared_examples.rb +105 -0
  151. metadata +408 -65
  152. data/spec/checker/checker_spec.rb +0 -286
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :dictionary, class: LittleWeasel::Dictionary do
5
+ dictionary_key { create(:dictionary_key) }
6
+ dictionary_cache { {} }
7
+ dictionary_metadata { {} }
8
+ word_filters {}
9
+ dictionary_words do
10
+ %w(apple
11
+ better
12
+ cat
13
+ dog
14
+ everyone
15
+ fat
16
+ game
17
+ help
18
+ italic
19
+ jasmine
20
+ kelp
21
+ love
22
+ man
23
+ nope
24
+ octopus
25
+ popeye
26
+ queue
27
+ ruby
28
+ stop
29
+ top
30
+ ultimate
31
+ very
32
+ was
33
+ xylophone
34
+ yes
35
+ zebra)
36
+ end
37
+
38
+ skip_create
39
+ initialize_with do
40
+ new dictionary_key: dictionary_key, dictionary_cache: dictionary_cache, dictionary_metadata: dictionary_metadata, dictionary_words: dictionary_words, word_filters: word_filters
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: false
2
+
3
+ require 'pry'
4
+
5
+ FactoryBot.define do
6
+ factory :dictionary_cache_service, class: LittleWeasel::Services::DictionaryCacheService do
7
+ dictionary_key { create(:dictionary_key) }
8
+ dictionary_cache { {} }
9
+
10
+ transient do
11
+ # The dictionary reference created in the cache will point to a MEMORY source.
12
+ #
13
+ # Valid values: nil | true | false | <Array of dictionary words>
14
+ #
15
+ # If nil or false - No memory source will be added to the dictionary cache.
16
+ # If true - A memory source will be added to the dictionary cache.
17
+ # If <An Array of dictionary words> - A memory source will be added to the dictionry cache.
18
+ # This only makes sense if load == true.
19
+ dictionary_memory_source {}
20
+
21
+ # The dictionary reference created in the cache will point to a FILE source.
22
+ #
23
+ # Important: dictionary_file_source will only be used if dictionary_memory_source
24
+ # is false.
25
+ #
26
+ # Valid values: nil | true | false | <Path to dictionary file>
27
+ #
28
+ # If nil or false - No file source will be added to the dictionary cache.
29
+ #
30
+ # If true - A file source will be added to the dictionry cache.
31
+ # dictionary_key.key will be used to create the dictionary
32
+ # file path.
33
+ # If <Path to dictionary file> - A files source will be added to the dictionry cache.
34
+ # The file source will point to <Path to dictionary file>.
35
+ dictionary_file_source {}
36
+
37
+ # If load == true - A dictionary object will be created and added to the dictionary cache
38
+ # depending on the dictionary source (file or memory).
39
+ load { false }
40
+ end
41
+
42
+ skip_create
43
+ initialize_with do
44
+ new(dictionary_key: dictionary_key, dictionary_cache: dictionary_cache)
45
+ end
46
+
47
+ after :create do |dictionary_cache_service, evaluator|
48
+ dictionary_key = dictionary_cache_service.dictionary_key
49
+ dictionary_cache = dictionary_cache_service.dictionary_cache
50
+
51
+ # Initialize the dictionary cache if the user already passed an
52
+ # initialized dictionary cache; otherwise, just use what they passed us.
53
+ dictionary_cache_service.class.init(dictionary_cache: dictionary_cache) \
54
+ unless dictionary_cache_service.class.count(dictionary_cache: dictionary_cache).positive?
55
+
56
+ load = evaluator.load
57
+ dictionary_memory_source = evaluator.dictionary_memory_source
58
+ dictionary_file_source = evaluator.dictionary_file_source
59
+
60
+ if load
61
+ unless dictionary_memory_source.present? || dictionary_file_source.present?
62
+ raise 'Transient attributes dictionary_memory_source or dictionary_file_source ' \
63
+ "must be present if transient attribute load is true: #{dictionary_reference}"
64
+ end
65
+ end
66
+
67
+ if dictionary_file_source
68
+ file_name = if dictionary_file_source == true
69
+ dictionary_key.key
70
+ else
71
+ dictionary_file_source
72
+ end
73
+ dictionary_cache_service.add_dictionary_source(source: dictionary_path_for(file_name: file_name))
74
+ elsif dictionary_memory_source
75
+ dictionary_cache_service.add_dictionary_source(source: LittleWeasel::Modules::DictionarSourceable.memory_source)
76
+ end
77
+
78
+ if load
79
+ dictionary_words = if dictionary_file_source
80
+ dictionary_file_loader_service = create(:dictionary_file_loader_service, dictionary_key: dictionary_key, dictionary_cache: dictionary_cache)
81
+ dictionary_file_loader_service.execute
82
+ else
83
+ unless dictionary_memory_source.is_a? Array
84
+ raise 'Transient attribute dictionary_memory_source must be an Array of words ' \
85
+ "if transient attribute load == true: #{dictionary_memory_source}"
86
+ end
87
+ dictionary_memory_source
88
+ end
89
+ dictionary_cache_service.dictionary_object = create(:dictionary, dictionary_key: dictionary_key, dictionary_cache: dictionary_cache, dictionary_words: dictionary_words)
90
+ end
91
+
92
+ dictionary_cache_service
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :dictionary_creator_service, class: LittleWeasel::Services::DictionaryCreatorService do
5
+ dictionary_key { create(:dictionary_key) }
6
+ dictionary_cache { {} }
7
+ dictionary_metadata { {} }
8
+ word_filters {}
9
+ word_preprocessors {}
10
+
11
+ skip_create
12
+ initialize_with do
13
+ new(dictionary_key: dictionary_key, dictionary_cache: dictionary_cache, dictionary_metadata: dictionary_metadata, word_filters: word_filters, word_preprocessors: word_preprocessors)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :dictionary_file_loader_service, class: LittleWeasel::Services::DictionaryFileLoaderService do
5
+ dictionary_key { create(:dictionary_key) }
6
+ dictionary_cache { {} }
7
+
8
+ skip_create
9
+ initialize_with do
10
+ new(dictionary_key: dictionary_key, dictionary_cache: dictionary_cache)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :dictionary_hash, class: Hash do
5
+ dictionary_words do
6
+ %w(apple
7
+ better
8
+ cat
9
+ dog
10
+ everyone
11
+ fat
12
+ game
13
+ help
14
+ italic
15
+ jasmine
16
+ kelp
17
+ love
18
+ man
19
+ nope
20
+ octopus
21
+ popeye
22
+ queue
23
+ ruby
24
+ stop
25
+ top
26
+ ultimate
27
+ very
28
+ was
29
+ xylophone
30
+ yes
31
+ zebra)
32
+ end
33
+
34
+ skip_create
35
+ initialize_with do
36
+ LittleWeasel::Dictionary.to_hash(dictionary_words: dictionary_words)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :dictionary_key, class: LittleWeasel::DictionaryKey do
5
+ language { :en }
6
+ region { :us }
7
+ tag {}
8
+
9
+ skip_create
10
+ initialize_with do
11
+ new language: language, region: region, tag: tag
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :dictionary_killer_service, class: LittleWeasel::Services::DictionaryKillerService do
5
+ dictionary_key { create(:dictionary_key) }
6
+ dictionary_cache { {} }
7
+ dictionary_metadata { {} }
8
+
9
+ skip_create
10
+ initialize_with do
11
+ new(dictionary_key: dictionary_key, dictionary_cache: dictionary_cache, dictionary_metadata: dictionary_metadata)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :dictionary_loader_service, class: LittleWeasel::Services::DictionaryLoaderService do
5
+ dictionary_key { create(:dictionary_key) }
6
+ dictionary_cache { {} }
7
+ dictionary_metadata { {} }
8
+
9
+ skip_create
10
+ initialize_with do
11
+ new(dictionary_key: dictionary_key, dictionary_cache: dictionary_cache, dictionary_metadata: dictionary_metadata)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :dictionary_manager, class: LittleWeasel::DictionaryManager do
5
+ skip_create
6
+ initialize_with do
7
+ new
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :dictionary_metadata, class: LittleWeasel::Metadata::DictionaryMetadata do
5
+ dictionary_words {}
6
+ dictionary_key { create(:dictionary_key) }
7
+ dictionary_cache { {} }
8
+ dictionary_metadata { {} }
9
+
10
+ skip_create
11
+ initialize_with do
12
+ dictionary_hash = dictionary_words || create(:dictionary_hash)
13
+ new dictionary_words: dictionary_hash, dictionary_key: dictionary_key, dictionary_cache: dictionary_cache, dictionary_metadata: dictionary_metadata
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: false
2
+
3
+ require 'pry'
4
+
5
+ FactoryBot.define do
6
+ factory :dictionary_metadata_service, class: LittleWeasel::Services::DictionaryMetadataService do
7
+ dictionary_key { create(:dictionary_key) }
8
+ dictionary_cache { {} }
9
+ dictionary_metadata { {} }
10
+
11
+ skip_create
12
+ initialize_with do
13
+ new(dictionary_key: dictionary_key, dictionary_cache: dictionary_cache, dictionary_metadata: dictionary_metadata)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :numeric_filter, class: LittleWeasel::Filters::EnUs::NumericFilter do
5
+ filter_on { true }
6
+
7
+ skip_create
8
+ initialize_with do
9
+ new filter_on: filter_on
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :preprocessed_word, class: LittleWeasel::Preprocessors::PreprocessedWord do
5
+ original_word {}
6
+ preprocessed {}
7
+ preprocessed_word {}
8
+ preprocessor {}
9
+ preprocessor_order {}
10
+
11
+ skip_create
12
+ initialize_with do
13
+ new original_word: original_word, preprocessed: preprocessed, preprocessed_word: preprocessed_word, preprocessor: preprocessor, preprocessor_order: preprocessor_order
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: false
2
+
3
+ require 'pry'
4
+
5
+ FactoryBot.define do
6
+ factory :preprocessed_words, class: LittleWeasel::Preprocessors::PreprocessedWords do
7
+ original_word { 'word' }
8
+ preprocessed_words {}
9
+
10
+ transient do
11
+ with_word_processors { 0 }
12
+ end
13
+
14
+ skip_create
15
+ initialize_with do
16
+ new original_word: original_word, preprocessed_words: preprocessed_words
17
+ end
18
+
19
+ after :create do |preprocessed_words, evaluator|
20
+ with_word_processors = evaluator.with_word_processors
21
+
22
+ if with_word_processors.positive?
23
+ preprocessed_words.preprocessed_words = []
24
+ preprocessed_word = evaluator.original_word
25
+
26
+ with_word_processors.times do |index|
27
+ preprocessed_word_object = create(:preprocessed_word,
28
+ original_word: preprocessed_word,
29
+ preprocessed: true,
30
+ preprocessed_word: "#{preprocessed_word}-#{index}").tap do |preprocessed_word_object|
31
+ preprocessed_word_object.preprocessed_word = "#{preprocessed_word}-#{index}"
32
+ preprocessed_word_object.preprocessor = "preprocesor#{index}"
33
+ preprocessed_word_object.preprocessor_order = index
34
+ end
35
+ preprocessed_words.preprocessed_words << preprocessed_word_object
36
+ preprocessed_word = preprocessed_word_object.preprocessed_word
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :single_character_word_filter, class: LittleWeasel::Filters::EnUs::SingleCharacterWordFilter do
5
+ filter_on { true }
6
+
7
+ skip_create
8
+ initialize_with do
9
+ new filter_on: filter_on
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: false
2
+
3
+ FactoryBot.define do
4
+ factory :word_results, class: LittleWeasel::WordResults do
5
+ original_word {}
6
+ filters_matched { [] }
7
+ preprocessed_words {}
8
+ word_cached { false }
9
+ word_valid { false }
10
+
11
+ skip_create
12
+ initialize_with do
13
+ new filters_matched: filters_matched, original_word: original_word, preprocessed_words: preprocessed_words, word_cached: word_cached, word_valid: word_valid
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,248 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe LittleWeasel::BlockResults do
6
+ subject { described_class.new original_word_block: original_word_block }
7
+
8
+ def ceate_word_results(word:, word_valid: false, word_cached: false, filters_matched: [], preprocessed_words: nil)
9
+ create(:word_results, original_word: word, word_valid: word_valid, word_cached: word_cached, filters_matched: filters_matched, preprocessed_words: preprocessed_words)
10
+ end
11
+
12
+ let(:original_word_block) { 'Original word block' }
13
+
14
+ #.new
15
+ describe '.new' do
16
+ it 'instantiates an object' do
17
+ expect { subject }.to_not raise_error
18
+ end
19
+
20
+ it 'initializes #word_results to an empty Array' do
21
+ expect(subject.word_results).to eq []
22
+ end
23
+
24
+ it 'initializes #original_word_block to the original_word_block argument passed' do
25
+ expect(subject.original_word_block).to eq original_word_block
26
+ end
27
+ end
28
+
29
+ #<<
30
+ describe '<<' do
31
+ context 'with an invalid argument' do
32
+ it 'raises an error' do
33
+ expect { subject << :not_a_word_results_object }.to raise_error(/Argument word_result is not a WordResults object/)
34
+ end
35
+ end
36
+
37
+ context 'with a valid argument' do
38
+ it 'adds the WordResults object to the #word_results Array' do
39
+ expect { subject << ceate_word_results(word: 'word01') }.to \
40
+ change { subject.word_results.count }.from(0).to(1)
41
+ end
42
+ end
43
+ end
44
+
45
+ #success?
46
+ describe '#success?' do
47
+ before do
48
+ subject << ceate_word_results(word: 'word01', word_valid: true)
49
+ subject << ceate_word_results(word: 'word02', word_valid: false, filters_matched: [:matched_filter])
50
+ end
51
+
52
+ context 'when all WordResults#successful? return true' do
53
+ before do
54
+ subject << ceate_word_results(word: 'word03', word_valid: false, filters_matched: [:matched_filter])
55
+ subject << ceate_word_results(word: 'word04', word_valid: false, filters_matched: [:matched_filter])
56
+ end
57
+
58
+ it 'returns true' do
59
+ expect(subject.success?).to be true
60
+ end
61
+ end
62
+
63
+ context 'when any WordResults#word_valid? objects return false' do
64
+ before do
65
+ subject << ceate_word_results(word: 'word03', word_valid: false)
66
+ end
67
+
68
+ it 'returns false' do
69
+ expect(subject.success?).to be false
70
+ end
71
+ end
72
+
73
+ context 'when any WordResults#filter_match? objects return false' do
74
+ before do
75
+ subject << ceate_word_results(word: 'word03')
76
+ end
77
+
78
+ it 'returns false' do
79
+ expect(subject.success?).to be false
80
+ end
81
+ end
82
+ end
83
+
84
+ #words_valid?
85
+ describe '#words_valid?' do
86
+ before do
87
+ subject << ceate_word_results(word: 'word01', word_valid: true)
88
+ subject << ceate_word_results(word: 'word02', word_valid: true)
89
+ end
90
+
91
+ context 'when all WordResults#word_valid? objects return true' do
92
+ it 'returns true' do
93
+ expect(subject.words_valid?).to be true
94
+ end
95
+ end
96
+
97
+ context 'when any WordResults#word_valid? objects return false' do
98
+ before do
99
+ subject << ceate_word_results(word: 'word03', word_valid: false)
100
+ end
101
+
102
+ it 'returns false' do
103
+ expect(subject.words_valid?).to be false
104
+ end
105
+ end
106
+ end
107
+
108
+ #filters_match?
109
+ describe '#filters_match?' do
110
+ before do
111
+ subject << ceate_word_results(word: 'word01', filters_matched: [:matched_filter])
112
+ subject << ceate_word_results(word: 'word02', filters_matched: [:matched_filter])
113
+ end
114
+
115
+ context 'when all WordResults#filters_match? objects return true' do
116
+ it 'returns true' do
117
+ expect(subject.filters_match?).to be true
118
+ end
119
+ end
120
+
121
+ context 'when any WordResults#filters_match? objects return false' do
122
+ before do
123
+ subject << ceate_word_results(word: 'word03', filters_matched: [])
124
+ end
125
+
126
+ it 'returns false' do
127
+ expect(subject.filters_match?).to be false
128
+ end
129
+ end
130
+ end
131
+
132
+ #preprocessed_words?
133
+ describe '#preprocessed_words?' do
134
+ before do
135
+ subject << ceate_word_results(word: word, preprocessed_words: preprocessed_words)
136
+ end
137
+
138
+ let(:word) { 'word' }
139
+ let(:preprocessed_words) { create(:preprocessed_words, original_word: word, with_word_processors: 1) }
140
+
141
+ context 'when all WordResults#preprocessed_word? objects return true' do
142
+ it 'returns true' do
143
+ expect(subject.preprocessed_words?).to be true
144
+ end
145
+ end
146
+ end
147
+
148
+ #preprocessed_words_or_original_words
149
+ describe '#preprocessed_words_or_original_words' do
150
+ context 'with all preprocessed words' do
151
+ before do
152
+ subject << ceate_word_results(word: words[0], preprocessed_words: preprocessed_words[0])
153
+ subject << ceate_word_results(word: words[1], preprocessed_words: preprocessed_words[1])
154
+ end
155
+
156
+ let(:words) { %w(word1 word2) }
157
+ let(:preprocessed_words) do
158
+ [
159
+ create(:preprocessed_words, original_word: words[0], with_word_processors: 1),
160
+ create(:preprocessed_words, original_word: words[1], with_word_processors: 2)
161
+ ]
162
+ end
163
+
164
+ it 'returns all the preprocessed words' do
165
+ expect(subject.preprocessed_words_or_original_words).to eq %w(word1-0 word2-0-1)
166
+ end
167
+ end
168
+
169
+ context 'with all original words' do
170
+ before do
171
+ subject << ceate_word_results(word: words[0])
172
+ subject << ceate_word_results(word: words[1])
173
+ end
174
+
175
+ let(:words) { %w(word1 word2) }
176
+
177
+ it 'returns all the original words' do
178
+ expect(subject.preprocessed_words_or_original_words).to eq %w(word1 word2)
179
+ end
180
+ end
181
+
182
+ context 'with mixed original words and preprocessed words' do
183
+ before do
184
+ subject << ceate_word_results(word: words[0], word_valid: true)
185
+ subject << ceate_word_results(word: words[1], word_valid: true, preprocessed_words: preprocessed_words[0])
186
+ subject << ceate_word_results(word: words[2], word_valid: true)
187
+ subject << ceate_word_results(word: words[3], word_valid: true, preprocessed_words: preprocessed_words[1])
188
+ end
189
+
190
+ let(:words) { %w(word0 word1 word2 word3) }
191
+ let(:preprocessed_words) do
192
+ [
193
+ create(:preprocessed_words, original_word: words[1], with_word_processors: 1),
194
+ create(:preprocessed_words, original_word: words[3], with_word_processors: 2)
195
+ ]
196
+ end
197
+
198
+ it 'returns all the original and preprocessed words' do
199
+ expect(subject.preprocessed_words_or_original_words).to eq %w(word0 word1-0 word2 word3-0-1)
200
+ end
201
+ end
202
+ end
203
+
204
+ #words_cached?
205
+ describe '#words_cached?' do
206
+ before do
207
+ subject << ceate_word_results(word: words[0])
208
+ subject << ceate_word_results(word: words[1])
209
+ subject << ceate_word_results(word: words[2])
210
+ subject << ceate_word_results(word: words[3])
211
+ end
212
+
213
+ let(:words) { %w(word1 word2 word3 word4) }
214
+
215
+ context 'when all the words are cached' do
216
+ before do
217
+ allow_any_instance_of(LittleWeasel::WordResults).to receive(:word_cached?).and_return(true)
218
+ end
219
+
220
+ it 'returns true' do
221
+ expect(subject.words_cached?).to eq true
222
+ end
223
+ end
224
+
225
+ context 'when some of the words are cached' do
226
+ before do
227
+ allow(subject.word_results[0]).to receive(:word_cached?).and_return(false)
228
+ allow(subject.word_results[1]).to receive(:word_cached?).and_return(true)
229
+ allow(subject.word_results[2]).to receive(:word_cached?).and_return(false)
230
+ allow(subject.word_results[3]).to receive(:word_cached?).and_return(true)
231
+ end
232
+
233
+ it 'returns false' do
234
+ expect(subject.words_cached?).to eq false
235
+ end
236
+ end
237
+
238
+ context 'when NONE of the words are cached' do
239
+ before do
240
+ allow_any_instance_of(LittleWeasel::WordResults).to receive(:word_cached?).and_return(false)
241
+ end
242
+
243
+ it 'returns false' do
244
+ expect(subject.words_cached?).to eq false
245
+ end
246
+ end
247
+ end
248
+ end