Module: Wgit::Utils
- Defined in:
- lib/wgit/utils.rb
Overview
Utility module containing generic methods.
Class Method Summary collapse
-
.each(obj_or_objs) ⇒ Object
Improved each method which takes care of singleton and enumerable objects.
-
.format_sentence_length(sentence, index, sentence_limit) ⇒ Object
Formats the sentence (modifies the receiver) and returns its value.
-
.printf_search_results(results, text = nil, case_sensitive = false, sentence_length = 80, keyword_count = 5, stream = Kernel) ⇒ Object
Prints out the search results in a search engine page format.
- .time_stamp ⇒ Object
-
.to_h(obj, ignore = []) ⇒ Object
Returns a hash created from obj’s instance vars and values.
Class Method Details
.each(obj_or_objs) ⇒ Object
Improved each method which takes care of singleton and enumerable objects. Yields one or more objects.
23 24 25 26 27 28 29 |
# File 'lib/wgit/utils.rb', line 23 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 end |
.format_sentence_length(sentence, index, sentence_limit) ⇒ Object
Formats the sentence (modifies the receiver) and returns its value. The length will be based on the sentence_limit parameter or the full length of the original sentence, which ever is less. The full sentence is returned if the sentence_limit is 0. The algorithm obviously ensures that the search value is visible somewhere in the sentence.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/wgit/utils.rb', line 36 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 < 0 or index > sentence.length raise "Incorrect index value: #{index}" end return sentence if sentence_limit == 0 start = 0 finish = sentence.length if sentence.length > sentence_limit start = index - (sentence_limit / 2) finish = index + (sentence_limit / 2) if start < 0 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) < 0 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, text = nil, case_sensitive = false, sentence_length = 80, keyword_count = 5, stream = Kernel) ⇒ Object
Prints out the search results in a search engine page format. Most of the params are passed to Document#search - see class docs. The steam param decides where the printf output is written to, and therefore must respond_to? :puts The format for each result is:
Title Keywords (if there are some) Text Snippet (showing the searched for text if provided) Url <empty_line>
87 88 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 |
# File 'lib/wgit/utils.rb', line 87 def self.printf_search_results(results, text = nil, case_sensitive = false, sentence_length = 80, keyword_count = 5, stream = Kernel) raise "stream must respond_to? :puts" unless stream.respond_to? :puts keyword_count -= 1 # Because Array's are zero indexed. results.each do |doc| sentence = if text.nil? nil else sentence = doc.search(text, sentence_length).first if sentence.nil? nil else sentence.strip.empty? ? nil : sentence end end stream.puts doc.title unless doc.keywords.empty? stream.puts doc.keywords[0..keyword_count].join(", ") end stream.puts sentence unless sentence.nil? stream.puts doc.url stream.puts end nil end |
.time_stamp ⇒ Object
7 8 9 |
# File 'lib/wgit/utils.rb', line 7 def self.time_stamp Time.new end |
.to_h(obj, ignore = []) ⇒ Object
Returns a hash created from obj’s instance vars and values.
12 13 14 15 16 17 18 19 |
# File 'lib/wgit/utils.rb', line 12 def self.to_h(obj, ignore = []) hash = {} obj.instance_variables.each do |var| next if ignore.include?(var) hash[var[1..-1].to_sym] = obj.instance_variable_get(var) end hash end |