Module: Wgit::Utils
- Defined in:
- lib/wgit/utils.rb
Overview
Utility module containing generic methods that don't belong to a Class.
Class Method Summary collapse
-
.each(obj_or_objs) {|el| ... } ⇒ Object
An improved :each method which supports both singleton and Enumerable objects (as opposed to just an Enumerable object).
-
.fetch(hash, key, default = nil) ⇒ Object
An improved Hash :fetch method which checks for multiple formats of the given key and returns the value, or the default value (nil unless provided).
-
.format_sentence_length(sentence, index, sentence_limit) ⇒ String
Formats the sentence (modifies the receiver) and returns its value.
-
.printf_search_results(results, keyword_limit: 5, stream: STDOUT) ⇒ Object
Prints out the search results in a search engine like format.
-
.process_arr(arr, encode: true) ⇒ Enumerable
Processes an Array to make it uniform.
-
.process_str(str, encode: true) ⇒ String
Processes a String to make it uniform.
-
.remove_non_bson_types(model_hash) ⇒ Hash
Returns the model having removed non bson types (for use with MongoDB).
-
.time_stamp ⇒ Time
Returns the current time stamp.
-
.to_h(obj, ignore: [], use_strings_as_keys: true) ⇒ Hash
Returns a Hash created from obj's instance vars and values.
Class Method Details
.each(obj_or_objs) {|el| ... } ⇒ Object
An improved :each method which supports both singleton and Enumerable objects (as opposed to just an Enumerable object).
40 41 42 43 44 45 46 47 48 |
# File 'lib/wgit/utils.rb', line 40 def self.each(obj_or_objs) if obj_or_objs.respond_to?(:each) obj_or_objs.each { |obj| yield(obj) } else yield(obj_or_objs) end obj_or_objs end |
.fetch(hash, key, default = nil) ⇒ Object
An improved Hash :fetch method which checks for multiple formats of the given key and returns the value, or the default value (nil unless provided).
For example, if key == :foo, hash is searched for: :foo, 'foo', 'Foo', 'FOO' in that order. The first value found is returned. If no value is found, the default value is returned.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/wgit/utils.rb', line 63 def self.fetch(hash, key, default = nil) key = key.to_s.downcase # Try (in order): :foo, 'foo', 'Foo', 'FOO'. [key.to_sym, key, key.capitalize, key.upcase].each do |k| value = hash[k] return value if value end default end |
.format_sentence_length(sentence, index, sentence_limit) ⇒ String
Formats the sentence (modifies the receiver) and returns its value. The formatting is essentially to shorten the sentence and ensure that the index is present somewhere in the sentence. Used for search query results with the index of the matching query.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/wgit/utils.rb', line 89 def self.format_sentence_length(sentence, index, sentence_limit) raise 'A sentence value must be provided' if sentence.empty? raise 'The sentence length value must be even' if sentence_limit.odd? if index.negative? || (index > sentence.length) raise "Incorrect index value: #{index}" end return sentence if sentence_limit.zero? start = 0 finish = sentence.length if sentence.length > sentence_limit start = index - (sentence_limit / 2) finish = index + (sentence_limit / 2) if start.negative? diff = 0 - start if (finish + diff) > sentence.length finish = sentence.length else finish += diff end start = 0 elsif finish > sentence.length diff = finish - sentence.length if (start - diff).negative? start = 0 else start -= diff end finish = sentence.length end raise if sentence[start..(finish - 1)].length != sentence_limit end sentence.replace(sentence[start..(finish - 1)]) end |
.printf_search_results(results, keyword_limit: 5, stream: STDOUT) ⇒ Object
Prints out the search results in a search engine like format. The format for each result looks like:
Title
Keywords (if there are some)
Text Snippet (formatted to show the searched for query, if provided)
URL
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/wgit/utils.rb', line 149 def self.printf_search_results(results, keyword_limit: 5, stream: STDOUT) raise 'stream must respond_to? :puts' unless stream.respond_to?(:puts) results.each do |doc| title = (doc.title || '<no title>') keywords = doc.keywords&.take(keyword_limit)&.join(', ') sentence = doc.text.first url = doc.url stream.puts title stream.puts keywords if keywords stream.puts sentence stream.puts url stream.puts end nil end |
.process_arr(arr, encode: true) ⇒ Enumerable
Processes an Array to make it uniform. Removes empty Strings and nils, processes non empty Strings using Wgit::Utils.process_str and removes duplicates.
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/wgit/utils.rb', line 191 def self.process_arr(arr, encode: true) if arr.is_a?(Array) arr.map! { |str| process_str(str, encode: encode) } arr.reject! { |str| str.is_a?(String) ? str.empty? : false } arr.compact! arr.uniq! end arr end |
.process_str(str, encode: true) ⇒ String
Processes a String to make it uniform. Strips any leading/trailing white
space. Also applies UTF-8 encoding (replacing invalid characters) if
encode: true.
176 177 178 179 180 181 182 183 |
# File 'lib/wgit/utils.rb', line 176 def self.process_str(str, encode: true) if str.is_a?(String) str.encode!('UTF-8', undef: :replace, invalid: :replace) if encode str.strip! end str end |
.remove_non_bson_types(model_hash) ⇒ Hash
Returns the model having removed non bson types (for use with MongoDB).
206 207 208 |
# File 'lib/wgit/utils.rb', line 206 def self.remove_non_bson_types(model_hash) model_hash.select { |_k, v| v.respond_to?(:bson_type) } end |
.time_stamp ⇒ Time
Returns the current time stamp.
9 10 11 |
# File 'lib/wgit/utils.rb', line 9 def self.time_stamp Time.new end |
.to_h(obj, ignore: [], use_strings_as_keys: true) ⇒ Hash
Returns a Hash created from obj's instance vars and values.
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/wgit/utils.rb', line 20 def self.to_h(obj, ignore: [], use_strings_as_keys: true) hash = {} obj.instance_variables.each do |var| next if ignore.include?(var.to_s) key = var.to_s[1..-1] # Remove the @ prefix. key = key.to_sym unless use_strings_as_keys hash[key] = obj.instance_variable_get(var) end hash end |