active_date_range 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e93ef4c06d102fab1f4d60e5a6e8ef093f80f73e5d18ae80feb7b3ff952e53d3
4
- data.tar.gz: f3e0b0fcfc7e7ae3a4085ceb6eb719b13c8b03452c7219d9570f6634768327a6
3
+ metadata.gz: 39ccc9d0058b87f4924d253b5e0018b0d95b7eb1f97177e4a2b09f85a6d438e5
4
+ data.tar.gz: e80e3a5cfd455008ee8354f4342922b2b601fafabab4e7af8ad223f2a1cccf4f
5
5
  SHA512:
6
- metadata.gz: 0e6ec69527db1a35890f69111e4b1a00f2d1158b2f7bfaeb5d45b51cc7f0a943876c9a4c2cb0cc6da944e251e836c02c8b3a5ccaee43b9cfb351f2cc9340159a
7
- data.tar.gz: f5405fc96a213e444dd62a3cd4caf97601a0852937204c5261cf7cb9d979bdfca20b8920f4988af8e6ecd30cb7797e28355ff0f71781dd1cb96677d3ad56b464
6
+ metadata.gz: 7f16e285ad8b6075546916ef1a31342b82ae9f6e84e87ebaf6081326676be69f59c0ef72e5b85460bdc87586b403341df922e816cb54561b8834b6fb5d07828b
7
+ data.tar.gz: dbf53b90fd564dd372e8af5b3cc974521df3434d0f1a6e79ffed73a81c372b122a25e4af2d130e6b8b10c10a9a5805c920ec2a8209a6cda71ff50315c5390975
@@ -0,0 +1,57 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ main ]
13
+ pull_request:
14
+ branches: [ main ]
15
+
16
+ jobs:
17
+ test:
18
+ runs-on: ubuntu-latest
19
+ strategy:
20
+ matrix:
21
+ ruby-version: ['2.7', '3.0']
22
+
23
+ steps:
24
+ - uses: actions/checkout@v2
25
+ - name: Set up Ruby
26
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
27
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
28
+ # uses: ruby/setup-ruby@v1
29
+ uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
30
+ with:
31
+ ruby-version: ${{ matrix.ruby-version }}
32
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
33
+ - name: Run tests
34
+ run: bundle exec rake
35
+
36
+ lint:
37
+ runs-on: ubuntu-latest
38
+
39
+ steps:
40
+ - uses: actions/checkout@v2
41
+ - name: Set up Ruby 2.7
42
+ uses: ruby/setup-ruby@v1
43
+ with:
44
+ ruby-version: 2.7
45
+ - name: Cache gems
46
+ uses: actions/cache@v1
47
+ with:
48
+ path: vendor/bundle
49
+ key: ${{ runner.os }}-rubocop-${{ hashFiles('**/Gemfile.lock') }}
50
+ restore-keys: |
51
+ ${{ runner.os }}-rubocop-
52
+ - name: Install gems
53
+ run: |
54
+ bundle config path vendor/bundle
55
+ bundle install --jobs 4 --retry 3
56
+ - name: Run RuboCop
57
+ run: bundle exec rubocop --parallel
data/.rubocop.yml CHANGED
@@ -8,7 +8,6 @@ AllCops:
8
8
  # RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
9
9
  # to ignore them, so only the ones explicitly set in this file are enabled.
10
10
  DisabledByDefault: true
11
- SuggestExtensions: false
12
11
  Exclude:
13
12
  - '**/tmp/**/*'
14
13
  - '**/templates/**/*'
data/CHANGELOG.md CHANGED
@@ -1,4 +1,15 @@
1
- == 0.1.0
1
+ ## 0.2.0
2
+
3
+ * Add support for weeks:
4
+
5
+ - Shorthands for `this_week`, `next_week` and `prev_week`
6
+ - `full_week?` and `one_week?`
7
+ - `next` and `previous` now handle weeks correctly
8
+ - Tests for biweekly calculations
9
+
10
+ *Edwin Vlieg*
11
+
12
+ ## 0.1.0
2
13
 
3
14
  * Initial import of the DateRange implementation from the internal implementation in the Moneybird code.
4
15
 
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "https://rubygems.org"
2
4
 
3
5
  # Specify your gem's dependencies in active_date_range.gemspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_date_range (0.1.0)
4
+ active_date_range (0.2.0)
5
5
  activesupport (~> 6.1)
6
6
  i18n
7
7
 
data/Guardfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # A sample Guardfile
2
4
  # More info at https://github.com/guard/guard#readme
3
5
 
@@ -19,11 +21,11 @@ clearing :on
19
21
 
20
22
  guard :minitest do
21
23
  # with Minitest::Unit
22
- watch(%r{^test/(.*)\/?test_(.*)\.rb$})
23
- watch(%r{^test/(.*)\/?(.*)_test\.rb$})
24
+ watch(%r{^test/(.*)/?test_(.*)\.rb$})
25
+ watch(%r{^test/(.*)/?(.*)_test\.rb$})
24
26
  watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
25
27
  watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
26
- watch(%r{^test/test_helper\.rb$}) { 'test' }
28
+ watch(%r{^test/test_helper\.rb$}) { "test" }
27
29
 
28
30
  # with Minitest::Spec
29
31
  # watch(%r{^spec/(.*)_spec\.rb$})
data/README.md CHANGED
@@ -29,18 +29,21 @@ ActiveDateRange::DateRange.new(Date.new(2021, 1, 1), Date.new(2021, 12, 31))
29
29
  ActiveDateRange::DateRange.new(Date.new(2021, 1, 1)..Date.new(2021, 12, 31))
30
30
  ```
31
31
 
32
- You can also use shorthands to initialize a range relative to today. Shorthands are available for `this`, `prev` and `next` for the ranges `month`, `quarter` and `year`:
32
+ You can also use shorthands to initialize a range relative to today. Shorthands are available for `this`, `prev` and `next` for the ranges `month`, `quarter`, `year` and `week`:
33
33
 
34
34
  ```ruby
35
35
  ActiveDateRange::DateRange.this_month
36
36
  ActiveDateRange::DateRange.this_year
37
37
  ActiveDateRange::DateRange.this_quarter
38
+ ActiveDateRange::DateRange.this_week
38
39
  ActiveDateRange::DateRange.prev_month
39
40
  ActiveDateRange::DateRange.prev_year
40
41
  ActiveDateRange::DateRange.prev_quarter
42
+ ActiveDateRange::DateRange.prev_week
41
43
  ActiveDateRange::DateRange.next_month
42
44
  ActiveDateRange::DateRange.next_year
43
45
  ActiveDateRange::DateRange.next_quarter
46
+ ActiveDateRange::DateRange.next_week
44
47
  ```
45
48
 
46
49
  The third option is to use parse:
@@ -71,6 +74,7 @@ date_range.months # => 12
71
74
  date_range.quarters # => 4
72
75
  date_range.years # => 1
73
76
  date_range.one_month? # => false
77
+ date_range.one_week? # => false
74
78
  date_range.one_year? # => true
75
79
  date_range.full_year? # => true
76
80
  date_range.same_year? # => true
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
4
  require "rake/testtask"
3
5
 
@@ -7,4 +9,4 @@ Rake::TestTask.new(:test) do |t|
7
9
  t.test_files = FileList["test/**/*_test.rb"]
8
10
  end
9
11
 
10
- task :default => :test
12
+ task default: :test
@@ -1,4 +1,6 @@
1
- require_relative 'lib/active_date_range/version'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/active_date_range/version"
2
4
 
3
5
  Gem::Specification.new do |spec|
4
6
  spec.name = "active_date_range"
@@ -9,15 +11,15 @@ Gem::Specification.new do |spec|
9
11
  spec.summary = "DateRange for ActiveSupport"
10
12
  spec.description = "ActiveDateRange provides a range of dates with a powerful API to manipulate and use date ranges in your software."
11
13
  spec.homepage = "https://github.com/moneybird/active-date-range"
12
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
13
15
 
14
16
  spec.metadata["homepage_uri"] = spec.homepage
15
17
  spec.metadata["source_code_uri"] = "https://github.com/moneybird/active-date-range"
16
- spec.metadata["changelog_uri"] = "https://github.com/moneybird/active-date-range/CHANGELOG.md"
18
+ spec.metadata["changelog_uri"] = "https://github.com/moneybird/active-date-range/blob/main/CHANGELOG.md"
17
19
 
18
20
  # Specify which files should be added to the gem when it is released.
19
21
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
21
23
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
24
  end
23
25
  spec.bindir = "exe"
data/bin/console CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require "bundler/setup"
4
5
  require "active_support"
@@ -12,7 +12,10 @@ module ActiveDateRange
12
12
  next_quarter: -> { DateRange.new(3.months.from_now.to_date.all_quarter) },
13
13
  this_year: -> { DateRange.new(Time.zone.today.all_year) },
14
14
  prev_year: -> { DateRange.new(12.months.ago.to_date.all_year) },
15
- next_year: -> { DateRange.new(12.months.from_now.to_date.all_year) }
15
+ next_year: -> { DateRange.new(12.months.from_now.to_date.all_year) },
16
+ this_week: -> { DateRange.new(Time.zone.today.all_week) },
17
+ prev_week: -> { DateRange.new(1.week.ago.to_date.all_week) },
18
+ next_week: -> { DateRange.new(1.week.from_now.to_date.all_week) }
16
19
  }.freeze
17
20
 
18
21
  RANGE_PART_REGEXP = %r{\A(?<year>((1\d|2\d)\d\d))-?(?<month>0[1-9]|1[012])-?(?<day>[0-2]\d|3[01])?\z}
@@ -107,6 +110,13 @@ module ActiveDateRange
107
110
  months / 12
108
111
  end
109
112
 
113
+ # Returns the number of weeks on the range or nil when range is no full week
114
+ def weeks
115
+ return nil unless full_week?
116
+
117
+ days / 7
118
+ end
119
+
110
120
  # Returns true when begin of the range is at the beginning of the month
111
121
  def begin_at_beginning_of_month?
112
122
  self.begin.day == 1
@@ -122,6 +132,11 @@ module ActiveDateRange
122
132
  begin_at_beginning_of_month? && self.begin.month == 1
123
133
  end
124
134
 
135
+ # Returns true when begin of the range is at the beginning of the week
136
+ def begin_at_beginning_of_week?
137
+ self.begin == self.begin.at_beginning_of_week
138
+ end
139
+
125
140
  # Returns true when the range is exactly one month long
126
141
  def one_month?
127
142
  (28..31).cover?(days) &&
@@ -143,6 +158,12 @@ module ActiveDateRange
143
158
  self.end == self.begin.at_end_of_year
144
159
  end
145
160
 
161
+ def one_week?
162
+ days == 7 &&
163
+ begin_at_beginning_of_week? &&
164
+ self.end == self.begin.at_end_of_week
165
+ end
166
+
146
167
  # Returns true when the range is exactly one or more months long
147
168
  def full_month?
148
169
  begin_at_beginning_of_month? && self.end == self.end.at_end_of_month
@@ -164,6 +185,13 @@ module ActiveDateRange
164
185
 
165
186
  alias :full_years? :full_year?
166
187
 
188
+ # Returns true when the range is exactly one or more weeks long
189
+ def full_week?
190
+ begin_at_beginning_of_week? && self.end == self.end.at_end_of_week
191
+ end
192
+
193
+ alias :full_weeks? :full_week?
194
+
167
195
  # Returns true when begin and end are in the same year
168
196
  def same_year?
169
197
  self.begin.year == self.end.year
@@ -196,6 +224,8 @@ module ActiveDateRange
196
224
  :quarter
197
225
  elsif one_month?
198
226
  :month
227
+ elsif one_week?
228
+ :week
199
229
  end
200
230
  end
201
231
 
@@ -244,13 +274,17 @@ module ActiveDateRange
244
274
  # DateRange.this_month.previous # => DateRange.prev_month
245
275
  # DateRange.this_month.previous(2) # => DateRange.prev_month.previous + DateRange.prev_month
246
276
  def previous(periods = 1)
247
- if granularity
248
- DateRange.new(self.begin - periods.send(granularity), self.begin - 1.day)
277
+ begin_date = if granularity
278
+ self.begin - periods.send(granularity)
249
279
  elsif full_month?
250
- DateRange.new(in_groups_of(:month).first.previous(periods * months).begin, self.begin - 1.day)
280
+ in_groups_of(:month).first.previous(periods * months).begin
251
281
  else
252
- DateRange.new((self.begin - (periods * days).days).at_beginning_of_month, self.begin - 1.day)
282
+ (self.begin - (periods * days).days)
253
283
  end
284
+
285
+ begin_date = begin_date.at_beginning_of_month if full_month?
286
+
287
+ DateRange.new(begin_date, self.begin - 1.day)
254
288
  end
255
289
 
256
290
  # Returns the period next to the current period. `periods` can be raised to return more
@@ -259,11 +293,10 @@ module ActiveDateRange
259
293
  # DateRange.this_month.next # => DateRange.next_month
260
294
  # DateRange.this_month.next(2) # => DateRange.next_month + DateRange.next_month.next
261
295
  def next(periods = 1)
262
- if granularity
263
- DateRange.new(self.end + 1.day, (self.end + periods.send(granularity)).at_end_of_month)
264
- else
265
- DateRange.new(self.end + 1.day, (self.end + days.days).at_end_of_month)
266
- end
296
+ end_date = self.end + (granularity ? periods.send(granularity) : days.days)
297
+ end_date = end_date.at_end_of_month if full_month?
298
+
299
+ DateRange.new(self.end + 1.day, end_date)
267
300
  end
268
301
 
269
302
  # Returns an array with date ranges containing full months/quarters/years in the current range.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
4
  require "i18n"
3
5
  require "i18n/backend/fallbacks"
@@ -25,12 +25,15 @@ en:
25
25
  relative_range:
26
26
  next_month: "next month"
27
27
  next_quarter: "next quarter"
28
+ next_week: "next week"
28
29
  next_year: "next year"
29
30
  prev_month: "the previous month"
30
31
  prev_quarter: "the previous quarter"
32
+ prev_week: "the previous week"
31
33
  prev_year: "the previous year"
32
34
  this_month: "this month"
33
35
  this_quarter: "this quarter"
36
+ this_week: "this week"
34
37
  this_year: "this year"
35
38
  today: "today"
36
39
  long_quarter: "quarter %{quarter}"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveDateRange
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_date_range
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edwin Vlieg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-22 00:00:00.000000000 Z
11
+ date: 2021-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -102,6 +102,7 @@ executables: []
102
102
  extensions: []
103
103
  extra_rdoc_files: []
104
104
  files:
105
+ - ".github/workflows/ruby.yml"
105
106
  - ".gitignore"
106
107
  - ".rubocop.yml"
107
108
  - ".travis.yml"
@@ -128,7 +129,7 @@ licenses: []
128
129
  metadata:
129
130
  homepage_uri: https://github.com/moneybird/active-date-range
130
131
  source_code_uri: https://github.com/moneybird/active-date-range
131
- changelog_uri: https://github.com/moneybird/active-date-range/CHANGELOG.md
132
+ changelog_uri: https://github.com/moneybird/active-date-range/blob/main/CHANGELOG.md
132
133
  post_install_message:
133
134
  rdoc_options: []
134
135
  require_paths:
@@ -137,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
138
  requirements:
138
139
  - - ">="
139
140
  - !ruby/object:Gem::Version
140
- version: 2.3.0
141
+ version: 2.7.0
141
142
  required_rubygems_version: !ruby/object:Gem::Requirement
142
143
  requirements:
143
144
  - - ">="