duranged 0.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.
- checksums.yaml +7 -0
- data/lib/duranged.rb +45 -0
- data/lib/duranged/base.rb +182 -0
- data/lib/duranged/duration.rb +6 -0
- data/lib/duranged/interval.rb +6 -0
- data/lib/duranged/occurrence.rb +124 -0
- data/lib/duranged/range.rb +111 -0
- data/lib/duranged/version.rb +3 -0
- data/spec/duranged/base_spec.rb +6 -0
- data/spec/duranged/duration_spec.rb +14 -0
- data/spec/duranged/interval_spec.rb +14 -0
- data/spec/duranged/occurrence_spec.rb +245 -0
- data/spec/duranged/range_spec.rb +324 -0
- data/spec/duranged_spec.rb +15 -0
- data/spec/shared/base_examples.rb +224 -0
- data/spec/spec_helper.rb +78 -0
- metadata +136 -0
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Duranged do
|
4
|
+
describe 'duration' do
|
5
|
+
it 'returns a Duranged::Duration' do
|
6
|
+
expect(subject.duration(300.seconds)).to be_an_instance_of(Duranged::Duration)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'range' do
|
11
|
+
it 'returns a Duranged::Range' do
|
12
|
+
expect(subject.range(Time.now, (Time.now + 300.seconds))).to be_an_instance_of(Duranged::Range)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,224 @@
|
|
1
|
+
RSpec.shared_examples "a hash method" do |method|
|
2
|
+
it 'returns a hash of the days, hours, minutes and seconds' do
|
3
|
+
expect(subject.send(method)).to eq({years: 0, months: 0, weeks: 0, days_after_weeks: 1, days: 1, hours: 0, minutes: 2, seconds: 2})
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
RSpec.shared_examples "a format conversion" do |period, formatter|
|
8
|
+
context "using the #{period} formatter %#{formatter}" do
|
9
|
+
it 'returns the formatted string' do
|
10
|
+
expect(subject.strfdur("%#{formatter}")).to eq subject.send(:zero_pad, subject.send(period)) % subject.send(period)
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'with a padding modifier' do
|
14
|
+
it 'uses the provided padding' do
|
15
|
+
expect(subject.strfdur("%5#{formatter}")).to eq subject.send(:zero_pad, subject.send(period), 5) % subject.send(period)
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'with a padding negator' do
|
19
|
+
it 'strips all padding' do
|
20
|
+
expect(subject.strfdur("%-5#{formatter}")).to eq (subject.send(:zero_pad, subject.send(period), 5) % subject.send(period)).to_i.to_s.lstrip
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'with a padding negator' do
|
26
|
+
it 'strips all padding' do
|
27
|
+
expect(subject.strfdur("%-#{formatter}")).to eq (subject.send(:zero_pad, subject.send(period)) % subject.send(period)).to_i.to_s.lstrip
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'when using the space padding flag' do
|
32
|
+
it 'returns the formatted string' do
|
33
|
+
expect(subject.strfdur("%_#{formatter}")).to eq subject.send(:space_pad, subject.send(period)) % subject.send(period)
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'with a padding modifier' do
|
37
|
+
it 'uses the provided padding' do
|
38
|
+
expect(subject.strfdur("%_5#{formatter}")).to eq subject.send(:space_pad, subject.send(period), 5) % subject.send(period)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
RSpec.shared_examples "a complex format string" do |duration, formatter, string|
|
46
|
+
context "using complex formatter '#{formatter}'" do
|
47
|
+
subject { Duranged::Duration.new(duration) }
|
48
|
+
|
49
|
+
it "returns '#{string}'" do
|
50
|
+
expect(subject.strfdur(formatter)).to eq string
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
RSpec.shared_examples "the base class" do |klass|
|
56
|
+
subject { klass.new(86522.seconds) }
|
57
|
+
|
58
|
+
describe 'dump' do
|
59
|
+
it 'dumps the value as an integer' do
|
60
|
+
expect(klass.dump(subject)).to eq subject.to_json
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe 'load' do
|
65
|
+
it 'creates a new value from the integer' do
|
66
|
+
expect(klass.load(subject.to_json)).to be_an_instance_of klass
|
67
|
+
expect(klass.load(subject.to_json).to_json).to eq subject.to_json
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#initialize' do
|
72
|
+
context 'when passed an integer' do
|
73
|
+
it 'sets the value to the integer value' do
|
74
|
+
expect(subject.value).to eq 86522
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'when passed a hash' do
|
79
|
+
subject { klass.new({days: 1, hours: 0, minutes: 2, seconds: 2}) }
|
80
|
+
|
81
|
+
it 'parses the hash keys into time values and sums them' do
|
82
|
+
expect(subject.value).to eq 86522
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'when passed a parseable string' do
|
87
|
+
subject { klass.new("1 day, 2 minutes, 2 seconds") }
|
88
|
+
|
89
|
+
it 'parses the hash keys into time values and sums them' do
|
90
|
+
expect(subject.value).to eq 86522
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe '#days?' do
|
96
|
+
it 'returns the total number of days' do
|
97
|
+
expect(subject.days).to eq 1
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe '#hours?' do
|
102
|
+
it 'returns the remainder of hours' do
|
103
|
+
expect(subject.hours).to eq 0
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe '#minutes?' do
|
108
|
+
it 'returns the remainder of minutes' do
|
109
|
+
expect(subject.minutes).to eq 2
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe '#seconds?' do
|
114
|
+
it 'returns the remainder of seconds' do
|
115
|
+
expect(subject.seconds).to eq 2
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe '#+' do
|
120
|
+
context 'when passed an integer' do
|
121
|
+
it 'returns an instance of the same class' do
|
122
|
+
expect(subject + 20).to be_an_instance_of subject.class
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'adds the integer to the value' do
|
126
|
+
expect((subject + 20).value).to eq (subject.value + 20)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
context 'when passed a duration' do
|
131
|
+
it 'returns an instance of the same class' do
|
132
|
+
expect(subject + Duranged::Duration.new(20)).to be_an_instance_of subject.class
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'adds the duration to the value' do
|
136
|
+
expect((subject + Duranged::Duration.new(20)).value).to eq (subject.value + 20)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context 'when passed an interval' do
|
141
|
+
it 'returns an instance of the same class' do
|
142
|
+
expect(subject + Duranged::Interval.new(20)).to be_an_instance_of subject.class
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'adds the interval to the value' do
|
146
|
+
expect((subject + Duranged::Interval.new(20)).value).to eq (subject.value + 20)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
describe '#-' do
|
152
|
+
context 'when passed an integer' do
|
153
|
+
it 'returns an instance of the same class' do
|
154
|
+
expect(subject - 20).to be_an_instance_of subject.class
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'adds the integer to the value' do
|
158
|
+
expect((subject - 20).value).to eq (subject.value - 20)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
context 'when passed a duration' do
|
163
|
+
it 'returns an instance of the same class' do
|
164
|
+
expect(subject - Duranged::Duration.new(20)).to be_an_instance_of subject.class
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'adds the duration to the value' do
|
168
|
+
expect((subject - Duranged::Duration.new(20)).value).to eq (subject.value - 20)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
context 'when passed an interval' do
|
173
|
+
it 'returns an instance of the same class' do
|
174
|
+
expect(subject - Duranged::Interval.new(20)).to be_an_instance_of subject.class
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'adds the interval to the value' do
|
178
|
+
expect((subject - Duranged::Interval.new(20)).value).to eq (subject.value - 20)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
describe '#as_json' do
|
184
|
+
it 'returns the value integer' do
|
185
|
+
expect(subject.as_json).to eq subject.value
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
describe '#to_h' do
|
190
|
+
it_behaves_like "a hash method", :to_h
|
191
|
+
end
|
192
|
+
|
193
|
+
describe '#to_s' do
|
194
|
+
context 'when no format is specified' do
|
195
|
+
it 'returns a string using the default format' do
|
196
|
+
expect(subject.to_s).to eq '1 day, 2 minutes, 2 seconds'
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
describe '#strfdur' do
|
202
|
+
context 'when no format is provided' do
|
203
|
+
it 'raises an ArgumentError' do
|
204
|
+
expect { subject.strfdur }.to raise_error(ArgumentError)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
context 'when a format is specified' do
|
209
|
+
it_behaves_like "a format conversion", :seconds, 's'
|
210
|
+
it_behaves_like "a format conversion", :minutes, 'm'
|
211
|
+
it_behaves_like "a format conversion", :hours, 'h'
|
212
|
+
it_behaves_like "a format conversion", :days, 'd'
|
213
|
+
it_behaves_like "a format conversion", :days_after_weeks, 'D'
|
214
|
+
it_behaves_like "a format conversion", :weeks, 'w'
|
215
|
+
it_behaves_like "a format conversion", :months, 'M'
|
216
|
+
it_behaves_like "a format conversion", :years, 'y'
|
217
|
+
|
218
|
+
|
219
|
+
it_behaves_like "a complex format string", (3.days + 1.hour + 5.minutes + 30.seconds), '%d:%h:%m:%s', '03:01:05:30'
|
220
|
+
it_behaves_like "a complex format string", (5.hours + 5.minutes), '%_h hours and %m minutes', ' 5 hours and 05 minutes'
|
221
|
+
it_behaves_like "a complex format string", (3.days + 1.hour + 5.minutes + 30.seconds), '%-d days, %-h hour, %-m minutes, %-s seconds', '3 days, 1 hour, 5 minutes, 30 seconds'
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'duranged'
|
2
|
+
require 'rspec'
|
3
|
+
require 'coveralls'
|
4
|
+
Coveralls.wear!
|
5
|
+
|
6
|
+
Dir[File.join(File.dirname(__FILE__), '..', "spec/support/**/*.rb")].each { |f| require f }
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
#config.before(:suite) do
|
10
|
+
# ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
11
|
+
# capture_stdout { load "db/schema.rb" }
|
12
|
+
# load 'support/models.rb'
|
13
|
+
#end
|
14
|
+
|
15
|
+
# rspec-expectations config goes here. You can use an alternate
|
16
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
17
|
+
# assertions if you prefer.
|
18
|
+
config.expect_with :rspec do |expectations|
|
19
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
20
|
+
# and `failure_message` of custom matchers include text for helper methods
|
21
|
+
# defined using `chain`, e.g.:
|
22
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
23
|
+
# # => "be bigger than 2 and smaller than 4"
|
24
|
+
# ...rather than:
|
25
|
+
# # => "be bigger than 2"
|
26
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
27
|
+
end
|
28
|
+
|
29
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
30
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
31
|
+
config.mock_with :rspec do |mocks|
|
32
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
33
|
+
# a real object. This is generally recommended, and will default to
|
34
|
+
# `true` in RSpec 4.
|
35
|
+
mocks.verify_partial_doubles = true
|
36
|
+
end
|
37
|
+
|
38
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
39
|
+
# file, and it's useful to allow more verbose output when running an
|
40
|
+
# individual spec file.
|
41
|
+
if config.files_to_run.one?
|
42
|
+
# Use the documentation formatter for detailed output,
|
43
|
+
# unless a formatter has already been configured
|
44
|
+
# (e.g. via a command-line flag).
|
45
|
+
config.default_formatter = 'doc'
|
46
|
+
end
|
47
|
+
|
48
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
49
|
+
# For more details, see:
|
50
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
51
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
52
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
53
|
+
config.disable_monkey_patching!
|
54
|
+
|
55
|
+
# Run specs in random order to surface order dependencies. If you find an
|
56
|
+
# order dependency and want to debug it, you can fix the order by providing
|
57
|
+
# the seed, which is printed after each run.
|
58
|
+
# --seed 1234
|
59
|
+
config.order = :random
|
60
|
+
|
61
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
62
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
63
|
+
# test failures related to randomization by passing the same `--seed` value
|
64
|
+
# as the one that triggered the failure.
|
65
|
+
Kernel.srand config.seed
|
66
|
+
|
67
|
+
# Print the 10 slowest examples and example groups at the
|
68
|
+
# end of the spec run, to help surface which specs are running
|
69
|
+
# particularly slow.
|
70
|
+
#config.profile_examples = 10
|
71
|
+
|
72
|
+
# These two settings work together to allow you to limit a spec run
|
73
|
+
# to individual examples or groups you care about by tagging them with
|
74
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
75
|
+
# get run.
|
76
|
+
#config.filter_run :focus
|
77
|
+
#config.run_all_when_everything_filtered = true
|
78
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: duranged
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mark Rebec
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-11 00:00:00.000000000 Z
|
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: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: chronic_duration
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: canfig
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: API for working with durations and time ranges.
|
84
|
+
email:
|
85
|
+
- mark@markrebec.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- lib/duranged.rb
|
91
|
+
- lib/duranged/base.rb
|
92
|
+
- lib/duranged/duration.rb
|
93
|
+
- lib/duranged/interval.rb
|
94
|
+
- lib/duranged/occurrence.rb
|
95
|
+
- lib/duranged/range.rb
|
96
|
+
- lib/duranged/version.rb
|
97
|
+
- spec/duranged/base_spec.rb
|
98
|
+
- spec/duranged/duration_spec.rb
|
99
|
+
- spec/duranged/interval_spec.rb
|
100
|
+
- spec/duranged/occurrence_spec.rb
|
101
|
+
- spec/duranged/range_spec.rb
|
102
|
+
- spec/duranged_spec.rb
|
103
|
+
- spec/shared/base_examples.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
homepage: http://github.com/markrebec/duranged
|
106
|
+
licenses: []
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.2.2
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: API for working with durations and time ranges.
|
128
|
+
test_files:
|
129
|
+
- spec/duranged/base_spec.rb
|
130
|
+
- spec/duranged/duration_spec.rb
|
131
|
+
- spec/duranged/interval_spec.rb
|
132
|
+
- spec/duranged/occurrence_spec.rb
|
133
|
+
- spec/duranged/range_spec.rb
|
134
|
+
- spec/duranged_spec.rb
|
135
|
+
- spec/shared/base_examples.rb
|
136
|
+
- spec/spec_helper.rb
|