nostradamus 0.0.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1e44655ec1d07f8c31bd5e13ca94463fde2f53b7
4
+ data.tar.gz: e97a83425711798b33ea9382471003fe76f42801
5
+ SHA512:
6
+ metadata.gz: 53b96a266aba5b9d3061da6bf437a2d4c76b6dc4ef7718636344e1445f91f28cec0a162faaba1c95c9b6eae8dae77619787e322a695e16dbeec87aade40d344d
7
+ data.tar.gz: a6e9ba4c41d4b4fdd75c96385a8adf8a3eca964bc2ab7c2f27ec88b9d39fcbf3be83c1d1b2127fd824ab9a7e75a99b73897df4ecaab65e4f38b05ca6fad74e17
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2
4
+ script: bundle exec rspec spec
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ #The Nostradamus gem
2
+
3
+ You can parse human time to seconds or convert seconds to human time.
4
+
5
+ ## Install nostradamus gem
6
+
7
+ Add `nostradamus` in Gemfile and run `bundle install`.
8
+
9
+ gem "nostradamus"
10
+
11
+ Or only execute `gem install nostradamus` on terminal.
12
+
13
+ ## How to use
14
+
15
+ ### Human time to seconds:
16
+ ```
17
+ Nostradamus.parse("10:00") # => 36000
18
+ Nostradamus.parse("10:00:00") # => 36000
19
+ ```
20
+
21
+ ### Seconds to human time:
22
+ ```
23
+ Nostradamus.humanize(36000, :short) # => "10:00"
24
+ Nostradamus.humanize(36000) # => "10:00:00"
25
+ ```
26
+
27
+ ### Sum with seconds
28
+ ```
29
+ time = Nostradamus.new("12:00") + 60
30
+ time.to_i # => 43260
31
+ time.to_s # => "12:01:00"
32
+ time.to_s(:short) # => "12:01"
33
+ ```
34
+
35
+ ### Sum with another Nostradamus instance
36
+ ```
37
+ time = Nostradamus.new("12:00") + Nostradamus.new(60)
38
+ time.to_i # => 43260
39
+ time.to_s # => "12:01:00"
40
+ time.to_s(:short) # => "12:01"
41
+ ```
42
+
43
+ ### Compare times
44
+ ```
45
+ Nostradamus.new("12:00") == 43260
46
+ Nostradamus.new("00:01:00") == Nostradamus.new(60)
47
+ ```
48
+
49
+ ### Returning a Time object
50
+ ```
51
+ Nostradamus.new("12:00").to_time # => Returns a object: Time.new(CURRENT_YEAR, CURRENT_MONTH, CURRENT_DAY, 12, 0)
52
+ Nostradamus.new("12:00").to_time(:on => Date.new(2012, 5, 1)) # => Returns a object: Time.new(2012, 5, 1, 12, 0)
53
+ ```
54
+
55
+ ## Contributions
56
+
57
+ If you want contribute, please:
58
+
59
+ * Fork the project.
60
+ * Make your feature addition or bug fix.
61
+ * Send me a pull request on Github.
62
+
63
+ ## Code Status
64
+
65
+ [![Build Status](https://travis-ci.org/caironoleto/nostradamus.svg?branch=master)](https://travis-ci.org/caironoleto/nostradamus)
data/lib/nostradamus.rb CHANGED
@@ -1,42 +1,77 @@
1
- module Nostradamus
2
- extend self
1
+ require 'date'
2
+
3
+ class Nostradamus
3
4
  HOURS_REGEX = /^([0-9]*):.*/
4
5
  MINUTES_REGEX = /[0-9]*:([0-9]*).*/
5
6
  SECONDS_REGEX = /[0-9]*:[0-9]*:([0-9]*)/
6
7
 
7
- def parse(human_time)
8
+ def self.parse(human_time)
8
9
  if human_time
9
- convert(human_time, :to => :seconds)
10
+ new(human_time).to_i
10
11
  end
11
12
  end
12
13
 
13
- def humanize(seconds, format = nil)
14
+ def self.humanize(seconds, format = nil)
14
15
  if seconds
15
- convert(seconds, :to => :human_time, :format => format)
16
+ new(seconds).to_s(format)
16
17
  end
17
18
  end
18
19
 
19
- private
20
+ def initialize(time)
21
+ @time = time
22
+ end
23
+
24
+ def ==(value)
25
+ to_i == self.class.new(value).to_i
26
+ end
27
+
28
+ def +(value)
29
+ self.class.new(time_in_seconds + value.to_i)
30
+ end
31
+
32
+ def <(value)
33
+ to_i < self.class.new(value).to_i
34
+ end
35
+
36
+ def >(value)
37
+ to_i > self.class.new(value).to_i
38
+ end
20
39
 
21
- def convert(time, params)
22
- case params[:to]
23
- when :seconds
24
- hours = extract_hours_from_human_time(time)
25
- minutes = extract_minutes_from_human_time(time)
26
- seconds = extract_seconds_from_human_time(time)
27
-
28
- (hours * 3600) + (minutes * 60) + (seconds)
29
- when :human_time
30
- hours, minutes, seconds = extract_time_from_seconds(time)
31
-
32
- if params[:format] && params[:format] == :short
33
- "#{hours}:#{"%02d" % minutes}"
34
- else
35
- "#{hours}:#{"%02d" % minutes}:#{"%02d" % seconds}"
36
- end
40
+ def to_i
41
+ time_in_seconds
42
+ end
43
+
44
+ def to_s(format = nil)
45
+ hours, minutes, seconds = extract_time_from_seconds(time_in_seconds)
46
+
47
+ if format == :short
48
+ "#{hours}:#{"%02d" % minutes}"
49
+ else
50
+ "#{hours}:#{"%02d" % minutes}:#{"%02d" % seconds}"
37
51
  end
38
52
  end
39
53
 
54
+ def to_time(params = {})
55
+ date = params[:on] || Date.today
56
+ hours, minutes, seconds = extract_time_from_seconds(time_in_seconds)
57
+
58
+ Time.zone.local(date.year, date.month, date.day, hours, minutes)
59
+ end
60
+
61
+ private
62
+
63
+ def time_in_seconds
64
+ @seconds ||= if @time.is_a?(String)
65
+ hours = extract_hours_from_human_time(@time)
66
+ minutes = extract_minutes_from_human_time(@time)
67
+ seconds = extract_seconds_from_human_time(@time)
68
+
69
+ (hours * 3600) + (minutes * 60) + (seconds)
70
+ else
71
+ @time
72
+ end
73
+ end
74
+
40
75
  def extract_hours_from_human_time(time)
41
76
  time.match(HOURS_REGEX)
42
77
  matched($1)
@@ -1,5 +1,5 @@
1
- module Nostradamus
1
+ class Nostradamus
2
2
  module Version
3
- STRING = "0.0.3"
3
+ STRING = "1.0.0"
4
4
  end
5
5
  end
data/nostradamus.gemspec CHANGED
@@ -17,5 +17,10 @@ Gem::Specification.new do |gem|
17
17
  gem.require_paths = ["lib"]
18
18
  gem.version = Nostradamus::Version::STRING
19
19
 
20
- gem.add_development_dependency 'rspec', '>= 2.11.0'
20
+ gem.required_ruby_version = '>= 2.2'
21
+
22
+ gem.add_dependency 'activesupport', '>= 5.0.0'
23
+ gem.add_dependency 'tzinfo', '>= 0.3.33'
24
+
25
+ gem.add_development_dependency 'rspec', '>= 3.5.0'
21
26
  end
@@ -1,13 +1,12 @@
1
1
  require 'spec_helper'
2
- require 'nostradamus'
3
2
 
4
3
  describe Nostradamus do
5
- context "human time to seconds" do
4
+ context "#parse" do
6
5
  it "should parse human time to seconds" do
7
- described_class.parse("12:00").should eq 43200
8
- described_class.parse("12:00:00").should eq 43200
9
- described_class.parse("12:15").should eq 44100
10
- described_class.parse("12:15:15").should eq 44115
6
+ expect(described_class.parse("12:00")).to eq 43200
7
+ expect(described_class.parse("12:00:00")).to eq 43200
8
+ expect(described_class.parse("12:15")).to eq 44100
9
+ expect(described_class.parse("12:15:15")).to eq 44115
11
10
  end
12
11
 
13
12
  it "should not raise error without value" do
@@ -17,12 +16,12 @@ describe Nostradamus do
17
16
  end
18
17
  end
19
18
 
20
- context "seconds to human time" do
19
+ context "#humanize" do
21
20
  it "should convert seconds to human time" do
22
- described_class.humanize(43200, :short).should eq "12:00"
23
- described_class.humanize(44100).should eq "12:15:00"
24
- described_class.humanize(44115).should eq "12:15:15"
25
- described_class.humanize(44115, :short).should eq "12:15"
21
+ expect(described_class.humanize(43200, :short)).to eq "12:00"
22
+ expect(described_class.humanize(44100)).to eq "12:15:00"
23
+ expect(described_class.humanize(44115)).to eq "12:15:15"
24
+ expect(described_class.humanize(44115, :short)).to eq "12:15"
26
25
  end
27
26
 
28
27
  it "should not raise error without value" do
@@ -31,4 +30,182 @@ describe Nostradamus do
31
30
  }.to_not raise_error
32
31
  end
33
32
  end
33
+
34
+ context "convert human time to seconds" do
35
+ it "should compare human time to seconds" do
36
+ expect(described_class.new("12:00")).to eq 43200
37
+ expect(described_class.new("12:00:00")).to eq 43200
38
+ expect(described_class.new("12:15")).to eq 44100
39
+ expect(described_class.new("12:15:15")).to eq 44115
40
+ end
41
+
42
+ context "less (<)" do
43
+ it "should be less than 44115 seconds" do
44
+ expect(described_class.new("12:00")).to be < 44115
45
+ end
46
+
47
+ it "should not be less than" do
48
+ expect(described_class.new("12:00")).to_not be < 40115
49
+ end
50
+ end
51
+
52
+ context "greater (<)" do
53
+ it "should be greater than" do
54
+ expect(described_class.new("12:00")).to be > 40115
55
+ end
56
+
57
+ it "should not be greater than" do
58
+ expect(described_class.new("12:00")).to_not be > 44115
59
+ end
60
+ end
61
+
62
+ context "#to_i" do
63
+ it "should return seconds" do
64
+ expect(described_class.new("12:15:15").to_i).to eq 44115
65
+ end
66
+ end
67
+
68
+ context "#to_s" do
69
+ subject do
70
+ described_class.new("12:00")
71
+ end
72
+
73
+ it "should return formatted time with seconds" do
74
+ expect(subject.to_s).to eq "12:00:00"
75
+ end
76
+
77
+ it "should return short formatted time" do
78
+ expect(subject.to_s(:short)).to eq "12:00"
79
+ end
80
+ end
81
+
82
+ context "sum seconds" do
83
+ let :time do
84
+ described_class.new("12:00") + 60
85
+ end
86
+
87
+ it "should be a instance of Nostradamus" do
88
+ expect(time).to be_instance_of Nostradamus
89
+ end
90
+
91
+ it "should sum 60 seconds" do
92
+ expect(time).to eq 43260
93
+ end
94
+ end
95
+
96
+ context "sum with another Nostradamus instance" do
97
+ let :time do
98
+ described_class.new("12:00") + described_class.new("00:00:60")
99
+ end
100
+
101
+ it "should be a instance of Nostradamus" do
102
+ expect(time).to be_instance_of Nostradamus
103
+ end
104
+
105
+ it "should sum 60 seconds" do
106
+ expect(time).to eq 43260
107
+ end
108
+ end
109
+ end
110
+
111
+ context "convert seconds to human time" do
112
+ it "should compare seconds to human time" do
113
+ expect(described_class.new(43200)).to eq "12:00"
114
+ expect(described_class.new(44100)).to eq "12:15:00"
115
+ expect(described_class.new(44100)).to eq "12:15"
116
+ expect(described_class.new(44115)).to eq "12:15:15"
117
+ end
118
+
119
+ context "less (<)" do
120
+ it "should be less than 44115 seconds" do
121
+ expect(described_class.new(43200)).to be < 44115
122
+ end
123
+
124
+ it "should not be less than" do
125
+ expect(described_class.new(43200)).to_not be < 40115
126
+ end
127
+ end
128
+
129
+ context "greater (<)" do
130
+ it "should be greater than" do
131
+ expect(described_class.new(43200)).to be > 40115
132
+ end
133
+
134
+ it "should not be greater than" do
135
+ expect(described_class.new(43200)).to_not be > 44115
136
+ end
137
+ end
138
+
139
+ context "#to_i" do
140
+ it "should return seconds" do
141
+ expect(described_class.new(44115).to_i).to eq 44115
142
+ end
143
+ end
144
+
145
+ context "#to_s" do
146
+ subject do
147
+ described_class.new(44115)
148
+ end
149
+
150
+ it "should return formatted time with seconds" do
151
+ expect(subject.to_s).to eq "12:15:15"
152
+ end
153
+
154
+ it "should return short formatted time" do
155
+ expect(subject.to_s(:short)).to eq "12:15"
156
+ end
157
+ end
158
+
159
+ context "sum seconds" do
160
+ let :time do
161
+ described_class.new(43200) + 60
162
+ end
163
+
164
+ it "should be a instance of Nostradamus" do
165
+ expect(time).to be_instance_of Nostradamus
166
+ end
167
+
168
+ it "should sum 60 seconds" do
169
+ expect(time).to eq 43260
170
+ end
171
+ end
172
+
173
+ context "sum with another Nostradamus instance" do
174
+ let :time do
175
+ described_class.new(43200) + described_class.new(60)
176
+ end
177
+
178
+ it "should be a instance of Nostradamus" do
179
+ expect(time).to be_instance_of Nostradamus
180
+ end
181
+
182
+ it "should sum 60 seconds" do
183
+ expect(time).to eq 43260
184
+ end
185
+ end
186
+ end
187
+
188
+ context "convert seconds to time object" do
189
+ context "without a base date" do
190
+ let :today do
191
+ Date.today
192
+ end
193
+
194
+ it "return a today time object" do
195
+ expect(described_class.new(43200).to_time).to eq Time.zone.local(today.year, today.month, today.day, 12, 0)
196
+ expect(described_class.new("12:00").to_time).to eq Time.zone.local(today.year, today.month, today.day, 12, 0)
197
+ end
198
+ end
199
+
200
+ context "with a base date" do
201
+ let :date do
202
+ Date.new(2012, 10, 1)
203
+ end
204
+
205
+ it "uses a date to return a time object" do
206
+ expect(described_class.new(43200).to_time(:on => date)).to eq Time.zone.local(date.year, date.month, date.day, 12, 0)
207
+ expect(described_class.new("12:00").to_time(:on => date)).to eq Time.zone.local(date.year, date.month, date.day, 12, 0)
208
+ end
209
+ end
210
+ end
34
211
  end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,12 @@
1
- # This file was generated by the `rspec --init` command. Conventionally, all
2
- # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
- # Require this file using `require "spec_helper"` to ensure that it is only
4
- # loaded once.
5
- #
6
- # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
1
+ require 'bundler/setup'
2
+ require 'active_support'
3
+ require 'active_support/core_ext'
4
+ require 'tzinfo'
5
+
6
+ require 'nostradamus'
7
+
8
+ Time.zone = 'Pacific Time (US & Canada)'
9
+
7
10
  RSpec.configure do |config|
8
11
  config.treat_symbols_as_metadata_keys_with_true_values = true
9
12
  config.run_all_when_everything_filtered = true
metadata CHANGED
@@ -1,32 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nostradamus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Cairo Noleto
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-07-25 00:00:00.000000000 Z
11
+ date: 2016-08-22 00:00:00.000000000 Z
13
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: tzinfo
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.33
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.33
14
41
  - !ruby/object:Gem::Dependency
15
42
  name: rspec
16
43
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
44
  requirements:
19
- - - ! '>='
45
+ - - ">="
20
46
  - !ruby/object:Gem::Version
21
- version: 2.11.0
47
+ version: 3.5.0
22
48
  type: :development
23
49
  prerelease: false
24
50
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
51
  requirements:
27
- - - ! '>='
52
+ - - ">="
28
53
  - !ruby/object:Gem::Version
29
- version: 2.11.0
54
+ version: 3.5.0
30
55
  description: Time calculation
31
56
  email:
32
57
  - caironoleto@gmail.com
@@ -34,9 +59,11 @@ executables: []
34
59
  extensions: []
35
60
  extra_rdoc_files: []
36
61
  files:
37
- - .gitignore
38
- - .rspec
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
39
65
  - Gemfile
66
+ - README.md
40
67
  - lib/nostradamus.rb
41
68
  - lib/nostradamus/version.rb
42
69
  - nostradamus.gemspec
@@ -44,27 +71,26 @@ files:
44
71
  - spec/spec_helper.rb
45
72
  homepage: https://github.com/caironoleto/nostradamus
46
73
  licenses: []
74
+ metadata: {}
47
75
  post_install_message:
48
76
  rdoc_options: []
49
77
  require_paths:
50
78
  - lib
51
79
  required_ruby_version: !ruby/object:Gem::Requirement
52
- none: false
53
80
  requirements:
54
- - - ! '>='
81
+ - - ">="
55
82
  - !ruby/object:Gem::Version
56
- version: '0'
83
+ version: '2.2'
57
84
  required_rubygems_version: !ruby/object:Gem::Requirement
58
- none: false
59
85
  requirements:
60
- - - ! '>='
86
+ - - ">="
61
87
  - !ruby/object:Gem::Version
62
88
  version: '0'
63
89
  requirements: []
64
90
  rubyforge_project:
65
- rubygems_version: 1.8.19
91
+ rubygems_version: 2.5.1
66
92
  signing_key:
67
- specification_version: 3
93
+ specification_version: 4
68
94
  summary: Time calculation
69
95
  test_files:
70
96
  - spec/nostradamus/nostradamus_spec.rb