Module: Birdwatcher::Util
- Defined in:
- lib/birdwatcher/util.rb
Class Method Summary collapse
- .ago_in_words_pair(secs) ⇒ Object
- .ago_in_words_singularize(pair) ⇒ Object
- .escape_html(string) ⇒ Object
- .excerpt(text, max_length, omission = "...") ⇒ Object
- .parse_time(time) ⇒ Object
- .pluralize(count, singular, plural) ⇒ Object
- .strip_control_characters(string) ⇒ Object
- .strip_html(string) ⇒ Object
- .suppress_output(&block) ⇒ Object
- .suppress_warnings(&block) ⇒ Object
- .time_ago_in_words(time) ⇒ Object
- .unescape_html(string) ⇒ Object
Class Method Details
.ago_in_words_pair(secs) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/birdwatcher/util.rb', line 13 def self.ago_in_words_pair(secs) [[60, :seconds], [60, :minutes], [24, :hours], [100_000, :days]].map{ |count, name| if secs > 0 secs, n = secs.divmod(count) "#{n.to_i} #{name}" end }.compact.reverse[0..1] end |
.ago_in_words_singularize(pair) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/birdwatcher/util.rb', line 22 def self.ago_in_words_singularize(pair) if pair.size == 1 pair.map! {|part| part[0, 2].to_i == 1 ? part.chomp("s") : part } else pair.map! {|part| part[0, 2].to_i == 1 ? part.chomp("s") : part[0, 2].to_i == 0 ? nil : part } end pair.compact end |
.escape_html(string) ⇒ Object
46 47 48 |
# File 'lib/birdwatcher/util.rb', line 46 def self.escape_html(string) CGI.escapeHTML(string.to_s) end |
.excerpt(text, max_length, omission = "...") ⇒ Object
58 59 60 61 62 |
# File 'lib/birdwatcher/util.rb', line 58 def self.excerpt(text, max_length, omission = "...") text = text.gsub(/\s/, " ").split(" ").map(&:strip).join(" ") return text if text.length < max_length text[0..max_length] + omission end |
.parse_time(time) ⇒ Object
31 32 33 |
# File 'lib/birdwatcher/util.rb', line 31 def self.parse_time(time) ::Chronic.parse(time) end |
.pluralize(count, singular, plural) ⇒ Object
54 55 56 |
# File 'lib/birdwatcher/util.rb', line 54 def self.pluralize(count, singular, plural) count == 1 ? "1 #{singular}" : "#{count} #{plural}" end |
.strip_control_characters(string) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/birdwatcher/util.rb', line 39 def self.strip_control_characters(string) string = string.to_s.uncolorize string.split("").delete_if do |char| char.ascii_only? and (char.ord < 32 or char.ord == 127) end.join("") end |
.strip_html(string) ⇒ Object
35 36 37 |
# File 'lib/birdwatcher/util.rb', line 35 def self.strip_html(string) string.to_s.gsub(/<\/?[^>]*>/, "") end |
.suppress_output(&block) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/birdwatcher/util.rb', line 64 def self.suppress_output(&block) original_stdout = $stdout $stdout = fake = StringIO.new begin yield ensure $stdout = original_stdout end fake.string end |
.suppress_warnings(&block) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/birdwatcher/util.rb', line 75 def self.suppress_warnings(&block) warn_level = $VERBOSE $VERBOSE = nil result = block.call $VERBOSE = warn_level result end |
.time_ago_in_words(time) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/birdwatcher/util.rb', line 3 def self.time_ago_in_words(time) return "a very very long time ago" if time.year < 1800 secs = Time.now - time return "just now" if secs > -1 && secs < 1 return "" if secs <= -1 pair = ago_in_words_pair(secs) ary = ago_in_words_singularize(pair) ary.size == 0 ? "" : ary.join(" and ") << " ago" end |
.unescape_html(string) ⇒ Object
50 51 52 |
# File 'lib/birdwatcher/util.rb', line 50 def self.unescape_html(string) CGI.unescapeHTML(string.to_s) end |