lib/flapjack/utility.rb in flapjack-0.7.28 vs lib/flapjack/utility.rb in flapjack-0.7.29
- old
+ new
@@ -2,10 +2,11 @@
module Flapjack
module Utility
def time_period_in_words(period)
+ return "0 seconds" unless period.is_a?(Integer) && period > 0
period_mm, period_ss = period.divmod(60)
period_hh, period_mm = period_mm.divmod(60)
period_dd, period_hh = period_hh.divmod(24)
["#{period_dd} day#{plural_s(period_dd)}",
"#{period_hh} hour#{plural_s(period_hh)}",
@@ -64,9 +65,21 @@
# is all that's needed, but for Ruby 1.8 compatability, these must
# be flattened and the resulting array unpacked. flatten(1) only
# flattens the arrays constructed in the block, it won't mess up
# any values (or keys) that are themselves arrays/hashes.
Hash[ *( key_value_pairs.flatten(1) )]
+ end
+
+ # copied from ActiveSupport
+ def truncate(str, length, options = {})
+ text = str.dup
+ options[:omission] ||= "..."
+
+ length_with_room_for_omission = length - options[:omission].length
+ stop = options[:separator] ?
+ (text.rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission
+
+ (text.length > length ? text[0...stop] + options[:omission] : text).to_s
end
private
def plural_s(value)