richunits 0.2.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +17 -1
- data/COPYING +22 -0
- data/MANIFEST +29 -13
- data/README +50 -1
- data/RELEASE +21 -0
- data/TODO +5 -0
- data/VERSION +1 -1
- data/doc/ads/rdoc.html +12 -0
- data/doc/images/ruby-md.png +0 -0
- data/doc/images/ruby-sm.png +0 -0
- data/doc/images/ruby.png +0 -0
- data/doc/images/tiger-sm.png +0 -0
- data/doc/images/tiger_logo.png +0 -0
- data/doc/index.css +179 -0
- data/doc/index.html +163 -0
- data/lib/rich_units.rb +1 -4
- data/lib/richunits.rb +5 -0
- data/lib/richunits/bytes.rb +178 -0
- data/lib/richunits/duration.rb +290 -0
- data/lib/richunits/multipliers.rb +113 -0
- data/lib/richunits/times.rb +491 -0
- data/lib/richunits/weekdays.rb +65 -0
- data/meta/version +1 -0
- data/test/test_bytes.rb +1 -1
- data/test/test_duration.rb +54 -0
- data/test/test_multipliers.rb +1 -1
- data/test/test_times.rb +10 -9
- metadata +38 -23
- data/NEWS +0 -3
- data/lib/rich_units/bytes.rb +0 -164
- data/lib/rich_units/multipliers.rb +0 -99
- data/lib/rich_units/times.rb +0 -388
- data/lib/rich_units/weekdays.rb +0 -53
@@ -0,0 +1,65 @@
|
|
1
|
+
module RichUnits
|
2
|
+
|
3
|
+
# = Weekdays
|
4
|
+
#
|
5
|
+
# The Weekdays class provides useful weekday terminology.
|
6
|
+
|
7
|
+
class Weekdays
|
8
|
+
|
9
|
+
WEEKDAYS = 1..5 # Monday is wday 1
|
10
|
+
ONE_DAY = 60 * 60 * 24
|
11
|
+
|
12
|
+
def initialize(n)
|
13
|
+
@n = n
|
14
|
+
end
|
15
|
+
|
16
|
+
def ago(time = ::Time.now)
|
17
|
+
step :down, time
|
18
|
+
end
|
19
|
+
alias_method :until, :ago
|
20
|
+
alias_method :before, :ago
|
21
|
+
|
22
|
+
def since(time = ::Time.now)
|
23
|
+
step :up, time
|
24
|
+
end
|
25
|
+
alias_method :from_now, :since
|
26
|
+
alias_method :after, :since
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def step(direction, original_time)
|
31
|
+
result = original_time
|
32
|
+
time = ONE_DAY
|
33
|
+
|
34
|
+
compare = direction == :up ? ">" : "<"
|
35
|
+
time *= -1 if direction == :down
|
36
|
+
|
37
|
+
@n.times do
|
38
|
+
result += time until result.send(compare, original_time) && WEEKDAYS.member?(result.wday)
|
39
|
+
original_time = result
|
40
|
+
end
|
41
|
+
result
|
42
|
+
end
|
43
|
+
|
44
|
+
# = Numeric Weekday Extensions
|
45
|
+
#
|
46
|
+
module Numeric
|
47
|
+
|
48
|
+
# Works with day in terms of weekdays.
|
49
|
+
def weekdays
|
50
|
+
Weekdays.new(self)
|
51
|
+
end
|
52
|
+
|
53
|
+
alias_method :weekday, :weekdays
|
54
|
+
|
55
|
+
end#module Numeric
|
56
|
+
|
57
|
+
end#class Weekdays
|
58
|
+
|
59
|
+
end#module RichUnits
|
60
|
+
|
61
|
+
|
62
|
+
class Numeric #:nodoc:
|
63
|
+
include RichUnits::Weekdays::Numeric
|
64
|
+
end
|
65
|
+
|
data/meta/version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.5.0
|
data/test/test_bytes.rb
CHANGED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'richunits/duration'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TestDuration < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_to_a
|
7
|
+
d = RichUnits::Duration.new(24*60*60 + 1)
|
8
|
+
r = d.to_a
|
9
|
+
x = [1, 0, 0, 1]
|
10
|
+
assert_equal(x, r)
|
11
|
+
|
12
|
+
d = RichUnits::Duration.new(10*24*60*60 + 5)
|
13
|
+
r = d.to_a
|
14
|
+
x = [10, 0, 0, 5]
|
15
|
+
assert_equal(x, r)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_segmented
|
19
|
+
d = RichUnits::Duration.new(8*24*60*60 + 1)
|
20
|
+
r = d.segmented(:week, :day, :hour, :minute, :second)
|
21
|
+
x = [1, 1, 0, 0, 1]
|
22
|
+
assert_equal(x, r.to_a)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_to_h
|
26
|
+
d = RichUnits::Duration.new(24*60*60)
|
27
|
+
r = d.to_h
|
28
|
+
x = { :days=>1, :hours=>0, :minutes=>0, :seconds=>0 }
|
29
|
+
assert_equal(x, r)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_repeated_numeric_days
|
33
|
+
a = 10.days
|
34
|
+
r = 10.days.days
|
35
|
+
x = 10
|
36
|
+
assert_equal(x,r)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_repeated_numeric_years
|
40
|
+
a = 10.years
|
41
|
+
r = 10.years.years
|
42
|
+
x = 10
|
43
|
+
assert_equal(x,r)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_repeated_strftime
|
47
|
+
a = RichUnits::Duration[24*60*60 + 1]
|
48
|
+
r = a.strftime('%d:%h:%m:%s')
|
49
|
+
x = "1:0:0:1"
|
50
|
+
assert_equal(x,r)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
data/test/test_multipliers.rb
CHANGED
data/test/test_times.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'richunits/times'
|
2
2
|
require 'test/unit'
|
3
3
|
|
4
4
|
class TC_Times < Test::Unit::TestCase
|
@@ -12,37 +12,38 @@ class TC_Times < Test::Unit::TestCase
|
|
12
12
|
#end
|
13
13
|
|
14
14
|
def test_seconds
|
15
|
-
assert_equal( 60**0, 1.seconds )
|
15
|
+
assert_equal( 60**0, 1.seconds.to_i )
|
16
16
|
end
|
17
17
|
|
18
18
|
def test_minutes
|
19
|
-
assert_equal( 60**1, 1.minutes )
|
19
|
+
assert_equal( 60**1, 1.minutes.to_i )
|
20
20
|
end
|
21
21
|
|
22
22
|
def test_hours
|
23
|
-
assert_equal( 60**2, 1.hours )
|
23
|
+
assert_equal( 60**2, 1.hours.to_i )
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_days
|
27
|
-
assert_equal( 24*(60**2), 1.days )
|
27
|
+
assert_equal( 24*(60**2), 1.days.to_i )
|
28
28
|
end
|
29
29
|
|
30
30
|
def test_weeks
|
31
|
-
assert_equal( 7*24*(60**2), 1.weeks )
|
31
|
+
assert_equal( 7*24*(60**2), 1.weeks.to_i )
|
32
32
|
end
|
33
33
|
|
34
34
|
def test_fortnights
|
35
|
-
assert_equal( 14*24*(60**2), 1.fortnights )
|
35
|
+
assert_equal( 14*24*(60**2), 1.fortnights.to_i )
|
36
36
|
end
|
37
37
|
|
38
38
|
def test_months
|
39
|
-
assert_equal( 30*24*(60**2), 1.months )
|
39
|
+
assert_equal( 30*24*(60**2), 1.months.to_i )
|
40
40
|
end
|
41
41
|
|
42
42
|
def test_years
|
43
|
-
assert_equal( 365*24*(60**2), 1.years )
|
43
|
+
assert_equal( 365*24*(60**2), 1.years.to_i )
|
44
44
|
end
|
45
45
|
|
46
|
+
|
46
47
|
def test_before
|
47
48
|
t = Time.now
|
48
49
|
assert_equal( t - 1.day, 1.day.before(t) )
|
metadata
CHANGED
@@ -1,17 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: richunits
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
8
|
-
7ransUnit <transfire@gmail.com>
|
9
|
-
|
7
|
+
- 7ransUnit <transfire@gmail.com>
|
10
8
|
autorequire:
|
11
9
|
bindir: bin
|
12
10
|
cert_chain: []
|
13
11
|
|
14
|
-
date:
|
12
|
+
date: 2009-06-01 00:00:00 -04:00
|
15
13
|
default_executable:
|
16
14
|
dependencies: []
|
17
15
|
|
@@ -25,38 +23,54 @@ extra_rdoc_files:
|
|
25
23
|
- README
|
26
24
|
- MANIFEST
|
27
25
|
- CHANGES
|
26
|
+
- RELEASE
|
27
|
+
- TODO
|
28
28
|
- VERSION
|
29
|
-
-
|
29
|
+
- COPYING
|
30
30
|
files:
|
31
31
|
- doc
|
32
|
-
-
|
32
|
+
- lib
|
33
|
+
- meta
|
33
34
|
- test
|
34
|
-
- test/test_bytes.rb
|
35
|
-
- test/test_multipliers.rb
|
36
|
-
- test/test_times.rb
|
37
35
|
- CHANGES
|
36
|
+
- RELEASE
|
37
|
+
- TODO
|
38
38
|
- README
|
39
|
-
-
|
39
|
+
- VERSION
|
40
|
+
- COPYING
|
41
|
+
- doc/ads
|
42
|
+
- doc/index.css
|
43
|
+
- doc/index.html
|
44
|
+
- doc/images
|
45
|
+
- doc/ads/rdoc.html
|
46
|
+
- doc/images/ruby.png
|
47
|
+
- doc/images/ruby-md.png
|
48
|
+
- doc/images/tiger_logo.png
|
49
|
+
- doc/images/ruby-sm.png
|
50
|
+
- doc/images/tiger-sm.png
|
51
|
+
- lib/rich_units.rb
|
52
|
+
- lib/richunits
|
53
|
+
- lib/richunits.rb
|
54
|
+
- lib/richunits/times.rb
|
55
|
+
- lib/richunits/duration.rb
|
56
|
+
- lib/richunits/multipliers.rb
|
57
|
+
- lib/richunits/weekdays.rb
|
58
|
+
- lib/richunits/bytes.rb
|
40
59
|
- meta/created
|
41
60
|
- meta/repository
|
42
61
|
- meta/homepage
|
43
62
|
- meta/abstract
|
44
63
|
- meta/title
|
64
|
+
- meta/version
|
45
65
|
- meta/license
|
46
66
|
- meta/contact
|
47
|
-
-
|
48
|
-
-
|
49
|
-
-
|
50
|
-
-
|
51
|
-
-
|
52
|
-
- lib/rich_units/weekdays.rb
|
53
|
-
- lib/rich_units/bytes.rb
|
54
|
-
- VERSION
|
55
|
-
- NEWS
|
67
|
+
- test/test_duration.rb
|
68
|
+
- test/test_bytes.rb
|
69
|
+
- test/test_multipliers.rb
|
70
|
+
- test/test_times.rb
|
71
|
+
- MANIFEST
|
56
72
|
has_rdoc: true
|
57
|
-
homepage:
|
58
|
-
http://tigerops.rubyforge.org/richunits
|
59
|
-
|
73
|
+
homepage: http://tigerops.rubyforge.org/richunits
|
60
74
|
post_install_message:
|
61
75
|
rdoc_options:
|
62
76
|
- --inline-source
|
@@ -86,6 +100,7 @@ signing_key:
|
|
86
100
|
specification_version: 2
|
87
101
|
summary: Rich Kilmer's Unit system provides english-esque methods
|
88
102
|
test_files:
|
103
|
+
- test/test_duration.rb
|
89
104
|
- test/test_bytes.rb
|
90
105
|
- test/test_multipliers.rb
|
91
106
|
- test/test_times.rb
|
data/NEWS
DELETED
data/lib/rich_units/bytes.rb
DELETED
@@ -1,164 +0,0 @@
|
|
1
|
-
# TITLE:
|
2
|
-
#
|
3
|
-
# Bytes
|
4
|
-
#
|
5
|
-
# DESCRIPTION:
|
6
|
-
#
|
7
|
-
# Additional methods for Numeric class to make working with
|
8
|
-
# bits and bytes easier.
|
9
|
-
#
|
10
|
-
# COPYRIGHT:
|
11
|
-
#
|
12
|
-
# Copyright (c) 2005 Rich Kilmer
|
13
|
-
#
|
14
|
-
# LICENSE:
|
15
|
-
#
|
16
|
-
# Ruby License
|
17
|
-
#
|
18
|
-
# This module is free software. You may use, modify, and/or redistribute this
|
19
|
-
# software under the same terms as Ruby.
|
20
|
-
#
|
21
|
-
# This program is distributed in the hope that it will be useful, but WITHOUT
|
22
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
23
|
-
# FOR A PARTICULAR PURPOSE.
|
24
|
-
#
|
25
|
-
# HISTORY:
|
26
|
-
#
|
27
|
-
# Special thanks to Richard Kilmer for the orignal work.
|
28
|
-
# This library is based on the original library bytes.rb
|
29
|
-
# Copyright (c) 2004 by Rich Kilmer.
|
30
|
-
#
|
31
|
-
# Also thanks to Alexander Kellett for suggesting it be
|
32
|
-
# included in Facets.
|
33
|
-
#
|
34
|
-
# AUTHORS:
|
35
|
-
#
|
36
|
-
# - Rich Kilmer
|
37
|
-
# - Thomas Sawyer
|
38
|
-
#
|
39
|
-
# NOTES:
|
40
|
-
#
|
41
|
-
# - This library is not compatible with STICK's units.rb (an spin-off
|
42
|
-
# of Facets old units.rb library). Do not attempt to use both at the same time.
|
43
|
-
#
|
44
|
-
# TODOs:
|
45
|
-
#
|
46
|
-
# - Currently kilo, mega, etc. are all powers of two and not ten,
|
47
|
-
# which technically isn't corrent even though it is common usage.
|
48
|
-
#
|
49
|
-
# - The in_* notation is weak. If a better nomentclature is thought
|
50
|
-
# of then consider changing this.
|
51
|
-
|
52
|
-
|
53
|
-
# = Binary Multipliers
|
54
|
-
#
|
55
|
-
# Additional methods for Numeric class to make working with
|
56
|
-
# bits and bytes easier. Bits are used as the base value and
|
57
|
-
# these methods can be used to convert between different
|
58
|
-
# magnitudes.
|
59
|
-
#
|
60
|
-
# == Synopisis
|
61
|
-
#
|
62
|
-
# 1.byte #=> 8
|
63
|
-
# 2.bytes #=> 16
|
64
|
-
# 1.kilobit #=> 1024
|
65
|
-
# 1.kilobyte #=> 8192
|
66
|
-
#
|
67
|
-
# Use the in_* methods to perform the inverse operations.
|
68
|
-
#
|
69
|
-
# 8192.in_kilobytes #=> 1
|
70
|
-
# 1024.in_kilobits #=> 1
|
71
|
-
#
|
72
|
-
|
73
|
-
class Numeric
|
74
|
-
|
75
|
-
def bit ; self ; end
|
76
|
-
def bits ; self ; end
|
77
|
-
def byte ; self * 8 ; end
|
78
|
-
def bytes ; self * 8 ; end
|
79
|
-
|
80
|
-
[ 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa' ].each_with_index do |m, i|
|
81
|
-
j = i + 1
|
82
|
-
class_eval %{
|
83
|
-
def #{m}bit ; self * #{1024**j} ; end
|
84
|
-
def #{m}byte ; self * #{1024**j*8} ; end
|
85
|
-
def in_#{m}bits ; self / #{1024**j} ; end
|
86
|
-
def in_#{m}bytes ; self / #{1024**j*8} ; end
|
87
|
-
alias_method :#{m}bits, :#{m}bit
|
88
|
-
alias_method :#{m}bytes, :#{m}byte
|
89
|
-
}
|
90
|
-
end
|
91
|
-
|
92
|
-
[ 'kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi' ].each_with_index do |m, i|
|
93
|
-
j = i + 1
|
94
|
-
class_eval %{
|
95
|
-
def #{m}bit ; self * #{1024**j} ; end
|
96
|
-
def #{m}byte ; self * #{1024**j*8} ; end
|
97
|
-
def in_#{m}bits ; self / #{1024**j} ; end
|
98
|
-
def in_#{m}bytes ; self / #{1024**j*8} ; end
|
99
|
-
alias_method :#{m}bits, :#{m}bit
|
100
|
-
alias_method :#{m}bytes, :#{m}byte
|
101
|
-
}
|
102
|
-
end
|
103
|
-
|
104
|
-
# Formated string of bits proportial to size.
|
105
|
-
#
|
106
|
-
# 1024.bits_to_s #=> "1.00 kb"
|
107
|
-
# 1048576.bits_to_s #=> "1.00 mb"
|
108
|
-
# 1073741824.bits_to_s #=> "1.00 gb"
|
109
|
-
# 1099511627776.bits_to_s #=> "1.00 tb"
|
110
|
-
#
|
111
|
-
# Takes a format string to adjust output.
|
112
|
-
#
|
113
|
-
# 1024.bits_to_s('%.0f') #=> "1 kb"
|
114
|
-
#
|
115
|
-
def strfbits(fmt='%.2f')
|
116
|
-
case
|
117
|
-
when self < 1024
|
118
|
-
"#{self} bits"
|
119
|
-
when self < 1024**2
|
120
|
-
"#{fmt % (self.to_f / 1024)} kb"
|
121
|
-
when self < 1024**3
|
122
|
-
"#{fmt % (self.to_f / 1024**2)} mb"
|
123
|
-
when self < 1024**4
|
124
|
-
"#{fmt % (self.to_f / 1024**3)} gb"
|
125
|
-
when self < 1024**5
|
126
|
-
"#{fmt % (self.to_f / 1024**4)} tb"
|
127
|
-
else
|
128
|
-
"#{self} bits"
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
# Formated string of bytes proportial to size.
|
133
|
-
#
|
134
|
-
# 1024.bytes_to_s #=> "1.00 KB"
|
135
|
-
# 1048576.bytes_to_s #=> "1.00 MB"
|
136
|
-
# 1073741824.bytes_to_s #=> "1.00 GB"
|
137
|
-
# 1099511627776.bytes_to_s #=> "1.00 TB"
|
138
|
-
#
|
139
|
-
# Takes a format string to adjust output.
|
140
|
-
#
|
141
|
-
# 1024.bytes_to_s('%.0f') #=> "1 KB"
|
142
|
-
#
|
143
|
-
def strfbytes(fmt='%.2f')
|
144
|
-
case
|
145
|
-
when self < 1024
|
146
|
-
"#{self} bytes"
|
147
|
-
when self < 1024**2
|
148
|
-
"#{fmt % (self.to_f / 1024)} KB"
|
149
|
-
when self < 1024**3
|
150
|
-
"#{fmt % (self.to_f / 1024**2)} MB"
|
151
|
-
when self < 1024**4
|
152
|
-
"#{fmt % (self.to_f / 1024**3)} GB"
|
153
|
-
when self < 1024**5
|
154
|
-
"#{fmt % (self.to_f / 1024**4)} TB"
|
155
|
-
else
|
156
|
-
"#{self} bytes"
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
# deprecated
|
161
|
-
alias_method :octet_units, :strfbytes
|
162
|
-
|
163
|
-
end
|
164
|
-
|