time_inputs 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/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/time_inputs.rb +6 -0
- data/lib/time_inputs/parser.rb +29 -0
- data/lib/time_inputs/version.rb +3 -0
- data/spec/lib/time_inputs/parser_spec.rb +103 -0
- data/spec/spec_helper.rb +14 -0
- data/time_inputs.gemspec +25 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d0f2c6db36f9d02424f7cf622dfcb3837f2c60a8
|
4
|
+
data.tar.gz: c612651cc3142112f5d5300e8c21ac4532b7cf5f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 20d499b04ab419434952b7beded4067ab03d97153050856fe835f8ce3f594efe2f55b85aa4382468782bf1ff1e6399e9ffcb803456ab313de31b1e2c6bfa2dec
|
7
|
+
data.tar.gz: 9d5633f4b9e01f38f26692fa5bdb2590670a0893d3262ca519215281c593390656f998e48209e18d4763452ef5c669bf5488edf6e0b77bb7ffec45c5a4180342
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Julien Gantner
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
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
|
+
|
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
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# TimeInputs
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'time_inputs'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install time_inputs
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/time_inputs.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module TimeInputs
|
2
|
+
class Parser
|
3
|
+
def initialize error_policy=->(message){ raise BaseError, message }
|
4
|
+
@error_policy = error_policy
|
5
|
+
end
|
6
|
+
|
7
|
+
def parse input_string, options={}
|
8
|
+
input_as_hh_mm = to_hh_mm(input_string.to_s)
|
9
|
+
date_context = check_date! options.fetch(:date_context) { Date.today }
|
10
|
+
hours, minutes = input_as_hh_mm.split(":")
|
11
|
+
Time.local(date_context.year, date_context.month, date_context.day, hours.to_i, minutes.to_i)
|
12
|
+
rescue BaseError => e
|
13
|
+
@error_policy.call(e.message)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
def to_hh_mm input
|
18
|
+
raise BaseError, "invalid input" unless input =~ /\A\d{1,2}:?\d{2}\z/
|
19
|
+
input.rjust(4,'0').tap do |str|
|
20
|
+
str.insert(2,':') unless str =~/\A\d?\d:\d{2}\z/
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def check_date! date
|
25
|
+
raise BaseError, "invalid date_context" unless date.respond_to?(:to_date)
|
26
|
+
date.to_date
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TimeInputs::Parser do
|
4
|
+
let(:today) { Date.today }
|
5
|
+
|
6
|
+
describe '.new' do
|
7
|
+
it 'takes an proc as error handling policy' do
|
8
|
+
expect { TimeInputs::Parser.new ->(error){}}.to_not raise_error
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'uses an error raising strategy by default' do
|
12
|
+
expect { subject }.to_not raise_error
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#parse' do
|
17
|
+
it 'takes one string argument and an :date_context' do
|
18
|
+
expect { subject.parse "08:00", date_context: Date.new(2014,1,31) }
|
19
|
+
.to_not raise_error
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'does not require a :date_context' do
|
23
|
+
expect { subject.parse "08:00" }.to_not raise_error
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'given valid inputs' do
|
27
|
+
context 'given a time in the hh:mm format' do
|
28
|
+
it 'converts "08:00" to a local time object on the current day' do
|
29
|
+
expect(subject.parse "08:00")
|
30
|
+
.to eq(Time.local(today.year, today.month, today.day,8,0))
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'converts "14:15" to a local time object on the current day' do
|
34
|
+
expect(subject.parse "14:15")
|
35
|
+
.to eq(Time.local(today.year, today.month, today.day,14,15))
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'converts "13:07", date_context: Date.new(2014,1,31) to a local time object on the given day' do
|
39
|
+
expect(subject.parse "13:07", date_context: Date.new(2014,1,31))
|
40
|
+
.to eq(Time.local(2014,1,31,13,7))
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'converts "3:12", date_context: Date.new(2014,1,31) to a local time object on the given day' do
|
44
|
+
expect(subject.parse "3:12", date_context: Date.new(2014,1,31))
|
45
|
+
.to eq(Time.local(2014,1,31,3,12))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'given a time in military format (e.g. 0200)' do
|
50
|
+
it 'converts "0800" to a local time object on the current day' do
|
51
|
+
expect(subject.parse "0800")
|
52
|
+
.to eq(Time.local(today.year, today.month, today.day, 8, 0))
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'converts "1415" to a local time object on the current day' do
|
56
|
+
expect(subject.parse "1415")
|
57
|
+
.to eq(Time.local(today.year, today.month, today.day, 14, 15))
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'converts "1307", date_context: Date.new(2014,1,31) to a local time object on the given day' do
|
61
|
+
expect(subject.parse "1307", date_context: Date.new(2014,1,31))
|
62
|
+
.to eq(Time.local(2014, 1, 31, 13, 7))
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'converts "0312", date_context: Date.new(2014,1,30) to a local time object on the given day' do
|
66
|
+
expect(subject.parse "0312", date_context: Date.new(2014,1,30))
|
67
|
+
.to eq(Time.local(2014, 1, 30, 3, 12))
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'given a time in the military format without leading zeros' do
|
72
|
+
it 'converts "215" to a local time object on the current day' do
|
73
|
+
expect(subject.parse "215")
|
74
|
+
.to eq(Time.local(today.year, today.month, today.day, 2, 15))
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'converts "343", date_context: Date.new(2014,1,31) to a local time object on the given day'do
|
78
|
+
expect(subject.parse "343", date_context: Date.new(2014,1,31))
|
79
|
+
.to eq(Time.local(2014,1,31,3,43))
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'given invalid inputs' do
|
85
|
+
let(:error_policy) { double('error_handler') }
|
86
|
+
subject { TimeInputs::Parser.new error_policy }
|
87
|
+
|
88
|
+
context 'given invalid parse inputs' do
|
89
|
+
it 'calls the error handler with "invalid input"' do
|
90
|
+
expect(error_policy).to receive(:call).with("invalid input")
|
91
|
+
subject.parse "invalid input"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context 'given an invalid date_context: object' do
|
96
|
+
it 'calls the error handler with "invalid date_context"' do
|
97
|
+
expect(error_policy).to receive(:call).with("invalid date_context")
|
98
|
+
subject.parse "1605", date_context: Object.new
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'time_inputs'
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
6
|
+
config.run_all_when_everything_filtered = true
|
7
|
+
config.filter_run :focus
|
8
|
+
|
9
|
+
# Run specs in random order to surface order dependencies. If you find an
|
10
|
+
# order dependency and want to debug it, you can fix the order by providing
|
11
|
+
# the seed, which is printed after each run.
|
12
|
+
# --seed 1234
|
13
|
+
config.order = 'random'
|
14
|
+
end
|
data/time_inputs.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'time_inputs/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "time_inputs"
|
8
|
+
spec.version = TimeInputs::VERSION
|
9
|
+
spec.authors = ["Julien Gantner"]
|
10
|
+
spec.email = ["julien.gantner@softwareinmotion.de"]
|
11
|
+
spec.description = spec.summary
|
12
|
+
spec.summary = %q{Helper to parse strings to time objects in various formats}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "guard-rspec", "~> 4.2.5"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time_inputs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julien Gantner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
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: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
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: guard-rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 4.2.5
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 4.2.5
|
69
|
+
description: ''
|
70
|
+
email:
|
71
|
+
- julien.gantner@softwareinmotion.de
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- Gemfile
|
79
|
+
- Guardfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/time_inputs.rb
|
84
|
+
- lib/time_inputs/parser.rb
|
85
|
+
- lib/time_inputs/version.rb
|
86
|
+
- spec/lib/time_inputs/parser_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
- time_inputs.gemspec
|
89
|
+
homepage: ''
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.0.3
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Helper to parse strings to time objects in various formats
|
113
|
+
test_files:
|
114
|
+
- spec/lib/time_inputs/parser_spec.rb
|
115
|
+
- spec/spec_helper.rb
|