diff --git a/README.md b/README.md index 121a22a..0122a10 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,14 @@ end_time = Time.new(2014,1) TimeDifference.between(start_time, end_time).humanize => "12 Months and 5 Days" + +The :only and :except options can be used to limit the components included + +TimeDifference.between(start_time, end_time).humanize(only: [:months]) +=> "12 Months" + +TimeDifference.between(start_time, end_time).humanize(except: [:months]) +=> "5 Days" ``` ## Contributing diff --git a/lib/time_difference.rb b/lib/time_difference.rb index 73705cf..ce70dd8 100644 --- a/lib/time_difference.rb +++ b/lib/time_difference.rb @@ -56,9 +56,22 @@ def in_general end] end - def humanize + def humanize(options={}) diff_parts = [] - in_general.each do |part,quantity| + general_hash = in_general + attribute_names = general_hash.keys + + if only = options[:only] + attribute_names &= Array(only).map(&:to_sym) + end + + if except = options[:except] + attribute_names -= Array(except).map(&:to_sym) + end + + result = general_hash.select {|part, quantity| part.in?(attribute_names) && quantity > 0 } + + result.each do |part,quantity| next if quantity <= 0 part = part.to_s.humanize @@ -78,7 +91,7 @@ def humanize end private - + def initialize(start_time, end_time) start_time = time_in_seconds(start_time) end_time = time_in_seconds(end_time) diff --git a/spec/time_difference_spec.rb b/spec/time_difference_spec.rb index 46343ff..3b723f4 100644 --- a/spec/time_difference_spec.rb +++ b/spec/time_difference_spec.rb @@ -40,7 +40,7 @@ def self.with_each_class(&block) start_time = clazz.new(2009, 11) end_time = clazz.new(2011, 1) - expect(TimeDifference.between(start_time, end_time).in_general).to eql({years: 1, months: 2, weeks: 0, days: 0, hours: 18, minutes: 0, seconds: 0}) + expect(TimeDifference.between(start_time, end_time).in_general).to eql({years: 1, months: 1, weeks: 4, days: 2, hours: 7, minutes: 41, seconds: 42}) end end end @@ -51,8 +51,30 @@ def self.with_each_class(&block) start_time = clazz.new(2009, 11) end_time = clazz.new(2011, 1) - expect(TimeDifference.between(start_time, end_time).humanize).to eql("1 Year, 2 Months and 18 Hours") + expect(TimeDifference.between(start_time, end_time).humanize).to eql("1 Year, 1 Month, 4 Weeks, 2 Days, 7 Hours, 41 Minutes and 42 Seconds") end + + it "returns a string representing the time difference for selected components using :only option" do + start_time = clazz.new(2009, 11) + end_time = clazz.new(2011, 1) + + expect(TimeDifference.between(start_time, end_time).humanize(only: [:years, :months, :minutes])).to eql("1 Year, 1 Month and 41 Minutes") + end + + it "returns a string representing the time difference for selected components using :except option" do + start_time = clazz.new(2009, 11) + end_time = clazz.new(2011, 1) + + expect(TimeDifference.between(start_time, end_time).humanize(except: [:months, :weeks, :days])).to eql("1 Year, 7 Hours, 41 Minutes and 42 Seconds") + end + + it "returns nil when same components are specified in :only and :except option" do + start_time = clazz.new(2009, 11) + end_time = clazz.new(2011, 1) + + expect(TimeDifference.between(start_time, end_time).humanize(only: [:months, :weeks], except: [:months, :weeks])).to eql(nil) + end + end end