Class: Text::Gen::Filter
- Inherits:
-
Object
- Object
- Text::Gen::Filter
- Defined in:
- lib/text/gen/filter.rb
Constant Summary collapse
- SEPARATORS =
{ "tab" => "\t", "newline" => "\n", "space" => " " }.freeze
Class Method Summary collapse
-
.constant_builder(key, str) ⇒ Object
Create a builder that always returns a constant value.
- .constant_item(str, item = nil) ⇒ Object
- .filter_by_type(filters, type) ⇒ Object
- .filter_reject(items, filters) ⇒ Object
- .filter_select(items, filters) ⇒ Object
- .filters_by_type(filters, type) ⇒ Object
- .functions(result, filters) ⇒ Object
- .pass_reject?(meta, key, value) ⇒ Boolean
- .pass_select?(meta, key, value) ⇒ Boolean
-
.replace_locale(item, locale) ⇒ Object
Replace locale is used on an item; if the item is selected and has associated meta value of “locale” and the request is using that locale value then replace the item text with the value of the meta.
- .result_reject(result, filters) ⇒ Object
- .result_select(result, filters) ⇒ Object
- .separator(filters) ⇒ Object
Class Method Details
.constant_builder(key, str) ⇒ Object
Create a builder that always returns a constant value
72 73 74 75 76 77 78 79 |
# File 'lib/text/gen/filter.rb', line 72 def constant_builder(key, str) { "key" => key, "items" => [constant_item(str)], "meta" => {}, "filters" => [] } end |
.constant_item(str, item = nil) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/text/gen/filter.rb', line 81 def constant_item(str, item = nil) segments = Segment::Parser.parse(str) citem = { "segments" => segments } if item citem["value"] = item["value"] if item["value"] citem["multiplier"] = item["multiplier"] if item["multiplier"] citem["filters"] = item["filters"] if item["filters"] citem["meta"] = item["meta"] if item["meta"] end citem end |
.filter_by_type(filters, type) ⇒ Object
140 141 142 143 144 145 |
# File 'lib/text/gen/filter.rb', line 140 def filter_by_type(filters, type) return if filters.nil? return if filters.empty? filters.find { |f| f["type"] == type } end |
.filter_reject(items, filters) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/text/gen/filter.rb', line 46 def filter_reject(items, filters) reject_filters = filters_by_type(filters, "reject") return items if reject_filters.empty? items.select do |item| reject_filters.all? { |f| pass_reject?(item["meta"], f["key"], f["value"]) } end end |
.filter_select(items, filters) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/text/gen/filter.rb', line 37 def filter_select(items, filters) select_filters = filters_by_type(filters, "select") return items if select_filters.empty? items.select do |item| select_filters.any? { |f| pass_select?(item["meta"], f["key"], f["value"]) } end end |
.filters_by_type(filters, type) ⇒ Object
147 148 149 150 151 152 |
# File 'lib/text/gen/filter.rb', line 147 def filters_by_type(filters, type) return [] if filters.nil? return filters if filters.empty? filters.select { |f| f["type"] == type } end |
.functions(result, filters) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/text/gen/filter.rb', line 8 def functions(result, filters) return result if filters.nil? || filters.empty? filters.each do |filter| case filter["type"] when "capitalize" result = Result.from(text: result.text.capitalize, type: :function, result:) when "downcase" result = Result.from(text: result.text.downcase, type: :function, result:) when "upcase" result = Result.from(text: result.text.upcase, type: :function, result:) when "titleize" result = Result.from(text: Function::Titleizer.titleize(result.text), type: :function, result:) when "pluralize" result = Result.from(text: Function::Pluralizer.pluralize(result.text), type: :function, result:) when "clear" result = Result.from(text: result.text, type: :function, result:) result.(filter["key"], filter["value"]) result when "meta" result = Result.from(text: result.text, type: :function, result:) result.merge_kv(filter["key"], filter["value"]) result end end result end |
.pass_reject?(meta, key, value) ⇒ Boolean
123 124 125 126 127 128 |
# File 'lib/text/gen/filter.rb', line 123 def pass_reject?(, key, value) return false if value == "*" && .key?(key) return false if key == "*" && .values.any? { |arr| arr.include?(value) } ![key]&.include?(value) end |
.pass_select?(meta, key, value) ⇒ Boolean
116 117 118 119 120 121 |
# File 'lib/text/gen/filter.rb', line 116 def pass_select?(, key, value) return true if value == "*" && .key?(key) return true if key == "*" && .values.any? { |arr| arr.include?(value) } [key]&.include?(value) end |
.replace_locale(item, locale) ⇒ Object
Replace locale is used on an item; if the item is selected and has associated meta value of “locale” and the request is using that locale value then replace the item text with the value of the meta. If there is more than one value, select at random. Returns a new item with locale text and preserved value/multiplier, or nil
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/text/gen/filter.rb', line 98 def replace_locale(item, locale) return if locale.nil? return if item.nil? = item["meta"] return if .nil? || .empty? locale_text = [locale.downcase]&.sample return unless locale_text # Create new item with locale text, preserving value and multiplier # new_item = constant_item(locale_text) # new_item["value"] = item["value"] if item["value"] # new_item["multiplier"] = item["multiplier"] if item["multiplier"] # new_item constant_item(locale_text, item) end |
.result_reject(result, filters) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/text/gen/filter.rb', line 63 def result_reject(result, filters) exclude_filters = filters_by_type(filters, "exclude") return result if exclude_filters.empty? return result if exclude_filters.all? { |f| pass_reject?(result., f["key"], f["value"]) } nil end |
.result_select(result, filters) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/text/gen/filter.rb', line 55 def result_select(result, filters) match_filters = filters_by_type(filters, "match") return result if match_filters.empty? return result if match_filters.any? { |f| pass_select?(result., f["key"], f["value"]) } nil end |
.separator(filters) ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/text/gen/filter.rb', line 130 def separator(filters) return "" unless filters separator_filter = filter_by_type(filters, "separator") return "" unless separator_filter return separator_filter["value"] if separator_filter["key"] == "string" SEPARATORS.fetch(separator_filter["key"], "") end |