ruby-duration 0.1.8 → 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.
data/.gitignore CHANGED
@@ -19,4 +19,6 @@ rdoc
19
19
  pkg
20
20
 
21
21
  ## PROJECT::SPECIFIC
22
- .document
22
+ .document
23
+ .yardoc
24
+ doc
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 Jose Peleteiro
1
+ Copyright (c) 2010 Jose Peleteiro
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ ruby-duration
2
+ =============
3
+
4
+ Duration is an immutable type that represents some amount of time with accuracy in seconds.
5
+
6
+ A lot of the code and inspirations is borrowed from [duration](http://rubyforge.org/projects/duration)
7
+ lib, which is a **mutable** Duration type, with a lot more features but lack of test. I decide to create
8
+ a new one because and belive it's better to be immutable and the old project is still on rubyforge's subversion.
9
+
10
+ I'll try to contact the author of duration to make ruby-duration version 2 of duration's lib. ;)
11
+
12
+
13
+ Features
14
+ --------
15
+
16
+ * Representation of time in weeks, days, hours, minutes and seconds.
17
+ * Construtor can receive the amount of time in seconds or a Hash with unit and amount of time.
18
+ * Format method to display the time with i18n support.
19
+ * Mongoid serialization support. Use `require 'duration/mongoid'`.
20
+ * Tested on mri 1.8.7, ree 1.8.7, mri 1.9.2, jruby and rubinius. Kudos to rvm!
21
+
22
+
23
+ Show me the code
24
+ ----------------
25
+
26
+ ### constructor
27
+
28
+ Duration.new(100) => #<Duration: minutes=1, seconds=40, total=100>
29
+ Duration.new(:hours => 5, :minutes => 70) => #<Duration: hours=6, minutes=10, total=22200>
30
+
31
+ ### format
32
+
33
+ Duration.new(:weeks => 3, :days => 1).format("%w %~w and %d %~d") => "3 weeks and 1 day"
34
+ Duration.new(:weeks => 1, :days => 20).format("%w %~w and %d %~d") => "3 weeks and 6 days"
35
+
36
+ ### iso 8601 [more info](http://en.wikipedia.org/wiki/ISO_8601#Durations)
37
+
38
+ Duration.new(:weeks => 1, :days => 20).iso8601 => "P3W6DT0H0M0S"
39
+
40
+ ### Mongoid support
41
+
42
+ require 'duration/mongoid'
43
+
44
+ class MyModel
45
+ include Mongoid::Document
46
+ field :duration, type => Duration
47
+ end
48
+
49
+
50
+ Note on Patches/Pull Requests
51
+ -----------------------------
52
+
53
+ * Fork the project.
54
+ * Make your feature addition or bug fix.
55
+ * Add tests for it. This is important so I don't break it in a
56
+ future version unintentionally.
57
+ * Commit, do not mess with rakefile, version, or history.
58
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
59
+ * Send me a pull request. Bonus points for topic branches.
60
+
61
+
62
+ License
63
+ ---------
64
+ Copyright (c) 2010 Jose Peleteiro
65
+
66
+ Permission is hereby granted, free of charge, to any person obtaining
67
+ a copy of this software and associated documentation files (the
68
+ "Software"), to deal in the Software without restriction, including
69
+ without limitation the rights to use, copy, modify, merge, publish,
70
+ distribute, sublicense, and/or sell copies of the Software, and to
71
+ permit persons to whom the Software is furnished to do so, subject to
72
+ the following conditions:
73
+
74
+ The above copyright notice and this permission notice shall be
75
+ included in all copies or substantial portions of the Software.
76
+
77
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
78
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
79
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
80
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
81
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
82
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
83
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -39,6 +39,22 @@ rescue LoadError
39
39
  end
40
40
  end
41
41
 
42
+ namespace(:test) do
43
+ desc "Run tests on multiple ruby versions"
44
+ task(:portability) do
45
+ versions = %w{system 1.8.7 ree-1.8.7 1.9.2 jruby rubinius}
46
+ versions.each do |version|
47
+ system <<-BASH
48
+ bash -c 'source ~/.rvm/scripts/rvm;
49
+ rvm use #{version};
50
+ echo "-------- `ruby -v` ---------\n";
51
+ gem install jeweler activesupport minitest yard i18n
52
+ rake -s test'
53
+ BASH
54
+ end
55
+ end
56
+ end
57
+
42
58
  task :test => :check_dependencies
43
59
 
44
60
  task :default => :test
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.8
1
+ 0.2.0
data/lib/duration.rb CHANGED
@@ -9,10 +9,8 @@
9
9
  class Duration
10
10
  include Comparable
11
11
 
12
- # Unit names
13
12
  UNITS = [:seconds, :minutes, :hours, :days, :weeks]
14
13
 
15
- # Unit labels
16
14
  UNIT_LABELS = {:second => 'second',
17
15
  :seconds => 'seconds',
18
16
  :minute => 'minute',
@@ -23,8 +21,7 @@ class Duration
23
21
  :days => 'days',
24
22
  :week => 'week',
25
23
  :weeks => 'weeks'}
26
-
27
- # Unit multiples
24
+
28
25
  MULTIPLES = {:seconds => 1,
29
26
  :minutes => 60,
30
27
  :hours => 3600,
@@ -36,9 +33,8 @@ class Duration
36
33
  :day => 86400,
37
34
  :week => 604800}
38
35
 
36
+ attr_reader :weeks, :days, :hours, :minutes, :seconds, :total
39
37
 
40
- attr_reader :total, :seconds, :minutes, :hours, :days, :weeks
41
-
42
38
  # Initialize a duration. 'args' can be a hash or anything else. If a hash is
43
39
  # passed, it will be scanned for a key=>value pair of time units such as those
44
40
  # listed in the Duration::UNITS array or Duration::MULTIPLES hash.
@@ -46,8 +42,6 @@ class Duration
46
42
  # If anything else except a hash is passed, #to_i is invoked on that object
47
43
  # and expects that it return the number of seconds desired for the duration.
48
44
  def initialize(args = 0)
49
- # Two types of arguments are accepted. If it isn't a hash, it's converted
50
- # to an integer.
51
45
  if args.kind_of?(Hash)
52
46
  @seconds = 0
53
47
  MULTIPLES.each do |unit, multiple|
@@ -58,7 +52,6 @@ class Duration
58
52
  @seconds = args.to_i
59
53
  end
60
54
 
61
- # Calculate duration
62
55
  calculate!
63
56
  end
64
57
 
@@ -68,15 +61,18 @@ class Duration
68
61
  @total <=> other.to_i
69
62
  end
70
63
 
71
- # Formats a duration in ISO8601. See http://en.wikipedia.org/wiki/ISO_8601#Durations
64
+ # Formats a duration in ISO8601.
65
+ # @see http://en.wikipedia.org/wiki/ISO_8601#Durations
72
66
  def iso8601
73
67
  format("P%wW%dDT%hH%mM%sS")
74
68
  end
75
69
 
70
+ # @return true if total is 0
76
71
  def blank?
77
72
  @total == 0
78
73
  end
79
74
 
75
+ # @return true if total different than 0
80
76
  def present?
81
77
  !blank?
82
78
  end
@@ -97,7 +93,6 @@ class Duration
97
93
  # %~h => locale-dependent "hours" terminology
98
94
  # %~d => locale-dependent "days" terminology
99
95
  # %~w => locale-dependent "weeks" terminology
100
- #
101
96
  def format(format_str)
102
97
  identifiers = {
103
98
  'w' => @weeks,
@@ -113,8 +108,7 @@ class Duration
113
108
  '~m' => @minutes == 1 ? UNIT_LABELS[:minute] : UNIT_LABELS[:minutes],
114
109
  '~h' => @hours == 1 ? UNIT_LABELS[:hour] : UNIT_LABELS[:hours],
115
110
  '~d' => @days == 1 ? UNIT_LABELS[:day] : UNIT_LABELS[:days],
116
- '~w' => @weeks == 1 ? UNIT_LABELS[:week] : UNIT_LABELS[:weeks]
117
- }
111
+ '~w' => @weeks == 1 ? UNIT_LABELS[:week] : UNIT_LABELS[:weeks]}
118
112
 
119
113
  format_str.gsub(/%?%(w|d|h|m|s|t|H|M|S|~(?:s|m|h|d|w))/) do |match|
120
114
  match['%%'] ? match : identifiers[match[1..-1]]
@@ -2,23 +2,33 @@
2
2
  require 'duration'
3
3
  require 'active_support/core_ext'
4
4
 
5
- class Duration
6
- def self.get(seconds)
5
+ # Mongoid serialization support for Duration type.
6
+ module Duration::Mongoid
7
+
8
+ # Deserialize a Duration given the amount of seconds stored by Mongodb
9
+ #
10
+ # @param [Integer, nil] duration in seconds
11
+ # @return [Duration] deserialized Duration
12
+ def get(seconds)
7
13
  return if !seconds
8
14
  Duration.new(seconds)
9
15
  end
10
16
 
11
- def self.set(args)
12
- return if args.blank?
13
- if args.is_a?(Hash)
14
- args.delete_if{|k, v| v.blank? || !Duration::UNITS.include?(k.to_sym)}
15
- return if args.blank?
16
- Duration.new(args).to_i
17
- elsif args.respond_to?(:to_i)
18
- args.to_i
19
- else
20
- nil
17
+ # Serialize a Duration or a Hash (with duration units) or a amount of seconds to
18
+ # a BSON serializable type.
19
+ #
20
+ # @param [Duration, Hash, Integer] value
21
+ # @return [Integer] duration in seconds
22
+ def set(value)
23
+ return if value.blank?
24
+ if value.is_a?(Hash)
25
+ value.delete_if{|k, v| v.blank? || !Duration::UNITS.include?(k.to_sym)}
26
+ return if value.blank?
27
+ Duration.new(value).to_i
28
+ elsif value.respond_to?(:to_i)
29
+ value.to_i
21
30
  end
22
31
  end
23
32
  end
24
33
 
34
+ Duration.send(:extend, Duration::Mongoid)
@@ -5,21 +5,21 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ruby-duration}
8
- s.version = "0.1.8"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jose Peleteiro"]
12
- s.date = %q{2010-09-15}
12
+ s.date = %q{2010-09-16}
13
13
  s.description = %q{Duration type}
14
14
  s.email = %q{jose@peleteiro.net}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
17
- "README.rdoc"
17
+ "README.md"
18
18
  ]
19
19
  s.files = [
20
20
  ".gitignore",
21
21
  "LICENSE",
22
- "README.rdoc",
22
+ "README.md",
23
23
  "Rakefile",
24
24
  "VERSION",
25
25
  "lib/duration.rb",
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-duration
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 8
10
- version: 0.1.8
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jose Peleteiro
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-15 00:00:00 -03:00
18
+ date: 2010-09-16 00:00:00 -03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -54,11 +54,11 @@ extensions: []
54
54
 
55
55
  extra_rdoc_files:
56
56
  - LICENSE
57
- - README.rdoc
57
+ - README.md
58
58
  files:
59
59
  - .gitignore
60
60
  - LICENSE
61
- - README.rdoc
61
+ - README.md
62
62
  - Rakefile
63
63
  - VERSION
64
64
  - lib/duration.rb
data/README.rdoc DELETED
@@ -1,17 +0,0 @@
1
- = duration
2
-
3
- Description goes here.
4
-
5
- == Note on Patches/Pull Requests
6
-
7
- * Fork the project.
8
- * Make your feature addition or bug fix.
9
- * Add tests for it. This is important so I don't break it in a
10
- future version unintentionally.
11
- * Commit, do not mess with rakefile, version, or history.
12
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
- * Send me a pull request. Bonus points for topic branches.
14
-
15
- == Copyright
16
-
17
- Copyright (c) 2010 Jose Peleteiro. See LICENSE for details.