duration-range 0.2.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 31ca768c2bf4dc12f6de5f78ff2efcd8a08962c3b0c106583979c138ba688f7b
4
+ data.tar.gz: 34a075181c3f19e13bc1148481d60540841c0df5212f47fc2da94f99b01a1cc9
5
+ SHA512:
6
+ metadata.gz: 06daafdabccf9ff4473041e1b072a48dd12fef80b22d3639625da2cd7270ed693c12308eec6cbb085e7336d8eaa0ec0b9a739775f277584b711b92ae347ba1e6
7
+ data.tar.gz: c2998d5d3496143fb23655f0a552a8b6d1e97f547f51a294056b5513268c80e8f732c0e298a97a29e5a52c952f2673c105445d59b977ba5bfd372e80d81b38fb
data/.editorconfig ADDED
@@ -0,0 +1,3 @@
1
+ [*]
2
+ indent_size = 2
3
+ indent_style = space
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ *~
2
+ *.bak
3
+ .DS_Store
4
+ Gemfile.lock
5
+ /.bundle/
6
+ /.yardoc
7
+ /_yardoc/
8
+ /coverage/
9
+ /doc/
10
+ /pkg/
11
+ /spec/reports/
12
+ /tmp/
13
+ /vendor/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.5
7
+ before_install: gem install bundler -v 2.0.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in duration-range.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ BSD 2-Clause License
2
+
3
+ Copyright (c) 2021, wtnabe
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # DurationRange
2
+
3
+ duration range data generator
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'duration-range'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install duration-range
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ require 'duration-range'
25
+
26
+ DurationRange::Date.new.this_week.map(&:to_s)
27
+ # => ["2021-04-26", "2021-04-27", "2021-04-28", "2021-04-29", "2021-04-30", "2021-05-01", "2021-05-02"]
28
+
29
+ DurationRange::Date.new.last_month(as: :hash)
30
+ # => {:begin=>Thu, 01 Apr 2021, :end=>Fri, 30 Apr 2021}
31
+
32
+ DurationRange::Time.new.last_month
33
+ # => {:begin=>2021-04-01 00:00:00 UTC, :end=>2021-05-01 00:00:00 UTC}
34
+ ```
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
+
40
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wtnabe/duration-range.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:spec) do |t|
5
+ t.libs << "spec"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["spec/**/*_spec.rb"]
8
+ end
9
+
10
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "duration_range"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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,37 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "duration_range/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "duration-range"
7
+ spec.version = DurationRange::VERSION
8
+ spec.authors = ["wtnabe"]
9
+ spec.email = ["wtnabe@gmail.com"]
10
+
11
+ spec.summary = %q{generate Date or Time range from human daily time interval.}
12
+ spec.homepage = "https://github.com/wtnabe/duration-range."
13
+
14
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "#{spec.metadata['source_code_uri']}/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_runtime_dependency "activesupport", "~> 5"
30
+
31
+ spec.add_development_dependency "bundler", "~> 2.0"
32
+ spec.add_development_dependency "rake", "~> 13.0"
33
+ spec.add_development_dependency "minitest", "~> 5.0"
34
+ spec.add_development_dependency "minitest-reporters", "~> 1"
35
+ spec.add_development_dependency "minitest-power_assert", "~> 0.3"
36
+ spec.add_development_dependency "yard", "~> 0.9"
37
+ end
@@ -0,0 +1 @@
1
+ require_relative './duration_range'
@@ -0,0 +1,7 @@
1
+ Dir.glob(__dir__ + "/duration_range/**/*.rb").each { |f|
2
+ require f.sub(/\.rb/, '')
3
+ }
4
+
5
+ module DurationRange
6
+ class Error < StandardError; end
7
+ end
@@ -0,0 +1,94 @@
1
+ module DurationRange
2
+ module CommonFunctions
3
+ #
4
+ # @param today [Date]
5
+ #
6
+ def initialize(today: ::Date.today)
7
+ @today = today
8
+ end
9
+ attr_reader :today
10
+
11
+ #
12
+ # @return [Array]
13
+ #
14
+ def modes
15
+ [:array, :hash]
16
+ end
17
+
18
+ #
19
+ # @param as [Symbol]
20
+ # @param begin_date [Date]
21
+ # @param end_date [Date]
22
+ # @return [Object]
23
+ #
24
+ # :reek:UtilityFunction :reek:ControlParameter
25
+ def tidy_with_date(as:, begin_date:, end_date:)
26
+ case as
27
+ when :array
28
+ (begin_date..end_date).to_a
29
+ when :hash
30
+ { begin: begin_date, end: end_date }
31
+ end
32
+ end
33
+
34
+ #
35
+ # @param as [Symbol]
36
+ # @param reference_date [Date]
37
+ # @return [Object]
38
+ #
39
+ def this_month(as:, reference_date: today)
40
+ begin_date = reference_date.at_beginning_of_month
41
+ end_date = reference_date.at_end_of_month
42
+
43
+ tidy_with_date(as: as, begin_date: begin_date, end_date: end_date)
44
+ end
45
+
46
+ #
47
+ # @param as [Symbol]
48
+ # @param count [Integer]
49
+ # @return [Object]
50
+ #
51
+ def next_month(as:, count: 1)
52
+ this_month(as: as, reference_date: today.months_since(count))
53
+ end
54
+
55
+ #
56
+ # @param as [Symbol]
57
+ # @param count [Integer]
58
+ # @return [Object]
59
+ #
60
+ def last_month(as:, count: 1)
61
+ this_month(as: as, reference_date: today.months_ago(count))
62
+ end
63
+
64
+ #
65
+ # @param as [Symbol]
66
+ # @param reference_date [Date]
67
+ # @return [Object]
68
+ #
69
+ def this_week(as:, reference_date: today)
70
+ begin_date = reference_date.beginning_of_week
71
+ end_date = reference_date.end_of_week
72
+
73
+ tidy_with_date(as: as, begin_date: begin_date, end_date: end_date)
74
+ end
75
+
76
+ #
77
+ # @param as [Symbol]
78
+ # @param count [Integer]
79
+ # @return [Object]
80
+ #
81
+ def next_week(as:, count: 1)
82
+ this_week(as: as, reference_date: today.weeks_since(count))
83
+ end
84
+
85
+ #
86
+ # @param as [Symbol]
87
+ # @param count [Integer]
88
+ # @return [Object]
89
+ #
90
+ def last_week(as:, count: 1)
91
+ this_week(as: as, reference_date: today.weeks_ago(count))
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,63 @@
1
+ require "active_support/core_ext/date"
2
+ require "active_support/core_ext/time"
3
+ require_relative "./common_functions"
4
+
5
+ module DurationRange
6
+ class Date
7
+ include DurationRange::CommonFunctions
8
+
9
+ #
10
+ # @param as [Symbol]
11
+ # @param reference_date [Date]
12
+ # @return [Object]
13
+ #
14
+ def this_month(as: :array, reference_date: today)
15
+ super(as: as, reference_date: reference_date)
16
+ end
17
+
18
+ #
19
+ # @param as [Symbol]
20
+ # @param count [Integer]
21
+ # @return [Object]
22
+ #
23
+ def next_month(as: :array, count: 1)
24
+ super(as: as, count: count)
25
+ end
26
+
27
+ #
28
+ # @param as [Symbol]
29
+ # @param count [Integer]
30
+ # @return [Object]
31
+ #
32
+ def last_month(as: :array, count: 1)
33
+ super(as: as, count: count)
34
+ end
35
+
36
+ #
37
+ # @param as [Symbol]
38
+ # @param reference_date [Date]
39
+ # @return [Object]
40
+ #
41
+ def this_week(as: :array, reference_date: today)
42
+ super(as: as, reference_date: reference_date)
43
+ end
44
+
45
+ #
46
+ # @param as [Symbol]
47
+ # @param count [Integer]
48
+ # @return [Object]
49
+ #
50
+ def next_week(as: :array, count: 1)
51
+ super(as: as, count: count)
52
+ end
53
+
54
+ #
55
+ # @param as [Symbol]
56
+ # @param count [Integer]
57
+ # @return [Object]
58
+ #
59
+ def last_week(as: :array, count: 1)
60
+ super(as: as, count: count)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,106 @@
1
+ require "active_support/core_ext/date"
2
+ require "active_support/core_ext/time"
3
+ require_relative "./common_functions"
4
+
5
+ module DurationRange
6
+ class Time
7
+ include DurationRange::CommonFunctions
8
+
9
+ #
10
+ # @param today [Date]
11
+ # @param localtime [boolish]
12
+ #
13
+ # :reek:BooleanParameter
14
+ def initialize(today: ::Date.today, with_localtime: false)
15
+ @today = today
16
+ @with_localtime = with_localtime
17
+ end
18
+ attr_reader :today, :with_localtime
19
+
20
+ #
21
+ # @param as [Symbol]
22
+ # @param begin_date [Date]
23
+ # @param end_date [Date]
24
+ # @return [Object}
25
+ #
26
+ # :reek:TooManyStatements :reek:ControlParameter
27
+ def tidy_with_time(as:, begin_date:, end_date:)
28
+ case as
29
+ when :array
30
+ tidy_with_date(as: :array, begin_date: begin_date, end_date: end_date).map { |date| to_time(date) }
31
+ when :hash
32
+ {
33
+ begin: to_time(begin_date),
34
+ end: to_time(end_date)
35
+ }
36
+ end
37
+ end
38
+
39
+ #
40
+ # @param date [Date]
41
+ # @return [Time]
42
+ #
43
+ # :reek:FeatureEnvy
44
+ def to_time(date)
45
+ with_localtime ? date.to_time : date.to_time(:utc)
46
+ end
47
+
48
+ #
49
+ # @param as [Symbol]
50
+ # @param reference_date [Date]
51
+ # @return [Object]
52
+ #
53
+ def this_month(as: :hash, reference_date: today)
54
+ dates = super(as: :hash, reference_date: reference_date)
55
+
56
+ tidy_with_time(as: as, begin_date: dates[:begin], end_date: dates[:end] + 1)
57
+ end
58
+
59
+ #
60
+ # @param as [Symbol]
61
+ # @param count [Integer]
62
+ # @return [Object]
63
+ #
64
+ def next_month(as: :hash, count: 1)
65
+ this_month(as: as, reference_date: today.months_since(count))
66
+ end
67
+
68
+ #
69
+ # @param as [Symbol]
70
+ # @param count [Integer]
71
+ # @return [Object]
72
+ #
73
+ def last_month(as: :hash, count: 1)
74
+ this_month(as: as, reference_date: today.months_ago(count))
75
+ end
76
+
77
+ #
78
+ # @param as [Symbol]
79
+ # @param reference_date [Date]
80
+ # @return [Object]
81
+ #
82
+ def this_week(as: :hash, reference_date: today)
83
+ dates = super(as: :hash, reference_date: reference_date)
84
+
85
+ tidy_with_time(as: as, begin_date: dates[:begin], end_date: dates[:end] + 1)
86
+ end
87
+
88
+ #
89
+ # @param as [Symbol]
90
+ # @param count [Integer]
91
+ # @return [Object]
92
+ #
93
+ def next_week(as: :hash, count: 1)
94
+ this_week(as: as, reference_date: today.weeks_since(count))
95
+ end
96
+
97
+ #
98
+ # @param as [Symbol]
99
+ # @param count [Integer]
100
+ # @return [Object]
101
+ #
102
+ def last_week(as: :hash, count: 1)
103
+ this_week(as: as, reference_date: today.weeks_ago(count))
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,3 @@
1
+ module DurationRange
2
+ VERSION = "0.2.0"
3
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: duration-range
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - wtnabe
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-01 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: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-reporters
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-power_assert
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.9'
111
+ description:
112
+ email:
113
+ - wtnabe@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".editorconfig"
119
+ - ".gitignore"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - duration-range.gemspec
128
+ - lib/duration-range.rb
129
+ - lib/duration_range.rb
130
+ - lib/duration_range/common_functions.rb
131
+ - lib/duration_range/date.rb
132
+ - lib/duration_range/time.rb
133
+ - lib/duration_range/version.rb
134
+ homepage: https://github.com/wtnabe/duration-range.
135
+ licenses: []
136
+ metadata:
137
+ allowed_push_host: https://rubygems.org
138
+ homepage_uri: https://github.com/wtnabe/duration-range.
139
+ source_code_uri: https://github.com/wtnabe/duration-range.
140
+ changelog_uri: https://github.com/wtnabe/duration-range./CHANGELOG.md
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubygems_version: 3.0.3
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: generate Date or Time range from human daily time interval.
160
+ test_files: []