time_units 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+
2
+
3
+ # 0.2.0
4
+
5
+ - added months and years
6
+ - human_duration now take an argument allowing to limit the number of fields returned
7
+ (ex: (2.months + 1.hour + 10.seconds).human_duration(2) => "2 months 1 hour" )
@@ -5,21 +5,25 @@ module TimeDuration
5
5
  end
6
6
 
7
7
  self.units = [
8
- [1.week, :weeks, "week", "weeks"],
9
- [1.day, :days, "day", 'days'],
10
- [1.hour, :hours, "hour", "hours"],
11
- [1.minute, :minutes, "min", "mins"],
12
- [1, :seconds, "s", "s"]
8
+ [1.year, :years, "year", "years"],
9
+ [1.month, :months, "month", "months"],
10
+ [1.week, :weeks, "week", "weeks"],
11
+ [1.day, :days, "day", "days"],
12
+ [1.hour, :hours, "hour", "hours"],
13
+ [1.minute, :minutes, "min", "mins"],
14
+ [1, :seconds, "s", "s"]
13
15
  ]
14
16
 
15
- def human_duration
16
- diff = {
17
- :weeks => 0,
18
- :days => 0,
19
- :hours => 0,
20
- :minutes => 0,
21
- :seconds => 0
22
- }
17
+ ##
18
+ # Format a duration for display.
19
+ #
20
+ # @param [Integer,nil] limit if set this will limit the
21
+ # number of fields shown (ex: 2 = 2 months 3 weeks)
22
+ #
23
+ # @return [String] the formatted duration
24
+ #
25
+ def human_duration(limit = nil)
26
+ diff = Hash.new(0)
23
27
 
24
28
  ret = []
25
29
 
@@ -45,7 +49,13 @@ module TimeDuration
45
49
 
46
50
  TimeDuration.units.each.with_index do |unit|
47
51
  unit_name = unit[1]
48
- ret << pluralize.(diff[unit_name], unit) if diff[unit_name] > 0
52
+ if diff[unit_name] > 0
53
+ ret << pluralize.(diff[unit_name], unit)
54
+
55
+ if ret.size == limit
56
+ break
57
+ end
58
+ end
49
59
  end
50
60
 
51
61
  ret.join(' ')
@@ -1,3 +1,3 @@
1
1
  module TimeUnits
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -18,5 +18,9 @@ describe 'TimeDuration' do
18
18
  (2*day1 + 21*hour1 + 3).human_duration.should == "2 days 21 hours 3 s"
19
19
 
20
20
  (2.weeks + 36.hours).human_duration.should == "2 weeks 1 day 12 hours"
21
+ (2.weeks + 36.hours).human_duration(2).should == "2 weeks 1 day"
22
+
23
+ (2.months).human_duration.should == "2 months"
24
+ (2.years + 1.month).human_duration.should == "2 years 1 month"
21
25
  end
22
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_units
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-29 00:00:00.000000000 Z
12
+ date: 2012-07-13 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email:
@@ -19,6 +19,7 @@ extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - .gitignore
22
+ - ChangeLog
22
23
  - Gemfile
23
24
  - Guardfile
24
25
  - LICENSE
@@ -46,7 +47,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
46
47
  version: '0'
47
48
  segments:
48
49
  - 0
49
- hash: -2833575916128421829
50
+ hash: 4060331108915470852
50
51
  required_rubygems_version: !ruby/object:Gem::Requirement
51
52
  none: false
52
53
  requirements:
@@ -55,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
56
  version: '0'
56
57
  segments:
57
58
  - 0
58
- hash: -2833575916128421829
59
+ hash: 4060331108915470852
59
60
  requirements: []
60
61
  rubyforge_project:
61
62
  rubygems_version: 1.8.24