datebox 0.4.1 → 0.4.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8973e5e78c939fdc2aed702c51b3569fc246db78
4
- data.tar.gz: 824c632d4044104e6f9a996f11999352f4e8e397
3
+ metadata.gz: f867ee46d817fd7925ee67d2b7da65c77e9a0b3d
4
+ data.tar.gz: 765f0d7e2873bd6196baa5d2215e1c0128796f84
5
5
  SHA512:
6
- metadata.gz: da95609365d0055017a0f9dbe807ae0179bddd0c8142f524c00fddc6babd03bf3d6d85a370eed12b45f5c8994e1aca2bd82f1265b9cf1cd080bfba09be82cd7b
7
- data.tar.gz: e49e7a934141d7f3ba9d06000db5815792f42a7720d632595bc13dbf5187f956676a4c5e52ffc15e015beb8fd311ab03f497f6d96d30c003862bff8e764731dd
6
+ metadata.gz: 1ea5e6dc0cb49ac0ba631177595f1a20b84c32d2f726f0a5abfa79377f40158dcb83b92a5ff8eaadbc5ceb42f2a88c081419ebd3b13af3c9807568137b91e76d
7
+ data.tar.gz: 404b27cd4d7d1938ae8cc47e3df2bb6e57b8ea52a3a690b3af79f865ab8b51d6758c97cf960cc521d033e4e1e98ad7acb039cb946af5f613730049d58588c048
@@ -1,4 +1,5 @@
1
- 2014-03-14 Robert Borkowski
1
+ 0.4.3
2
+ Changed behaviour of last_week to be more consistent with other last_ methods particularly on Sunday
2
3
 
3
- * Version 0.2
4
- * Changed Datebox::Relative methods from instance methods to class methods. This requires changes in code.
4
+ 0.2.0
5
+ Changed Datebox::Relative methods from instance methods to class methods. This requires changes in code.
@@ -3,7 +3,7 @@ module Datebox
3
3
 
4
4
  @period_proc = nil
5
5
  attr_reader :period_name
6
-
6
+
7
7
  def initialize(proc, name = nil)
8
8
  @period_proc = proc
9
9
  @period_name = name
@@ -44,12 +44,12 @@ module Datebox
44
44
  def day_before
45
45
  new Proc.new {|relative_to| Period.new(relative_to - 1, relative_to - 1) }, __method__.to_s
46
46
  end
47
-
47
+
48
48
  def day_apart(difference)
49
49
  new Proc.new {|relative_to| Period.new(relative_to + difference, relative_to + difference) }, __method__.to_s
50
50
  end
51
-
52
- def method_missing(m, *args, &block)
51
+
52
+ def method_missing(m, *args, &block)
53
53
  if (m.to_s =~ /^day_ago_\d+$/) or (m.to_s =~ /^day_in_\d+$/)
54
54
  days = m.to_s.split('_').last.to_i
55
55
  if m.to_s =~ /^day_ago_\d+$/
@@ -62,8 +62,8 @@ module Datebox
62
62
  else
63
63
  super
64
64
  end
65
- end
66
-
65
+ end
66
+
67
67
  def last_n_days(options = {})
68
68
  days = (options[:days] || options['days']).to_i
69
69
  inclusive = (options[:exclusive] || options['exclusive']) ? false : true # inclusive by default
@@ -77,6 +77,7 @@ module Datebox
77
77
  def last_week(options = {})
78
78
  last_weekday = options[:last_weekday] || options['last_weekday'] || 'Sunday'
79
79
  new Proc.new { |relative_to|
80
+ relative_to -= 1
80
81
  end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == last_weekday }
81
82
  Period.new(end_date - 6, end_date)
82
83
  }, __method__.to_s
@@ -110,8 +111,20 @@ module Datebox
110
111
  }, __method__.to_s
111
112
  end
112
113
 
114
+ def same_week(options = {})
115
+ last_weekday = options[:last_weekday] || options['last_weekday'] || 'Sunday'
116
+ new Proc.new { |relative_to|
117
+ if relative_to.strftime("%A") == last_weekday
118
+ Period.new(relative_to - 6, relative_to)
119
+ else
120
+ end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == last_weekday }
121
+ Period.new(end_date + 1, end_date + 7)
122
+ end
123
+ }, __method__.to_s
124
+ end
125
+
113
126
  def last_month
114
- new Proc.new { |relative_to|
127
+ new Proc.new { |relative_to|
115
128
  previous_month_start = Date.parse("#{relative_to.prev_month.strftime('%Y-%m')}-01")
116
129
  previous_month_end = previous_month_start.next_month - 1
117
130
  Period.new(previous_month_start, previous_month_end)
@@ -125,6 +138,14 @@ module Datebox
125
138
  }, __method__.to_s
126
139
  end
127
140
 
141
+ def same_month
142
+ new Proc.new { |relative_to|
143
+ same_month_start = Date.parse("#{relative_to.strftime('%Y-%m')}-01")
144
+ same_month_end = same_month_start.next_month - 1
145
+ Period.new(same_month_start, same_month_end)
146
+ }, __method__.to_s
147
+ end
148
+
128
149
  def last_year
129
150
  new Proc.new { |relative_to|
130
151
  previous_year_start = Date.parse("#{relative_to.prev_year.year}-01-01")
@@ -142,5 +163,5 @@ module Datebox
142
163
 
143
164
  end
144
165
  end
145
-
146
- end
166
+
167
+ end
@@ -1,3 +1,3 @@
1
1
  module Datebox
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.3"
3
3
  end
@@ -5,7 +5,7 @@ class TestRelative < Test::Unit::TestCase
5
5
  def test_gives_back_correct_period_name_in_proc
6
6
  assert_equal :day_in_19, Datebox::Relative.day_in_19.period_name
7
7
  end
8
-
8
+
9
9
  def test_calculates_correctly
10
10
  # day
11
11
  assert_equal [Date.parse('2013-07-07')], Datebox::Relative.same_day.to('2013-07-07').dates
@@ -24,6 +24,7 @@ class TestRelative < Test::Unit::TestCase
24
24
  # week
25
25
  assert_equal Datebox::Period.new('2013-07-01', '2013-07-07'), Datebox::Relative.last_week.to('2013-07-09') # tue
26
26
  assert_equal Datebox::Period.new('2013-06-30', '2013-07-06'), Datebox::Relative.last_week({:last_weekday => "Saturday"}).to('2013-07-09') #tue
27
+ assert_equal Datebox::Period.new('2015-10-26', '2015-11-01'), Datebox::Relative.last_week.to('2015-11-08') # sun
27
28
  assert_equal Datebox::Period.new('2013-07-01', '2013-07-05'), Datebox::Relative.last_weekdays_between("Monday", "Friday").to('2013-07-09') # tue
28
29
  assert_equal Datebox::Period.new('2013-07-08', '2013-07-09'), Datebox::Relative.last_weekdays_between("Monday", "Tuesday").to('2013-07-09') #tue
29
30
 
@@ -51,6 +52,13 @@ class TestRelative < Test::Unit::TestCase
51
52
 
52
53
  # the one that's different
53
54
  assert_equal [Date.parse('2013-07-01'), Date.parse('2013-07-03')], Datebox::Relative.last_weeks_weekdays_as!("Monday", "Wednesday").to('2013-07-05') #fri
55
+
56
+ assert_equal Datebox::Period.new('2015-11-02', '2015-11-08'), Datebox::Relative.same_week.to('2015-11-02') # mon
57
+ assert_equal Datebox::Period.new('2015-11-02', '2015-11-08'), Datebox::Relative.same_week.to('2015-11-03') # tue
58
+ assert_equal Datebox::Period.new('2015-11-02', '2015-11-08'), Datebox::Relative.same_week.to('2015-11-08') # sun
59
+
60
+
61
+ assert_equal Datebox::Period.new('2015-11-01', '2015-11-30'), Datebox::Relative.same_month.to('2015-11-30')
54
62
  end
55
63
 
56
- end
64
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datebox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Borkowski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-14 00:00:00.000000000 Z
11
+ date: 2017-09-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Offers help with managing dates and periods
14
14
  email:
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  version: '0'
52
52
  requirements: []
53
53
  rubyforge_project:
54
- rubygems_version: 2.4.5
54
+ rubygems_version: 2.6.11
55
55
  signing_key:
56
56
  specification_version: 4
57
57
  summary: Got fed up with implementing date related functionality everywhere