Sha256: 38a9a6b4efce5776a5a1dc3fe1c53c104a3874c01105ce27aa281d41831bf062

Contents?: true

Size: 1.57 KB

Versions: 21

Compression:

Stored size: 1.57 KB

Contents

require 'i18n'

class Timespan
	class << self
		attr_writer :time_format

		def time_format
			@time_format ||= "%d %b %Y"
		end
	end

	module Printer
		# locale-dependent terminology
		# %~s, %~m, %~h, %~d and %~w
		#
		# %td  => total days
  	# %th  => total hours
  	# %tm  => total minutes
  	# %ts  => total seconds
		def to_s mode = :full
			meth = "print_#{mode}"
			raise ArgumentError, "Print mode not supported, was: #{mode}" unless respond_to?(meth)
			send(meth)
		end

		def print_dates
			"#{i18n_t 'from'} #{print :start_time} #{i18n_t 'to'} #{print :end_time}"
		end

		def print_duration
			print :duration
		end

		def print_full
			 [print_dates, i18n_t('lasting'), print_duration].join(' ')
		end

		protected

		def i18n_t label
			I18n.t(label, :scope => :timespan, :default => label.to_s)
		end

		def print type
			return duration.format(duration_format) if type == :duration
			raise ArgumentError, "Not a valid print type, was: #{type}" unless valid_print_type? type
			send(type).strftime(time_format)
		end
	
		def valid_print_type? type
			%w{start_time end_time}.include? type.to_s
		end

		def time_format
			Timespan.time_format
		end

		def duration_format
			identifiers = []
			identifiers << 'y' if (duration.years > 0)
			identifiers << 'o' if (duration.months > 0)
			identifiers << 'w' if (duration.weeks > 0)
			identifiers << 'd' if (duration.days > 0)
			identifiers << 'h' if (duration.hours > 0)
			identifiers << 'm' if (duration.minutes > 0)
			identifiers << 's' if (duration.seconds > 0)

			identifiers.map {|id| "%#{id} %~#{id}" }.join(' ')
		end
	end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
timespan-0.5.1 lib/timespan/printer.rb
timespan-0.5.0 lib/timespan/printer.rb
timespan-0.4.9 lib/timespan/printer.rb
timespan-0.4.6 lib/timespan/printer.rb
timespan-0.4.5 lib/timespan/printer.rb
timespan-0.4.4 lib/timespan/printer.rb
timespan-0.4.3 lib/timespan/printer.rb
timespan-0.4.2 lib/timespan/printer.rb
timespan-0.4.1 lib/timespan/printer.rb
timespan-0.4.0 lib/timespan/printer.rb
timespan-0.3.2 lib/timespan/printer.rb
timespan-0.3.1 lib/timespan/printer.rb
timespan-0.2.8 lib/timespan/printer.rb
timespan-0.2.7 lib/timespan/printer.rb
timespan-0.2.6 lib/timespan/printer.rb
timespan-0.2.5 lib/timespan/printer.rb
timespan-0.2.4 lib/timespan/printer.rb
timespan-0.2.3 lib/timespan/printer.rb
timespan-0.2.2 lib/timespan/printer.rb
timespan-0.2.1 lib/timespan/printer.rb