dayrange 1.0.0
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/.rubocop.yml +4 -0
- data/.standard.yml +2 -0
- data/CHANGELOG.md +14 -0
- data/Gemfile +11 -0
- data/README.md +56 -0
- data/Rakefile +14 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/day_range.gemspec +38 -0
- data/lib/day_range/version.rb +5 -0
- data/lib/day_range.rb +71 -0
- data/lib/dayrange.rb +4 -0
- data/sig/day_range.rbs +4 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0c477109871aa6b6d156b3a345778e0de371fe1daa43205fe862b3ba47cc6288
|
4
|
+
data.tar.gz: 1dbaa87fb96aab8b9e1c0a7c9132e73e2aef08d4abb63fd7b5db3b7b86e691cd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3af2ac0f6ebee15d5c776fb7436bdcb74534de02a3ace7e4b2707f190d26990de53c35d09a9e5b5df4de8166ff1afbc57879403297c7a28b0a8956efa025153c
|
7
|
+
data.tar.gz: b9d136d735e960993755cb71f1ebeeb330f131cde2985834c4d3b793b5d630222de30a34ad6cdb9b2e4c6a15b50794e47c1d6149afd732da73061806da0e4d87
|
data/.rubocop.yml
ADDED
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [Unreleased]
|
9
|
+
|
10
|
+
## [1.0.0] - 2022-10-07
|
11
|
+
|
12
|
+
### Added
|
13
|
+
|
14
|
+
- Everything :)
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# DayRange
|
2
|
+
|
3
|
+
`DayRange` provides methods to navigate and manipulate series of dates.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'dayrange'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install dayrange
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
year = DayRange.new(Date.new(2022, 1, 1), Date.new(2022, 12, 31))
|
25
|
+
|
26
|
+
# Navigate to the period before
|
27
|
+
last_year = year.previous
|
28
|
+
|
29
|
+
# Navigate to the period after
|
30
|
+
next_year = year.next
|
31
|
+
|
32
|
+
# Iterate through the year
|
33
|
+
year.each do |date|
|
34
|
+
# Every day of the year
|
35
|
+
end
|
36
|
+
|
37
|
+
# Iterate in more interesting ways
|
38
|
+
year.every(weeks: 2) do |date|
|
39
|
+
# Every other week of the year
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
## Related projects
|
44
|
+
|
45
|
+
* https://github.com/moneybird/active-date-range
|
46
|
+
* https://rubygems.org/gems/cada
|
47
|
+
|
48
|
+
## Development
|
49
|
+
|
50
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
51
|
+
|
52
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/substancelab/dayrange.
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "standard/rake"
|
13
|
+
|
14
|
+
task default: %i[test standard]
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "day_range"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/day_range.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/day_range/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "dayrange"
|
7
|
+
spec.version = DayRange::VERSION
|
8
|
+
spec.authors = ["Jakob Skjerning"]
|
9
|
+
spec.email = ["jakob@mentalized.net"]
|
10
|
+
|
11
|
+
spec.summary = "Range-like object that knows about dates"
|
12
|
+
spec.description = "DayRange provides methods to navigate and manipulate series of dates."
|
13
|
+
spec.homepage = "https://github.com/substancelab/day_range"
|
14
|
+
spec.required_ruby_version = ">= 2.7.0"
|
15
|
+
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/substancelab/day_range/blob/main/CHANGELOG.md"
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
25
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
# Uncomment to register a new dependency of your gem
|
34
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
35
|
+
|
36
|
+
# For more information and examples about making a new gem, check out our
|
37
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
38
|
+
end
|
data/lib/day_range.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "day_range/version"
|
4
|
+
|
5
|
+
# A Range-like object that represents a range of days.
|
6
|
+
class DayRange < Range
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
# Returns the number of days in the timeframe.
|
10
|
+
def days
|
11
|
+
last - first + 1
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns an enumerator that steps over the days in the range with specific
|
15
|
+
# intervals.
|
16
|
+
#
|
17
|
+
# step can be specified similarily to DateTime#advance argument
|
18
|
+
#
|
19
|
+
# Usage examples
|
20
|
+
#
|
21
|
+
# day_range.every(days: 42)
|
22
|
+
# day_range.every(weeks: 1)
|
23
|
+
# day_range.every(months: 1)
|
24
|
+
# day_range.every(days: 1, months: 2, years: 3)
|
25
|
+
#
|
26
|
+
def every(step)
|
27
|
+
c_date = first
|
28
|
+
finish_date = last
|
29
|
+
comparison_operator = exclude_end? ? :< : :<=
|
30
|
+
|
31
|
+
result = []
|
32
|
+
while c_date.send(comparison_operator, finish_date)
|
33
|
+
result << c_date
|
34
|
+
yield c_date if block_given?
|
35
|
+
c_date = advance(c_date, step)
|
36
|
+
end
|
37
|
+
result
|
38
|
+
end
|
39
|
+
|
40
|
+
# Returns a DayRange with the same length as this one, starting on the day
|
41
|
+
# after this one ends.
|
42
|
+
def next
|
43
|
+
self.class.new(
|
44
|
+
last.next_day,
|
45
|
+
last + days
|
46
|
+
)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Returns a DayRange with the same length as this one, ending on the day
|
50
|
+
# before this one starts.
|
51
|
+
def previous
|
52
|
+
self.class.new(
|
53
|
+
first - days,
|
54
|
+
first.prev_day
|
55
|
+
)
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 112
|
61
|
+
def advance(date, options)
|
62
|
+
d = date
|
63
|
+
|
64
|
+
d = d >> options[:years] * 12 if options[:years]
|
65
|
+
d = d >> options[:months] if options[:months]
|
66
|
+
d += options[:weeks] * 7 if options[:weeks]
|
67
|
+
d += options[:days] if options[:days]
|
68
|
+
|
69
|
+
d
|
70
|
+
end
|
71
|
+
end
|
data/lib/dayrange.rb
ADDED
data/sig/day_range.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dayrange
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jakob Skjerning
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-10-07 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: DayRange provides methods to navigate and manipulate series of dates.
|
14
|
+
email:
|
15
|
+
- jakob@mentalized.net
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rubocop.yml"
|
21
|
+
- ".standard.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- Gemfile
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- bin/console
|
27
|
+
- bin/setup
|
28
|
+
- day_range.gemspec
|
29
|
+
- lib/day_range.rb
|
30
|
+
- lib/day_range/version.rb
|
31
|
+
- lib/dayrange.rb
|
32
|
+
- sig/day_range.rbs
|
33
|
+
homepage: https://github.com/substancelab/day_range
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
metadata:
|
37
|
+
changelog_uri: https://github.com/substancelab/day_range/blob/main/CHANGELOG.md
|
38
|
+
homepage_uri: https://github.com/substancelab/day_range
|
39
|
+
source_code_uri: https://github.com/substancelab/day_range
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 2.7.0
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubygems_version: 3.3.7
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Range-like object that knows about dates
|
59
|
+
test_files: []
|