Module: TimeIntervalsToString

Defined in:
lib/time_subtract/time_intervals_to_string.rb

Class Method Summary collapse

Class Method Details

.interval_to_string(arg_hash) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/time_subtract/time_intervals_to_string.rb', line 2

def self.interval_to_string arg_hash
	# Raise Wrong Argument Type Error
	if !(arg_hash.is_a? Hash)
		raise 'WrongArgumentType'
	end

	order = [:year, :month, :day, :hour, :minute, :second]

	# Keep only selected key values
	arg_hash.keep_if{|k, v| order.include? k}

	# No Key match exception
	if arg_hash.blank?
		raise 'NoKeyMatch'
	end

	# Order the hash with our order
	ordered_hash = Hash[arg_hash.sort_by {|k, _| order.index k}]	

	out_string = [] 
	ordered_hash.each do |interval, value|
		out_string  << "#{value} #{interval.to_s.pluralize(value)}" unless value.zero?
	end
	out_string.join(' ')
end