jiff 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c4eb447fe9a1cedb307d125b3a539bb4b9531c2a
4
- data.tar.gz: 0f512b71e0472e52bd6960692b1efd57f56511cc
3
+ metadata.gz: daee8f43df0fc3c4d69cca0a2e2dfbb2e7021fe0
4
+ data.tar.gz: 5b9a63c3a463851d5465870a9f5be7a0824f7c48
5
5
  SHA512:
6
- metadata.gz: 2b1a4823a2c04cf41cdbf740f47bccc402b4fadbd6f122319d450353aac39e51bbd8169676626c2adeb010d25a063fff1839beb92e594b70f8fb48935db263b5
7
- data.tar.gz: 5fb29273e843b3ce4425b3e700639fcb7d66c4201c5df5188edf972cc8bdf0bc7c11bf119ec51d0f1c98b01e3ad711f2aef449f59acaa1af733ecd6978ae8ded
6
+ metadata.gz: 8e94cef432291f0c14dd3157e32afb9b5f4eb189f1df4c2dace6da56087d707d85a59d4bcdf990fca2ace4b0980d98e1449aeb1b618244086660f634774f29d8
7
+ data.tar.gz: 244d48d43198c2c6e79f940b278615ffcb6cac2ee783c74aae5926100ceb733cacb69006eace31cdd9bc2fe8ea99844d323bf97f9adc8406aa87ab815232e75d
data/README.md CHANGED
@@ -28,7 +28,7 @@ Create a new **Jiff** set to the current date/time:
28
28
  Jiff.new
29
29
  ```
30
30
 
31
- Create a new **Jiff** set to an unix timestamp:
31
+ or create one using an unix timestamp:
32
32
 
33
33
  ```ruby
34
34
  Jiff.new 1414612800
@@ -193,6 +193,22 @@ This is exactly the same as **.add**, only instead of adding time, it subtracts
193
193
  |s |second |
194
194
  |ms |millisecond |
195
195
 
196
+ ### Nearest
197
+
198
+ You can round any Jiff to the nearest; hour, minute or second.
199
+
200
+ ```ruby
201
+ j = Jiff.new
202
+
203
+ j.date
204
+ # -> 2014-10-29 22:41:59 +0000
205
+
206
+ j.nearest('hour').date
207
+ # -> 2014-10-29 23:00:00 +0000
208
+ ```
209
+
210
+ To round, pass the key of the time unit you want to round to (see table above).
211
+
196
212
  ### Timezone
197
213
 
198
214
  Once you have created a **Jiff**, you can alter its timezone.
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "jiff"
7
- spec.version = '0.0.3'
7
+ spec.version = '0.0.4'
8
8
  spec.authors = ["Charlie Revett"]
9
9
  spec.email = ["charlierevett@gmail.com"]
10
10
  spec.summary = %q{Parse, validate, manipulate, and display dates in Ruby.}
@@ -1,9 +1,11 @@
1
1
  require 'time'
2
2
  require_relative 'jiff/manipulate'
3
+ require_relative 'jiff/nearest'
3
4
  require_relative 'jiff/timezone'
4
5
 
5
6
  class Jiff
6
7
  include Manipulate
8
+ include Nearest
7
9
  include Timezone
8
10
 
9
11
  attr_reader :date
@@ -1,42 +1,18 @@
1
+ require_relative 'time_unit'
2
+
1
3
  module Manipulate
2
- def add(amount, type)
3
- manipulate(amount, convert_type(type), '+')
4
+ def add(amount, key)
5
+ manipulate(amount, TimeUnit.new(key), '+')
4
6
  end
5
7
 
6
- def subtract(amount, type)
7
- manipulate(amount, convert_type(type), '-')
8
+ def subtract(amount, key)
9
+ manipulate(amount, TimeUnit.new(key), '-')
8
10
  end
9
11
 
10
12
  private
11
13
 
12
- SECONDS = {
13
- 'y' => 31556926,
14
- 'M' => 2629743.83,
15
- 'w' => 604800,
16
- 'd' => 86400,
17
- 'h' => 3600,
18
- 'm' => 60,
19
- 's' => 1,
20
- 'ms' => 0.001
21
- }
22
-
23
- def convert_type(type)
24
- return type if type.length <= 2
25
- %w(month millisecond).include?(type) ? handle_special_type(type)
26
- : type.downcase[0]
27
- end
28
-
29
- def handle_special_type(type)
30
- type == 'month' ? 'M' : 'ms'
31
- end
32
-
33
- def manipulate(amount, type, operator)
34
- raise ArgumentError, 'Invalid Type' unless type_exist? type
35
- @date = @date.send(operator, (amount * SECONDS[type]))
14
+ def manipulate(amount, time_unit, operator)
15
+ @date = @date.send(operator, (amount * time_unit.seconds))
36
16
  self
37
17
  end
38
-
39
- def type_exist?(type)
40
- SECONDS.key? type
41
- end
42
18
  end
@@ -0,0 +1,16 @@
1
+ require_relative 'time_unit'
2
+
3
+ module Nearest
4
+ def nearest(key)
5
+ @date = round TimeUnit.new(key)
6
+ self
7
+ end
8
+
9
+ private
10
+
11
+ def round(time_unit)
12
+ raise ArgumentError, 'Time unit not allowed' unless time_unit.nearest_allowed?
13
+ Time.at((@date.to_f / time_unit.seconds).round * time_unit.seconds)
14
+ end
15
+ end
16
+
@@ -0,0 +1,44 @@
1
+ class TimeUnit
2
+ def initialize(key)
3
+ convert(key).tap do |k|
4
+ raise ArgumentError, 'Invalid key' unless type_exist? k
5
+ @key = k
6
+ end
7
+ end
8
+
9
+ def nearest_allowed?
10
+ %w(d h m s).include? @key
11
+ end
12
+
13
+ def seconds
14
+ SECONDS[@key]
15
+ end
16
+
17
+ private
18
+
19
+ SECONDS = {
20
+ 'y' => 31556926,
21
+ 'M' => 2629743.83,
22
+ 'w' => 604800,
23
+ 'd' => 86400,
24
+ 'h' => 3600,
25
+ 'm' => 60,
26
+ 's' => 1,
27
+ 'ms' => 0.001
28
+ }
29
+
30
+ def convert(key)
31
+ return key if key.length <= 2
32
+ key.downcase!
33
+ %w(month millisecond).include?(key) ? handle_special(key)
34
+ : key.downcase[0]
35
+ end
36
+
37
+ def handle_special(key)
38
+ key == 'month' ? 'M' : 'ms'
39
+ end
40
+
41
+ def type_exist?(key)
42
+ SECONDS.key? key
43
+ end
44
+ end
@@ -1,7 +1,7 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
3
  describe Jiff do
4
- subject { Jiff.new }
4
+ subject { described_class.new }
5
5
  let(:time) { Time.new(1992, 6, 8, 10, 30, 15) }
6
6
 
7
7
  before { Timecop.freeze time }
@@ -0,0 +1,35 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Jiff do
4
+ subject { described_class.new }
5
+
6
+ let(:time) { Time.new(2014, 6, 8, 10, 18, 31) + 0.700 }
7
+
8
+ before { Timecop.freeze time }
9
+
10
+ after { Timecop.return }
11
+
12
+ describe '#nearest' do
13
+ context 'hour' do
14
+ let(:expected) { Time.new(2014, 6, 8, 10, 0, 0) }
15
+ specify { expect(subject.nearest('hour').date).to eq expected }
16
+ specify { expect(subject.nearest('h').date).to eq expected }
17
+ end
18
+
19
+ context 'minute' do
20
+ let(:expected) { Time.new(2014, 6, 8, 10, 19, 0) }
21
+ specify { expect(subject.nearest('minute').date).to eq expected }
22
+ specify { expect(subject.nearest('m').date).to eq expected }
23
+ end
24
+
25
+ context 'second' do
26
+ let(:expected) { Time.new(2014, 6, 8, 10, 18, 32) }
27
+ specify { expect(subject.nearest('second').date).to eq expected }
28
+ specify { expect(subject.nearest('s').date).to eq expected }
29
+ end
30
+
31
+ context 'using disallowed time unit' do
32
+ specify { expect{ subject.nearest('year') }.to raise_error ArgumentError }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,43 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe TimeUnit do
4
+ subject { described_class }
5
+ describe '.new' do
6
+ context 'using valid key' do
7
+ let(:key) { 'second' }
8
+ specify { expect(subject.new(key).seconds).to eq 1 }
9
+ end
10
+
11
+ context 'using a shorthand key' do
12
+ let(:key) { 'm' }
13
+ specify { expect(subject.new(key).seconds).to eq 60 }
14
+ end
15
+
16
+ context 'using an invalid key' do
17
+ let(:key) { 'batman' }
18
+ specify { expect{ subject.new key }.to raise_error ArgumentError }
19
+ end
20
+
21
+ context 'using a special key' do
22
+ specify { expect(subject.new('M').seconds).to eq 2629743.83 }
23
+ specify { expect(subject.new('ms').seconds).to eq 0.001 }
24
+ end
25
+ end
26
+
27
+ describe '#nearest_allowed?' do
28
+ context 'using an allowed key' do
29
+ specify { expect(subject.new('m').seconds).to eq 60 }
30
+ specify { expect(subject.new('minute').seconds).to eq 60 }
31
+ end
32
+
33
+ context 'using a disallowed key' do
34
+ let(:key) { 'batman' }
35
+ specify { expect{ subject.new(key) }.to raise_error ArgumentError }
36
+ end
37
+ end
38
+
39
+ describe '#seconds' do
40
+ let(:key) { 'day' }
41
+ specify { expect(subject.new(key).seconds).to eq 86400 }
42
+ end
43
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jiff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charlie Revett
@@ -138,10 +138,14 @@ files:
138
138
  - jiff.gemspec
139
139
  - lib/jiff.rb
140
140
  - lib/jiff/manipulate.rb
141
+ - lib/jiff/nearest.rb
142
+ - lib/jiff/time_unit.rb
141
143
  - lib/jiff/timezone.rb
142
144
  - spec/jiff_spec.rb
143
145
  - spec/manipulate_spec.rb
146
+ - spec/nearest_spec.rb
144
147
  - spec/spec_helper.rb
148
+ - spec/time_unit_spec.rb
145
149
  - spec/timezone_spec.rb
146
150
  homepage: http://github.com/revett/jiff
147
151
  licenses:
@@ -170,5 +174,7 @@ summary: Parse, validate, manipulate, and display dates in Ruby.
170
174
  test_files:
171
175
  - spec/jiff_spec.rb
172
176
  - spec/manipulate_spec.rb
177
+ - spec/nearest_spec.rb
173
178
  - spec/spec_helper.rb
179
+ - spec/time_unit_spec.rb
174
180
  - spec/timezone_spec.rb