ruby-duration 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ .document
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jose Peleteiro
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
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.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "ruby-duration"
8
+ gem.summary = %Q{Duration type}
9
+ gem.description = %Q{Duration type}
10
+ gem.email = "jose@peleteiro.net"
11
+ gem.homepage = "http://github.com/peleteiro/duration"
12
+ gem.authors = ["Jose Peleteiro"]
13
+ gem.add_development_dependency "minitest", ">= 0"
14
+ gem.add_development_dependency "yard", ">= 0"
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.rcov_opts << %w{--exclude /Library,.bundle,test}
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ begin
47
+ require 'yard'
48
+ YARD::Rake::YardocTask.new
49
+ rescue LoadError
50
+ task :yardoc do
51
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
52
+ end
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,14 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'duration'
3
+
4
+ class Duration
5
+ def get
6
+ total
7
+ end
8
+
9
+ def self.set(seconds)
10
+ return Duration.new(0) unless seconds.respond_to?(:to_i)
11
+ Duration.new(seconds.to_i)
12
+ end
13
+ end
14
+
data/lib/duration.rb ADDED
@@ -0,0 +1,133 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ # Duration objects are simple mechanisms that allow you to operate on durations
4
+ # of time. They allow you to know how much time has passed since a certain
5
+ # point in time, or they can tell you how much time something is (when given as
6
+ # seconds) in different units of time measurement. Durations would particularly
7
+ # be useful for those scripts or applications that allow you to know the uptime
8
+ # of themselves or perhaps provide a countdown until a certain event.
9
+ class Duration
10
+ include Comparable
11
+ include Enumerable
12
+
13
+ # Unit names
14
+ UNITS = [:seconds, :minutes, :hours, :days, :weeks]
15
+
16
+ # Unit labels
17
+ UNIT_LABELS = {:second => 'seconds',
18
+ :seconds => 'seconds',
19
+ :minute => 'minute',
20
+ :minutes => 'minutes',
21
+ :hour => 'hour',
22
+ :hours => 'hours',
23
+ :day => 'day',
24
+ :days => 'days',
25
+ :week => 'week',
26
+ :weeks => 'weeks'}
27
+
28
+ # Unit multiples
29
+ MULTIPLES = {:seconds => 1,
30
+ :minutes => 60,
31
+ :hours => 3600,
32
+ :days => 86400,
33
+ :weeks => 604800,
34
+ :second => 1,
35
+ :minute => 60,
36
+ :hour => 3600,
37
+ :day => 86400,
38
+ :week => 604800}
39
+
40
+
41
+ attr_reader :total, *UNITS
42
+
43
+ # Initialize a duration. 'args' can be a hash or anything else. If a hash is
44
+ # passed, it will be scanned for a key=>value pair of time units such as those
45
+ # listed in the Duration::UNITS array or Duration::MULTIPLES hash.
46
+ #
47
+ # If anything else except a hash is passed, #to_i is invoked on that object
48
+ # and expects that it return the number of seconds desired for the duration.
49
+ def initialize(args = 0)
50
+ # Two types of arguments are accepted. If it isn't a hash, it's converted
51
+ # to an integer.
52
+ if args.kind_of?(Hash)
53
+ @seconds = 0
54
+ MULTIPLES.each do |unit, multiple|
55
+ unit = unit.to_sym
56
+ @seconds += args[unit] * multiple if args.key?(unit)
57
+ end
58
+ else
59
+ @seconds = args.to_i
60
+ end
61
+
62
+ # Calculate duration
63
+ calculate!
64
+ end
65
+
66
+ # Compare this duration to another (or objects that respond to #to_i)
67
+ def <=>(other)
68
+ @total <=> other.to_i
69
+ end
70
+
71
+ # Format a duration into a human-readable string.
72
+ #
73
+ # %w => weeks
74
+ # %d => days
75
+ # %h => hours
76
+ # %m => minutes
77
+ # %s => seconds
78
+ # %t => total seconds
79
+ # %H => zero-padded hours
80
+ # %M => zero-padded minutes
81
+ # %S => zero-padded seconds
82
+ # %~s => locale-dependent "seconds" terminology
83
+ # %~m => locale-dependent "minutes" terminology
84
+ # %~h => locale-dependent "hours" terminology
85
+ # %~d => locale-dependent "days" terminology
86
+ # %~w => locale-dependent "weeks" terminology
87
+ #
88
+ def format(format_str)
89
+ identifiers = {
90
+ 'w' => @weeks,
91
+ 'd' => @days,
92
+ 'h' => @hours,
93
+ 'm' => @minutes,
94
+ 's' => @seconds,
95
+ 't' => @total,
96
+ 'H' => @hours.to_s.rjust(2, '0'),
97
+ 'M' => @minutes.to_s.rjust(2, '0'),
98
+ 'S' => @seconds.to_s.rjust(2, '0'),
99
+ '~s' => @seconds == 1 ? UNIT_LABELS[:second] : UNIT_LABELS[:seconds],
100
+ '~m' => @minutes == 1 ? UNIT_LABELS[:minute] : UNIT_LABELS[:minutes],
101
+ '~h' => @hours == 1 ? UNIT_LABELS[:hour] : UNIT_LABELS[:hours],
102
+ '~d' => @days == 1 ? UNIT_LABELS[:day] : UNIT_LABELS[:days],
103
+ '~w' => @weeks == 1 ? UNIT_LABELS[:week] : UNIT_LABELS[:weeks]
104
+ }
105
+
106
+ format_str.gsub(/%?%(w|d|h|m|s|t|H|M|S|~(?:s|m|h|d|w))/) do |match|
107
+ match['%%'] ? match : identifiers[match[1..-1]]
108
+ end.gsub('%%', '%')
109
+ end
110
+
111
+ alias_method :to_i, :total
112
+ alias_method :strftime, :format
113
+
114
+ private
115
+
116
+ # Calculates the duration from seconds and figures out what the actual
117
+ # durations are in specific units. This method is called internally, and
118
+ # does not need to be called by user code.
119
+ def calculate!
120
+ multiples = [MULTIPLES[:weeks], MULTIPLES[:days], MULTIPLES[:hours], MULTIPLES[:minutes], MULTIPLES[:seconds]]
121
+ units = []
122
+ @total = @seconds.to_f.round
123
+ multiples.inject(@total) do |total, multiple|
124
+ # Divide into largest unit
125
+ units << total / multiple
126
+ total % multiple # The remainder will be divided as the next largest
127
+ end
128
+
129
+ # Gather the divided units
130
+ @weeks, @days, @hours, @minutes, @seconds = units
131
+ end
132
+
133
+ end
@@ -0,0 +1 @@
1
+ require 'duration'
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class Duration::TestMongoid < MiniTest::Unit::TestCase
5
+
6
+ # Returns seconds to serialization
7
+ def test_get
8
+ assert_equal 90, Duration.new(90).get
9
+ end
10
+
11
+ def test_set_default
12
+ zero = Duration.new(0)
13
+ assert_equal zero, Duration.set("string")
14
+ assert_equal zero, Duration.set(nil)
15
+ end
16
+
17
+ def test_set_default
18
+ assert_equal Duration.new(10), Duration.set("10")
19
+ assert_equal Duration.new(10), Duration.set(10)
20
+ end
21
+
22
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,13 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'rubygems'
3
+ require 'minitest/unit'
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'duration'
8
+ require 'duration/mongoid'
9
+
10
+ class MiniTest::Unit::TestCase
11
+ end
12
+
13
+ MiniTest::Unit.autorun
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'helper'
3
+
4
+ class TestDuration < MiniTest::Unit::TestCase
5
+
6
+ def test_initialization_with_seconds
7
+ d = Duration.new(90)
8
+ assert_equal 0, d.weeks
9
+ assert_equal 0, d.days
10
+ assert_equal 0, d.hours
11
+ assert_equal 1, d.minutes
12
+ assert_equal 30, d.seconds
13
+ assert_equal 90, d.total
14
+ end
15
+
16
+ def test_initialization_with_hash
17
+ d = Duration.new(:minutes => 1, :seconds => 30)
18
+ assert_equal 0, d.weeks
19
+ assert_equal 0, d.days
20
+ assert_equal 0, d.hours
21
+ assert_equal 1, d.minutes
22
+ assert_equal 30, d.seconds
23
+ assert_equal 90, d.total
24
+ end
25
+
26
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-duration
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Jose Peleteiro
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-14 00:00:00 -03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: minitest
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: yard
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: Duration type
50
+ email: jose@peleteiro.net
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - LICENSE
57
+ - README.rdoc
58
+ files:
59
+ - .gitignore
60
+ - LICENSE
61
+ - README.rdoc
62
+ - Rakefile
63
+ - VERSION
64
+ - lib/duration.rb
65
+ - lib/duration/mongoid.rb
66
+ - lib/ruby-duration.rb
67
+ - test/duration/test_mongoid.rb
68
+ - test/helper.rb
69
+ - test/test_duration.rb
70
+ has_rdoc: true
71
+ homepage: http://github.com/peleteiro/duration
72
+ licenses: []
73
+
74
+ post_install_message:
75
+ rdoc_options:
76
+ - --charset=UTF-8
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.3.7
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Duration type
104
+ test_files:
105
+ - test/duration/test_mongoid.rb
106
+ - test/helper.rb
107
+ - test/test_duration.rb