time-lord 0.2.5 → 1.0.1
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 +34 -4
- data/.rvmrc +38 -34
- data/.travis.yml +5 -0
- data/COPYRIGHT +19 -16
- data/README.md +46 -22
- data/Rakefile +30 -1
- data/lib/time-lord.rb +6 -81
- data/lib/time-lord/extensions/integer.rb +16 -0
- data/lib/time-lord/extensions/time.rb +5 -0
- data/lib/time-lord/period.rb +54 -0
- data/lib/time-lord/scale.rb +49 -0
- data/lib/time-lord/time.rb +13 -0
- data/lib/time-lord/units.rb +11 -0
- data/lib/time-lord/units/business.rb +6 -0
- data/lib/time-lord/units/long.rb +11 -0
- data/lib/time-lord/units/special.rb +8 -0
- data/lib/time-lord/version.rb +1 -1
- data/test/helper.rb +1 -3
- data/test/lib/time-lord/extentions/integer_test.rb +24 -0
- data/test/lib/time-lord/extentions/time_test.rb +30 -0
- data/test/lib/time-lord/period_test.rb +50 -0
- data/test/lib/time-lord/scale_test.rb +34 -0
- data/test/lib/time-lord/time_test.rb +32 -0
- data/test/lib/time-lord_test.rb +12 -0
- data/time-lord.gemspec +22 -18
- metadata +87 -30
- data/.yardoc/checksums +0 -2
- data/.yardoc/objects/root.dat +0 -0
- data/.yardoc/proxy_types +0 -2
- data/doc/Time.html +0 -290
- data/doc/TimeLord.html +0 -93
- data/doc/_index.html +0 -100
- data/doc/class_list.html +0 -36
- data/doc/css/common.css +0 -1
- data/doc/css/full_list.css +0 -53
- data/doc/css/style.css +0 -318
- data/doc/file.README.html +0 -104
- data/doc/file_list.html +0 -38
- data/doc/frames.html +0 -13
- data/doc/index.html +0 -104
- data/doc/js/app.js +0 -203
- data/doc/js/full_list.js +0 -149
- data/doc/js/jquery.js +0 -16
- data/doc/method_list.html +0 -43
- data/doc/top-level-namespace.html +0 -90
data/.gitignore
CHANGED
@@ -1,4 +1,34 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
2
|
+
#
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
5
|
+
# git config --global core.excludesfile ~/.gitignore
|
6
|
+
|
7
|
+
# Ignore all of the generated gem stuff
|
8
|
+
/pkg
|
9
|
+
/*.gem
|
10
|
+
|
11
|
+
# Ignore bundler config
|
12
|
+
/.bundle
|
13
|
+
/Gemfile.lock
|
14
|
+
|
15
|
+
# Ignore all bundler caching
|
16
|
+
/vendor/cache
|
17
|
+
/vendor/ruby
|
18
|
+
|
19
|
+
# Ignore all tempfiles
|
20
|
+
/tmp
|
21
|
+
|
22
|
+
# Ignores that should be in the global gitignore
|
23
|
+
# /*.rbc
|
24
|
+
# /.config
|
25
|
+
/.yardoc
|
26
|
+
# /InstalledFiles
|
27
|
+
/_yardoc
|
28
|
+
# /coverage/
|
29
|
+
/doc/
|
30
|
+
# /lib/bundler/man/
|
31
|
+
# /rdoc/
|
32
|
+
# /spec/reports/
|
33
|
+
# /test/tmp/
|
34
|
+
# /test/version_tmp/
|
data/.rvmrc
CHANGED
@@ -3,47 +3,51 @@
|
|
3
3
|
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
4
|
# development environment upon cd'ing into the directory
|
5
5
|
|
6
|
-
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional
|
7
|
-
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.9.3" > .rvmrc
|
9
|
+
environment_id="ruby-1.9.3-p385@time-lord"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.18.6 ()" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
8
17
|
|
9
|
-
#
|
10
18
|
# First we attempt to load the desired environment directly from the environment
|
11
|
-
# file. This is very fast and
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
12
20
|
# CLI and selector. If you want feedback on which environment was used then
|
13
21
|
# insert the word 'use' after --create as this triggers verbose mode.
|
14
|
-
|
15
|
-
|
16
|
-
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
17
25
|
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
18
|
-
|
19
|
-
|
26
|
+
for __hook in "${rvm_path:-$HOME/.rvm}/hooks/after_use"*
|
27
|
+
do
|
28
|
+
if [[ -f "${__hook}" && -x "${__hook}" && -s "${__hook}" ]]
|
29
|
+
then \. "${__hook}" || true
|
30
|
+
fi
|
31
|
+
done
|
32
|
+
unset __hook
|
20
33
|
else
|
21
34
|
# If the environment file has not yet been created, use the RVM CLI to select.
|
22
|
-
rvm --create "$environment_id"
|
35
|
+
rvm --create "$environment_id" || {
|
36
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
37
|
+
return 1
|
38
|
+
}
|
23
39
|
fi
|
24
40
|
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
41
|
+
# If you use bundler, this might be useful to you:
|
42
|
+
# if [[ -s Gemfile ]] && {
|
43
|
+
# ! builtin command -v bundle >/dev/null ||
|
44
|
+
# builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
|
45
|
+
# }
|
46
|
+
# then
|
47
|
+
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
48
|
+
# gem install bundler
|
49
|
+
# fi
|
50
|
+
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
51
|
+
# then
|
52
|
+
# bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
|
33
53
|
# fi
|
34
|
-
|
35
|
-
#
|
36
|
-
# If you use bundler and would like to run bundle each time you enter the
|
37
|
-
# directory, you can uncomment the following code.
|
38
|
-
#
|
39
|
-
# # Ensure that Bundler is installed. Install it if it is not.
|
40
|
-
# if ! command -v bundle >/dev/null; then
|
41
|
-
# printf "The rubygem 'bundler' is not installed. Installing it now.\n"
|
42
|
-
# gem install bundler
|
43
|
-
# fi
|
44
|
-
#
|
45
|
-
# # Bundle while reducing excess noise.
|
46
|
-
# printf "Bundling your gems. This may take a few minutes on a fresh clone.\n"
|
47
|
-
# bundle | grep -v '^Using ' | grep -v ' is complete' | sed '/^$/d'
|
48
|
-
#
|
49
|
-
|
data/.travis.yml
ADDED
data/COPYRIGHT
CHANGED
@@ -1,19 +1,22 @@
|
|
1
|
-
Copyright (c)
|
1
|
+
Copyright (c) 2013 Kurtis Rainbolt-Greene
|
2
2
|
|
3
|
-
|
4
|
-
of this software and associated documentation files (the "Software"), to deal
|
5
|
-
in the Software without restriction, including without limitation the rights
|
6
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
copies of the Software, and to permit persons to whom the Software is
|
8
|
-
furnished to do so, subject to the following conditions:
|
3
|
+
MIT License
|
9
4
|
|
10
|
-
|
11
|
-
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
THE
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,33 +1,59 @@
|
|
1
1
|
time-lord
|
2
2
|
=========
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
- [](https://rubygems.org/gems/time-lord)
|
5
|
+
- [](https://codeclimate.com/github/krainboltgreene/time-lord)
|
6
|
+
- [](https://travis-ci.org/krainboltgreene/time-lord)
|
7
|
+
- [](https://gemnasium.com/krainboltgreene/time-lord)
|
8
|
+
- [](https://coveralls.io/r/krainboltgreene/time-lord)
|
9
9
|
|
10
|
+
`time-lord` is a gem that gives you more human like expressions for time and space math.
|
11
|
+
Underneath the covers `time-lord` gives you single purpose objects for handling the more complex parts of Time & Space.
|
12
|
+
Specifically conversion, periods between two points, and scaling.
|
10
13
|
|
11
14
|
Examples
|
12
15
|
--------
|
13
16
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
17
|
+
In `time-lord` you have Period objects:
|
18
|
+
|
19
|
+
``` ruby
|
20
|
+
1.hour.ago.class #=> <TimeLord::Period @beginning=2013-03-03 23:44:59 -0800, @ending=2013-03-04 00:44:59 -0800>
|
21
|
+
```
|
22
|
+
|
23
|
+
Periods, if forced into strings or integers, represent the number of seconds between the two points
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
1.hour.ago.to_i #=> -3600
|
27
|
+
```
|
28
|
+
|
29
|
+
Obviously Periods act a lot like a Range and can be coerced into one:
|
18
30
|
|
19
31
|
``` ruby
|
20
|
-
|
21
|
-
|
32
|
+
1.hour.ago.to_range #=> 1362383339..1362386939
|
33
|
+
```
|
22
34
|
|
23
|
-
|
35
|
+
You may have noticed the negative integer from `Period#to_i`.
|
36
|
+
This represents the period beginning before the ending.
|
37
|
+
The reverse comes from using `Period#from_now`:
|
24
38
|
|
25
|
-
|
39
|
+
``` ruby
|
40
|
+
1.hour.from_now.to_i #=> 3600
|
41
|
+
```
|
26
42
|
|
27
|
-
|
43
|
+
Periods also have a special `Period#to_words` method:
|
44
|
+
|
45
|
+
``` ruby
|
46
|
+
1.hour.ago.to_words #=> "1 hour ago"
|
47
|
+
200.minutes.ago.to_words #=> "3 hour ago"
|
48
|
+
```
|
49
|
+
|
50
|
+
Of course you can also do fun Time math:
|
51
|
+
|
52
|
+
``` ruby
|
53
|
+
Time.now #=> 2013-03-04 00:55:49 -0800
|
54
|
+
Time.now - 2.hours #=> 2013-03-03 22:55:50 -0800
|
28
55
|
```
|
29
56
|
|
30
|
-
There's a lot more to come hopefully.
|
31
57
|
Time is fun.
|
32
58
|
|
33
59
|
|
@@ -36,9 +62,7 @@ Installing
|
|
36
62
|
|
37
63
|
**Requirements**
|
38
64
|
|
39
|
-
1.
|
40
|
-
2. Any Gem manager
|
41
|
-
3. A blue telephone box
|
65
|
+
1. A blue telephone box (optional)
|
42
66
|
|
43
67
|
Like any other gem you can install it via the `gem` command:
|
44
68
|
|
@@ -49,7 +73,7 @@ $ gem install time-lord
|
|
49
73
|
Although I suggest using `bundler`:
|
50
74
|
|
51
75
|
``` ruby
|
52
|
-
gem 'time-lord', '
|
76
|
+
gem 'time-lord', '~> 1.0'
|
53
77
|
```
|
54
78
|
|
55
79
|
|
@@ -58,8 +82,8 @@ gem 'time-lord', '0.1.5'
|
|
58
82
|
To make sure you did everything right, just do the following:
|
59
83
|
|
60
84
|
```
|
61
|
-
$ ruby -e "
|
62
|
-
>
|
85
|
+
$ ruby -e "1.hour.ago.to_words"
|
86
|
+
> 1 hour ago
|
63
87
|
```
|
64
88
|
|
65
89
|
|
@@ -113,7 +137,7 @@ Credits
|
|
113
137
|
License
|
114
138
|
-------
|
115
139
|
|
116
|
-
Copyright (c)
|
140
|
+
Copyright (c) 2013 Kurtis Rainbolt-Greene
|
117
141
|
|
118
142
|
Permission is hereby granted, free of charge, to any person obtaining
|
119
143
|
a copy of this software and associated documentation files (the
|
data/Rakefile
CHANGED
@@ -1,2 +1,31 @@
|
|
1
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'yard'
|
6
|
+
|
7
|
+
begin
|
8
|
+
Bundler.setup :default, :development
|
9
|
+
rescue Bundler::BundlerError => error
|
10
|
+
$stderr.puts error.message
|
11
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
12
|
+
exit error.status_code
|
13
|
+
end
|
14
|
+
|
2
15
|
Bundler::GemHelper.install_tasks
|
16
|
+
|
17
|
+
desc "Run all of the tests"
|
18
|
+
Rake::TestTask.new do |config|
|
19
|
+
config.libs << 'test'
|
20
|
+
config.pattern = 'test/**/*_test*'
|
21
|
+
config.verbose = true
|
22
|
+
config.warning = true
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "Generate all of the docs"
|
26
|
+
YARD::Rake::YardocTask.new do |config|
|
27
|
+
config.files = Dir['lib/**/*.rb']
|
28
|
+
end
|
29
|
+
|
30
|
+
desc 'Default: run tests, and generate docs'
|
31
|
+
task :default => [ :test, :yard ]
|
data/lib/time-lord.rb
CHANGED
@@ -1,83 +1,8 @@
|
|
1
1
|
require 'time'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
Fortnight = Week * 2
|
10
|
-
Month = Week * 4
|
11
|
-
Quarter = Month * 3
|
12
|
-
Year = Month * 12
|
13
|
-
Olympiad = Year * 4
|
14
|
-
Lustrum = Year * 5
|
15
|
-
Decade = Year * 10
|
16
|
-
Indiction = Year * 15
|
17
|
-
Jubilee = Decade * 5
|
18
|
-
Century = Decade * 10
|
19
|
-
Millennium = Century * 10
|
20
|
-
Eon = 1.0/0
|
21
|
-
|
22
|
-
def ago_in_words
|
23
|
-
# Find the time difference between the time provided and the current time.
|
24
|
-
difference = get_time_difference_from self
|
25
|
-
|
26
|
-
# Catch less than 1 second differences.
|
27
|
-
return "just now" if (-1...1) === difference
|
28
|
-
|
29
|
-
name = get_unit_name_from difference
|
30
|
-
amount = get_unit_amount_from difference
|
31
|
-
count = get_unit_count_from(difference, amount).abs
|
32
|
-
|
33
|
-
# Determine if unit name needs pluralization.
|
34
|
-
name += "s" if count > 1
|
35
|
-
|
36
|
-
# Return the remaining string.
|
37
|
-
difference >= 0 ? "#{count} #{name} ago" : "in #{count} #{name}"
|
38
|
-
end
|
39
|
-
|
40
|
-
private
|
41
|
-
|
42
|
-
def get_time_difference_from time
|
43
|
-
Time.now.to_i - time.to_i
|
44
|
-
end
|
45
|
-
|
46
|
-
def get_unit_count_from difference, amount
|
47
|
-
difference / amount
|
48
|
-
end
|
49
|
-
|
50
|
-
def get_unit_name_from difference
|
51
|
-
case difference.abs
|
52
|
-
when Second...Minute then "second"
|
53
|
-
when Minute...Hour then "minute"
|
54
|
-
when Hour...Day then "hour"
|
55
|
-
when Day...Week then "day"
|
56
|
-
when Week...Month then "week"
|
57
|
-
when Month...Year then "month"
|
58
|
-
when Year..Decade then "year"
|
59
|
-
when Decade...Century then "decade"
|
60
|
-
when Century...Millennium then "century"
|
61
|
-
when Millennium...Eon then "millennium"
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def get_unit_amount_from difference
|
66
|
-
case difference.abs
|
67
|
-
when Second...Minute then Second
|
68
|
-
when Minute...Hour then Minute
|
69
|
-
when Hour...Day then Hour
|
70
|
-
when Day...Week then Day
|
71
|
-
when Week...Month then Week
|
72
|
-
when Month...Year then Month
|
73
|
-
when Year..Decade then Year
|
74
|
-
when Decade...Century then Decade
|
75
|
-
when Century...Millennium then Century
|
76
|
-
when Millennium...Eon then Millennium
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
alias_method :time_ago_in_words, :ago_in_words
|
81
|
-
alias_method :distance_in_words, :ago_in_words
|
82
|
-
alias_method :time_distance_in_words, :ago_in_words
|
83
|
-
end
|
3
|
+
require_relative 'time-lord/units'
|
4
|
+
require_relative 'time-lord/time'
|
5
|
+
require_relative 'time-lord/period'
|
6
|
+
require_relative 'time-lord/scale'
|
7
|
+
require_relative 'time-lord/extensions/integer'
|
8
|
+
require_relative 'time-lord/extensions/time'
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class Integer
|
2
|
+
TimeLord::Units.constants.each do |constant|
|
3
|
+
define_method constant.downcase do
|
4
|
+
self * TimeLord::Units.const_get(constant)
|
5
|
+
end
|
6
|
+
alias_method "#{constant.downcase}s", constant.downcase
|
7
|
+
end
|
8
|
+
|
9
|
+
def ago
|
10
|
+
TimeLord::Time.new(Time.now - self).period
|
11
|
+
end
|
12
|
+
|
13
|
+
def from_now
|
14
|
+
TimeLord::Time.new(Time.now + self).period
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module TimeLord
|
2
|
+
class Period
|
3
|
+
attr_writer :beginning, :ending
|
4
|
+
|
5
|
+
def initialize(beginning, ending)
|
6
|
+
self.beginning = beginning
|
7
|
+
self.ending = ending
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_words
|
11
|
+
"#{value} #{unit} #{tense}"
|
12
|
+
end
|
13
|
+
alias_method :in_words, :to_words
|
14
|
+
|
15
|
+
def difference
|
16
|
+
beginning - ending
|
17
|
+
end
|
18
|
+
alias_method :to_i, :difference
|
19
|
+
|
20
|
+
def to_time
|
21
|
+
if difference < 0 then @beginning else @ending end
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_range
|
25
|
+
beginning..ending
|
26
|
+
end
|
27
|
+
|
28
|
+
def beginning
|
29
|
+
@beginning.to_i
|
30
|
+
end
|
31
|
+
|
32
|
+
def ending
|
33
|
+
@ending.to_i
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def value
|
39
|
+
Scale.new(absolute).to_value
|
40
|
+
end
|
41
|
+
|
42
|
+
def unit
|
43
|
+
Scale.new(absolute).to_unit
|
44
|
+
end
|
45
|
+
|
46
|
+
def absolute
|
47
|
+
difference.abs
|
48
|
+
end
|
49
|
+
|
50
|
+
def tense
|
51
|
+
if difference < 0 then "ago" else "from now" end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|