ruby-time_range 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 45e5f5852c51c527ddf7bb104cde7a94db2b30f6
4
+ data.tar.gz: b51a31525db80a2daf5bb256d9c5c7ae9ed4b9b4
5
+ SHA512:
6
+ metadata.gz: 39f152da5800d47cc91c6db98ef3156a0447ac2e8a90878c672478420d649beb2d16b7606bbf6fc6c6027f641a87016a9e9e0971cc73231fdbde762556b51be1
7
+ data.tar.gz: ae499f4e9b52467e83466b3edae7fd173c4f720962858eb35f77dc8cc5895b093c2f4975cdc052a2a80548561d9586aabd8f7580361a4d335670b89e8d0e0e5b
@@ -0,0 +1,2 @@
1
+ notes.txt
2
+ .yardoc
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,2 @@
1
+ Style/Lambda:
2
+ Enabled: false
@@ -0,0 +1 @@
1
+ ruby-timerange
@@ -0,0 +1 @@
1
+ ruby-2.3.1
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at john.hope@storyful.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ gemspec
4
+
5
+ source 'https://rubygems.org'
6
+
7
+ group :development do
8
+ gem 'rspec'
9
+ gem 'rubocop'
10
+ end
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby-time_range (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.3.0)
10
+ diff-lcs (1.2.5)
11
+ parser (2.3.1.2)
12
+ ast (~> 2.2)
13
+ powerpack (0.1.1)
14
+ rainbow (2.1.0)
15
+ rake (10.4.2)
16
+ rspec (3.5.0)
17
+ rspec-core (~> 3.5.0)
18
+ rspec-expectations (~> 3.5.0)
19
+ rspec-mocks (~> 3.5.0)
20
+ rspec-core (3.5.2)
21
+ rspec-support (~> 3.5.0)
22
+ rspec-expectations (3.5.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.5.0)
25
+ rspec-mocks (3.5.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.5.0)
28
+ rspec-support (3.5.0)
29
+ rubocop (0.42.0)
30
+ parser (>= 2.3.1.1, < 3.0)
31
+ powerpack (~> 0.1)
32
+ rainbow (>= 1.99.1, < 3.0)
33
+ ruby-progressbar (~> 1.7)
34
+ unicode-display_width (~> 1.0, >= 1.0.1)
35
+ ruby-progressbar (1.8.1)
36
+ unicode-display_width (1.1.0)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ bundler (~> 1.11)
43
+ rake (~> 10.0)
44
+ rspec
45
+ rubocop
46
+ ruby-time_range!
47
+
48
+ BUNDLED WITH
49
+ 1.12.5
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Jon Hope
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ #!/usr/bin/env ruby
3
+
4
+ require 'bundler/setup'
5
+ require 'ruby/time_range'
6
+
7
+ require 'irb'
8
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'time_range/core_ext/range.rb'
4
+ require 'date'
5
+
6
+ # An enhanced, Range-type class that provides useful methods that apply to
7
+ # time-based objects.
8
+ #
9
+ # @since 0.0.1
10
+ class TimeRange < Range
11
+ def initialize(b, e, exclude_end = false)
12
+ raise TypeError unless [b, e].all? { |arg| arg.is_a?(Time) }
13
+
14
+ valid_time = if exclude_end
15
+ (e - 1) >= b
16
+ else
17
+ e >= b
18
+ end
19
+
20
+ raise ArgumentError unless valid_time
21
+
22
+ super
23
+ end
24
+
25
+ # Reimplementation of Range#== to account for time ranges excluding end being
26
+ # one second shorter than their end implies.
27
+ #
28
+ # @param [TimeRange] time_range time range to compare for equality
29
+ # @return [Boolean] whether contents of two TimeRanges match
30
+ def ==(other)
31
+ return false unless other.is_a?(self.class)
32
+
33
+ if exclude_end? || other.exclude_end?
34
+ return self.begin == other.begin && max == other.max
35
+ end
36
+
37
+ super
38
+ end
39
+
40
+ alias eql? ==
41
+
42
+ # Reimplementation of Range#max. TimeRange makes one assumption; that ranges
43
+ # excluding end should be one second shorter than those that don't.
44
+ #
45
+ # Normal Ranges simply TypeError when excluding_end is true. However, since
46
+ # Ruby's Time methods assume seconds anyway, it is reasonable to use seconds
47
+ # here.
48
+ #
49
+ # Doing so enables the implementation of a number of useful TimeRange methods.
50
+ #
51
+ # @return [Time] the highest time included in the range
52
+ def max
53
+ return self.end - 1 if exclude_end?
54
+
55
+ super
56
+ end
57
+
58
+ # The duration of the time range in seconds.
59
+ #
60
+ # @return [Float] the number of seconds in the time range
61
+ def duration
62
+ max - self.begin
63
+ end
64
+
65
+ # Compares another time range for any kind of overlap; partial or complete.
66
+ #
67
+ # @param [TimeRange] time_range time range to compare for overlap
68
+ # @return [Boolean] whether contents of two TimeRanges overlap
69
+ def overlaps?(time_range)
70
+ raise TypeError unless time_range.is_a?(TimeRange)
71
+
72
+ %w(begins_within? ends_within? encapsulates?).any? do |condition|
73
+ send(condition, time_range)
74
+ end
75
+ end
76
+
77
+ # Compares another time range for complete encapsulation. A time range is
78
+ # considered encapsulated if it begins and ends within this one, including if
79
+ # the ranges match exactly.
80
+ #
81
+ # @param [TimeRange] time_range time range to compare for encapsulation
82
+ # @return [Boolean] whether the target range is encapsulated
83
+ def encapsulates?(time_range)
84
+ raise TypeError unless time_range.is_a?(TimeRange)
85
+
86
+ self.begin <= time_range.begin && max >= time_range.max
87
+ end
88
+
89
+ # Reverse of #encapsulates?, checks if the current time range is encapsulated
90
+ # by another.
91
+ #
92
+ # @param [TimeRange] time_range time range to compare against encapsulation
93
+ # @return [Boolean] whether the target range encapsulates this one
94
+ def encapsulated_by?(time_range)
95
+ raise TypeError unless time_range.is_a?(TimeRange)
96
+
97
+ time_range.encapsulates?(self)
98
+ end
99
+
100
+ # Returns the overlap between this and the argument as a new time range.
101
+ #
102
+ # @param [TimeRange] time_range time range to compare for encapsulation
103
+ # @return [TimeRange, nil] the overlap in a TimeRange or nil if none occurs
104
+ def overlap_with(time_range)
105
+ raise TypeError unless time_range.is_a?(TimeRange)
106
+
107
+ self.class.new(
108
+ [self.begin, time_range.begin].max,
109
+ [max, time_range.max].min
110
+ ) if overlaps?(time_range)
111
+ end
112
+
113
+ # Returns the dates in the current TimeRange as a Range of Dates.
114
+ #
115
+ # @return [Range] the dates in the current time range
116
+ def dates
117
+ b_date = Date.civil(self.begin.year, self.begin.month, self.begin.day)
118
+ e_date = Date.civil(max.year, max.month, max.day)
119
+
120
+ b_date..e_date
121
+ end
122
+
123
+ private
124
+
125
+ def begins_within?(time_range)
126
+ self.begin >= time_range.begin && self.begin <= time_range.max
127
+ end
128
+
129
+ def ends_within?(time_range)
130
+ max <= time_range.max && max >= time_range.begin
131
+ end
132
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Reopens the Range class from the core lib to include methods to converting to
4
+ # TimeRange.
5
+ #
6
+ # @since 0.0.1
7
+ class Range
8
+ # Converts a Range of Times into a TimeRange.
9
+ #
10
+ # @return [TimeRange]
11
+ def to_time_range
12
+ TimeRange.new(self.begin, self.end, exclude_end?)
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class TimeRange < Range
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,59 @@
1
+ ## TimeRange
2
+
3
+ Enhance your Ruby project with time-based ranges.
4
+
5
+ ## Installation
6
+
7
+ Install the `time_range` gem and require it in your project.
8
+
9
+ ```shell
10
+ gem install ruby-time_range
11
+ ```
12
+
13
+ or include the gem in your Gemfile and `bundle install`.
14
+
15
+ ```ruby
16
+ gem 'ruby-time_range'
17
+ ```
18
+
19
+ ## About
20
+
21
+ `TimeRange` extends the regular `Range` with useful methods that apply to time-based objects. Where appropriate, it reimplements `Range` methods as they should apply to Time-based ranges.
22
+
23
+ `TimeRange`s can be compared and examined in useful ways.
24
+
25
+ - `TimeRange#duration` The duration of the TimeRange in seconds.
26
+ - `TimeRange#encapsulates?` Check if one TimeRange encapsulates another entirely.
27
+ - `TimeRange#encapsulated_by?` Check if one TimeRange is encapsulated by another entirely...
28
+ - `TimeRange#overlaps?` Check if one TimeRange overlaps another in any way.
29
+ - `TimeRange#overlap_with` Returns the overlap between two TimeRanges, as a new TimeRange.
30
+ - `TimeRange#dates` Returns a range of dates in the TimeRange.
31
+
32
+ Existing Range methods; such as `#max`, `#min`, `#cover?`, `#==`, etc, are still available, having been reimplemented where appropriate to maintain consistency in a temporal context.
33
+
34
+ TimeRanges can be easily created by calling `#to_time_range` on any normal Range of Times.
35
+
36
+ ```ruby
37
+ (Time.now .. (Time.now + 60)).to_time_range
38
+ ```
39
+
40
+ For full API documentation see the YARD docs.
41
+
42
+ ## Contributing
43
+ - Fork and clone the project.
44
+ - `git checkout master`.
45
+ - `git checkout -b ` a topic branch for your fix/addition.
46
+ - Run `bundle`.
47
+ - Make and commit your changes.
48
+ - Push to your fork and pull request against master.
49
+
50
+ #### Note:
51
+ - Contributions will not be accepted without tests.
52
+ - Please read and check Github issues and pending pull requests before submitting new code.
53
+ - If adding a feature please post a new issue for discussion first.
54
+
55
+ Thanks for taking the time to contribute!
56
+
57
+ ## License
58
+
59
+ ruby-time_range is Copyright ©2016 Jon Hope. It is free software, and may be redistributed under the terms specified in the [LICENSE](https://github.com/jonmidhir/ruby-time_range/blob/master/LICENSE) file.
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+ # coding: utf-8
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'time_range/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'ruby-time_range'
9
+ spec.version = TimeRange::VERSION
10
+ spec.authors = ['Jon Hope']
11
+ spec.email = ['me@jhope.ie']
12
+
13
+ spec.summary = 'Enhance your Ruby project with time-based ranges.'
14
+ spec.description = 'Enhance your Ruby project with time-based ranges.'
15
+ spec.homepage = 'https://github.com/jonmidhir/ruby-time_range'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.11'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-time_range
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jon Hope
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Enhance your Ruby project with time-based ranges.
42
+ email:
43
+ - me@jhope.ie
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".rubocop.yml"
51
+ - ".ruby-gemset"
52
+ - ".ruby-version"
53
+ - ".travis.yml"
54
+ - CODE_OF_CONDUCT.md
55
+ - Gemfile
56
+ - Gemfile.lock
57
+ - LICENSE
58
+ - Rakefile
59
+ - bin/console
60
+ - bin/setup
61
+ - lib/time_range.rb
62
+ - lib/time_range/core_ext/range.rb
63
+ - lib/time_range/version.rb
64
+ - readme.md
65
+ - ruby-time_range.gemspec
66
+ homepage: https://github.com/jonmidhir/ruby-time_range
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.5.1
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Enhance your Ruby project with time-based ranges.
90
+ test_files: []