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.
Files changed (45) hide show
  1. data/.gitignore +34 -4
  2. data/.rvmrc +38 -34
  3. data/.travis.yml +5 -0
  4. data/COPYRIGHT +19 -16
  5. data/README.md +46 -22
  6. data/Rakefile +30 -1
  7. data/lib/time-lord.rb +6 -81
  8. data/lib/time-lord/extensions/integer.rb +16 -0
  9. data/lib/time-lord/extensions/time.rb +5 -0
  10. data/lib/time-lord/period.rb +54 -0
  11. data/lib/time-lord/scale.rb +49 -0
  12. data/lib/time-lord/time.rb +13 -0
  13. data/lib/time-lord/units.rb +11 -0
  14. data/lib/time-lord/units/business.rb +6 -0
  15. data/lib/time-lord/units/long.rb +11 -0
  16. data/lib/time-lord/units/special.rb +8 -0
  17. data/lib/time-lord/version.rb +1 -1
  18. data/test/helper.rb +1 -3
  19. data/test/lib/time-lord/extentions/integer_test.rb +24 -0
  20. data/test/lib/time-lord/extentions/time_test.rb +30 -0
  21. data/test/lib/time-lord/period_test.rb +50 -0
  22. data/test/lib/time-lord/scale_test.rb +34 -0
  23. data/test/lib/time-lord/time_test.rb +32 -0
  24. data/test/lib/time-lord_test.rb +12 -0
  25. data/time-lord.gemspec +22 -18
  26. metadata +87 -30
  27. data/.yardoc/checksums +0 -2
  28. data/.yardoc/objects/root.dat +0 -0
  29. data/.yardoc/proxy_types +0 -2
  30. data/doc/Time.html +0 -290
  31. data/doc/TimeLord.html +0 -93
  32. data/doc/_index.html +0 -100
  33. data/doc/class_list.html +0 -36
  34. data/doc/css/common.css +0 -1
  35. data/doc/css/full_list.css +0 -53
  36. data/doc/css/style.css +0 -318
  37. data/doc/file.README.html +0 -104
  38. data/doc/file_list.html +0 -38
  39. data/doc/frames.html +0 -13
  40. data/doc/index.html +0 -104
  41. data/doc/js/app.js +0 -203
  42. data/doc/js/full_list.js +0 -149
  43. data/doc/js/jquery.js +0 -16
  44. data/doc/method_list.html +0 -43
  45. data/doc/top-level-namespace.html +0 -90
data/.gitignore CHANGED
@@ -1,4 +1,34 @@
1
- *.gem
2
- .bundle
3
- Gemfile.lock
4
- pkg/*
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
- environment_id="ruby-1.9.3-p0@time-lord"
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 efficicent compared to running through the entire
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
- if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
16
- && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]] ; then
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
- [[ -s ".rvm/hooks/after_use" ]] && . ".rvm/hooks/after_use"
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
- # If you use an RVM gemset file to install a list of gems (*.gems), you can have
27
- # it be automatically loaded. Uncomment the following and adjust the filename if
28
- # necessary.
29
- #
30
- # filename=".gems"
31
- # if [[ -s "$filename" ]] ; then
32
- # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
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
-
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - rbx-19mode
5
+ - jruby-19mode
data/COPYRIGHT CHANGED
@@ -1,19 +1,22 @@
1
- Copyright (c) 2010 Kurtis Rainbolt-Greene:
1
+ Copyright (c) 2013 Kurtis Rainbolt-Greene
2
2
 
3
- Permission is hereby granted, free of charge, to any person obtaining a copy
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
- The above copyright notice and this permission notice shall be included in
11
- all copies or substantial portions of the Software.
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
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- THE SOFTWARE.
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
- **Current Version**: 0.1.5
5
-
6
- `time-lord` is a gem that builds on the Time class to give more human expressions.
7
- It adds a lot of nifty little ideas present in Rails helpers to the time class.
8
- Nothing too fancy, just a little human touch to data.
4
+ - [![Gem Version](https://badge.fury.io/rb/time-lord.png)](https://rubygems.org/gems/time-lord)
5
+ - [![Code Climate](https://codeclimate.com/github/krainboltgreene/time-lord.png)](https://codeclimate.com/github/krainboltgreene/time-lord)
6
+ - [![Build Status](https://travis-ci.org/krainboltgreene/time-lord.png)](https://travis-ci.org/krainboltgreene/time-lord)
7
+ - [![Dependency Status](https://gemnasium.com/krainboltgreene/time-lord.png)](https://gemnasium.com/krainboltgreene/time-lord)
8
+ - [![Coverage Status](https://coveralls.io/repos/krainboltgreene/time-lord/badge.png?branch=master)](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
- To use `time-lord` you need a Time, Date, or DateTime object.
15
- You can pass an object that doesn't act like those, but you'll have to tell it to try and parse.
16
- Even then it might not be able to, so be careful and just send Time-likes.
17
- Here's an example or two:
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
- Time.now.ago_in_words
21
- # => "just now"
32
+ 1.hour.ago.to_range #=> 1362383339..1362386939
33
+ ```
22
34
 
23
- olden_times = Time.parse("1942-04-01")
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
- "I was dancing with Queen Elizabeth #{olden_times.ago_in_words} from now!"
39
+ ``` ruby
40
+ 1.hour.from_now.to_i #=> 3600
41
+ ```
26
42
 
27
- # => "I was dancing with Queen Elizabeth 7 decades ago from now!"
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. Any Ruby with a Time class
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', '0.1.5'
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 "puts Time.now.ago_in_words"
62
- > just now
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) 2011 Kurtis Rainbolt-Greene
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
- require 'bundler'
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 ]
@@ -1,83 +1,8 @@
1
1
  require 'time'
2
2
 
3
- class Time
4
- Second = 1
5
- Minute = Second * 60
6
- Hour = Minute * 60
7
- Day = Hour * 24
8
- Week = Day * 7
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,5 @@
1
+ class Time
2
+ def ago
3
+ TimeLord::Time.new(self).period
4
+ end
5
+ 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