datebox 0.1 → 0.2

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
  SHA1:
3
- metadata.gz: 85d1a9e887e9af4219ad6816854c19b3142f70d0
4
- data.tar.gz: 62cef3dbe4579ceb7309402f2e6917d6a1044c54
3
+ metadata.gz: 987247b0d89fd90410e93d3c0474ed9872ee3fef
4
+ data.tar.gz: 6f5f3555c722c186ba548f13728c8e6fa3e2ef44
5
5
  SHA512:
6
- metadata.gz: 731f49fc4373398eb10840785606e87b904e923e2a47579da37150200f58c36901440f6a211313b08c1f630e67503986e86599a400fb24f7d3c0e4ba60de7d83
7
- data.tar.gz: e26b6327ad42596463a81b8d0e6efabd59c125c1ef5239cf63e8178668981cf8ebabcce597009853b0f21b0daf6675f9d615aa0309bed995e6af3bffd930549f
6
+ metadata.gz: a6edab522ecc2b28c1a0b7261e2bbb85aefc7ec95f788dc235bda73795b883686d4b59cd78d7e82d214577f7abdf86965a697cac2f806e280b9a7f8faf58f74d
7
+ data.tar.gz: 0d8a4330cc658a23616e899349e54be21132dca95d1271f05e4c94cbaff5b3d07e190e10cd26d7ac8ec148d2479c3c3c9d81993782c2820a1a349cb4da8a16d6
@@ -0,0 +1,4 @@
1
+ 2014-03-14 Robert Borkowski
2
+
3
+ * Version 0.2
4
+ * Changed Datebox::Relative methods from instance methods to class methods. This requires changes in code.
data/README.md CHANGED
@@ -26,7 +26,7 @@ Allows splitting periods (returns ending dates of periods)
26
26
 
27
27
  It's also possible to calculate periods relative to given dates
28
28
 
29
- period_month = Datebox::Relative.new.last_month.to('2013-07-09')
30
- preiod_week = Datebox::Relative.new.last_week.to('2013-07-09')
29
+ period_month = Datebox::Relative.last_month.to('2013-07-09') # uses period method
30
+ preiod_week = Datebox::Relative.last(:week).to('2013-07-09') # uses peiod symbol
31
31
 
32
32
  It's best to have a look at code & tests
@@ -1,13 +1,19 @@
1
1
  module Datebox
2
2
  class Relative
3
3
 
4
- @period_proc = nil
4
+ @period_proc = nil
5
+
6
+ def initialize(proc)
7
+ @period_proc = proc
8
+ end
9
+
10
+ def to(relative_to)
11
+ relative_to = (relative_to.is_a?(Date) ? relative_to : Date.parse(relative_to))
12
+ @period_proc.call relative_to
13
+ end
14
+
15
+ class << self
5
16
 
6
- def to(relative_to)
7
- relative_to = (relative_to.is_a?(Date) ? relative_to : Date.parse(relative_to))
8
- @period_proc.call relative_to
9
- end
10
-
11
17
  def last(period, options = {})
12
18
  raise "Expected one of: #{Period::PREDEFINED}" unless Period::PREDEFINED.include?(period.to_sym)
13
19
  case period.to_sym
@@ -20,86 +26,78 @@ module Datebox
20
26
  end
21
27
 
22
28
  def same_day
23
- @period_proc = Proc.new {|relative_to| Period.new(relative_to, relative_to) }
24
- self
29
+ new Proc.new {|relative_to| Period.new(relative_to, relative_to) }
25
30
  end
26
31
 
27
32
  def day_before
28
- @period_proc = Proc.new {|relative_to| Period.new(relative_to - 1, relative_to - 1) }
29
- self
33
+ new Proc.new {|relative_to| Period.new(relative_to - 1, relative_to - 1) }
30
34
  end
31
35
 
32
36
  def last_n_days(options = {})
33
37
  days = (options[:days] || options['days']).to_i
34
38
  inclusive = (options[:exclusive] || options['exclusive']) ? false : true # inclusive by default
35
39
  days = 1 if days.nil? || days <= 0 # days should always > 0 since it only return last x days
36
- @period_proc = inclusive ?
40
+ proc = inclusive ?
37
41
  Proc.new {|relative_to| Period.new(relative_to - days + 1, relative_to) } :
38
42
  Proc.new {|relative_to| Period.new(relative_to - days, relative_to - 1) }
39
- self
43
+ new proc
40
44
  end
41
45
 
42
46
  def last_week(options = {})
43
47
  last_weekday = options[:last_weekday] || options['last_weekday'] || 'Sunday'
44
- @period_proc = Proc.new do |relative_to|
48
+ new Proc.new { |relative_to|
45
49
  end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == last_weekday }
46
50
  Period.new(end_date - 6, end_date)
47
- end
48
- self
51
+ }
49
52
  end
50
53
 
51
54
  def last_weekdays_between(start_day, end_day)
52
- @period_proc = Proc.new do |relative_to|
55
+ new Proc.new { |relative_to|
53
56
  end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == end_day }
54
57
  start_date = (end_date - 7 .. end_date).to_a.find { |d| d.strftime("%A") == start_day }
55
58
  Period.new(start_date, end_date)
56
- end
57
- self
59
+ }
58
60
  end
59
61
 
60
62
  def last_weeks_weekdays_as!(*days) #this one returns array!
61
- @period_proc = Proc.new do |relative_to|
63
+ new Proc.new { |relative_to|
62
64
  days.map do |p|
63
65
  (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == p }
64
66
  end
65
- end
66
- self
67
+ }
67
68
  end
68
69
 
69
70
  def last_month
70
- @period_proc = Proc.new do |relative_to|
71
+ new Proc.new { |relative_to|
71
72
  previous_month_start = Date.parse("#{relative_to.prev_month.strftime('%Y-%m')}-01")
72
73
  previous_month_end = previous_month_start.next_month - 1
73
74
  Period.new(previous_month_start, previous_month_end)
74
- end
75
- self
75
+ }
76
76
  end
77
77
 
78
78
  def month_to_date
79
- @period_proc = Proc.new do |relative_to|
79
+ new Proc.new { |relative_to|
80
80
  month_start = Date.parse("#{relative_to.strftime('%Y-%m')}-01")
81
81
  Period.new(month_start, relative_to)
82
- end
83
- self
82
+ }
84
83
  end
85
84
 
86
85
  def last_year
87
- @period_proc = Proc.new do |relative_to|
86
+ new Proc.new { |relative_to|
88
87
  previous_year_start = Date.parse("#{relative_to.prev_year.year}-01-01")
89
88
  previous_year_end = previous_year_start.next_year - 1
90
89
  Period.new(previous_year_start, previous_year_end)
91
- end
92
- self
90
+ }
93
91
  end
94
92
 
95
93
  def year_to_date
96
- @period_proc = Proc.new do |relative_to|
94
+ new Proc.new { |relative_to|
97
95
  year_start = Date.parse("#{relative_to.year}-01-01")
98
96
  Period.new(year_start, relative_to)
99
- end
100
- self
97
+ }
101
98
  end
102
99
 
100
+ end
103
101
  end
104
102
 
105
103
  end
@@ -1,3 +1,3 @@
1
1
  module Datebox
2
- VERSION = "0.1"
2
+ VERSION = "0.2"
3
3
  end
@@ -4,36 +4,36 @@ class TestRelative < Test::Unit::TestCase
4
4
 
5
5
  def test_calculates_correctly
6
6
  # day
7
- assert_equal [Date.parse('2013-07-07')], Datebox::Relative.new.same_day.to('2013-07-07').dates
8
- assert_equal [Date.parse('2013-07-06')], Datebox::Relative.new.day_before.to('2013-07-07').dates
7
+ assert_equal [Date.parse('2013-07-07')], Datebox::Relative.same_day.to('2013-07-07').dates
8
+ assert_equal [Date.parse('2013-07-06')], Datebox::Relative.day_before.to('2013-07-07').dates
9
9
 
10
10
  # n days
11
- assert_equal Datebox::Period.new('2014-06-01', '2014-06-30'), Datebox::Relative.new.last(:n_days, {:days => 30}).to('2014-06-30')
12
- assert_equal Datebox::Period.new('2014-06-01', '2014-06-30'), Datebox::Relative.new.last(:n_days, {days: 30, exclusive: true}).to('2014-07-01')
11
+ assert_equal Datebox::Period.new('2014-06-01', '2014-06-30'), Datebox::Relative.last(:n_days, {:days => 30}).to('2014-06-30')
12
+ assert_equal Datebox::Period.new('2014-06-01', '2014-06-30'), Datebox::Relative.last(:n_days, {days: 30, exclusive: true}).to('2014-07-01')
13
13
 
14
14
  # week
15
- assert_equal Datebox::Period.new('2013-07-01', '2013-07-07'), Datebox::Relative.new.last_week.to('2013-07-09') #tue
16
- assert_equal Datebox::Period.new('2013-06-30', '2013-07-06'), Datebox::Relative.new.last_week({:last_weekday => "Saturday"}).to('2013-07-09') #tue
17
- assert_equal Datebox::Period.new('2013-07-01', '2013-07-05'), Datebox::Relative.new.last_weekdays_between("Monday", "Friday").to('2013-07-09') #tue
18
- assert_equal Datebox::Period.new('2013-07-08', '2013-07-09'), Datebox::Relative.new.last_weekdays_between("Monday", "Tuesday").to('2013-07-09') #tue
15
+ assert_equal Datebox::Period.new('2013-07-01', '2013-07-07'), Datebox::Relative.last_week.to('2013-07-09') # tue
16
+ assert_equal Datebox::Period.new('2013-06-30', '2013-07-06'), Datebox::Relative.last_week({:last_weekday => "Saturday"}).to('2013-07-09') #tue
17
+ assert_equal Datebox::Period.new('2013-07-01', '2013-07-05'), Datebox::Relative.last_weekdays_between("Monday", "Friday").to('2013-07-09') # tue
18
+ assert_equal Datebox::Period.new('2013-07-08', '2013-07-09'), Datebox::Relative.last_weekdays_between("Monday", "Tuesday").to('2013-07-09') #tue
19
19
 
20
20
  # month
21
- assert_equal Datebox::Period.new('2013-06-01', '2013-06-30'), Datebox::Relative.new.last_month.to('2013-07-09')
22
- assert_equal Datebox::Period.new('2013-07-01', '2013-07-09'), Datebox::Relative.new.month_to_date.to('2013-07-09')
23
- assert_equal Datebox::Period.new('2013-02-01', '2013-02-28'), Datebox::Relative.new.last(:month).to('2013-03-02')
21
+ assert_equal Datebox::Period.new('2013-06-01', '2013-06-30'), Datebox::Relative.last_month.to('2013-07-09')
22
+ assert_equal Datebox::Period.new('2013-07-01', '2013-07-09'), Datebox::Relative.month_to_date.to('2013-07-09')
23
+ assert_equal Datebox::Period.new('2013-02-01', '2013-02-28'), Datebox::Relative.last(:month).to('2013-03-02')
24
24
 
25
25
  # year
26
- assert_equal Datebox::Period.new('2012-01-01', '2012-12-31'), Datebox::Relative.new.last_year.to('2013-07-09')
27
- assert_equal Datebox::Period.new('2013-01-01', '2013-07-09'), Datebox::Relative.new.year_to_date.to('2013-07-09')
26
+ assert_equal Datebox::Period.new('2012-01-01', '2012-12-31'), Datebox::Relative.last_year.to('2013-07-09')
27
+ assert_equal Datebox::Period.new('2013-01-01', '2013-07-09'), Datebox::Relative.year_to_date.to('2013-07-09')
28
28
 
29
29
  # anything
30
- assert_equal Datebox::Period.new('2013-06-01', '2013-06-01'), Datebox::Relative.new.last(:day).to('2013-06-02')
31
- assert_equal Datebox::Period.new('2013-06-01', '2013-06-30'), Datebox::Relative.new.last(:month).to('2013-07-09')
32
- assert_equal Datebox::Period.new('2014-02-03', '2014-02-09'), Datebox::Relative.new.last(:week).to('2014-02-10')
33
- assert_equal Datebox::Period.new('2014-02-02', '2014-02-08'), Datebox::Relative.new.last(:week, {:last_weekday => "Saturday"}).to('2014-02-10')
30
+ assert_equal Datebox::Period.new('2013-06-01', '2013-06-01'), Datebox::Relative.last(:day).to('2013-06-02')
31
+ assert_equal Datebox::Period.new('2013-06-01', '2013-06-30'), Datebox::Relative.last(:month).to('2013-07-09')
32
+ assert_equal Datebox::Period.new('2014-02-03', '2014-02-09'), Datebox::Relative.last(:week).to('2014-02-10')
33
+ assert_equal Datebox::Period.new('2014-02-02', '2014-02-08'), Datebox::Relative.last(:week, {:last_weekday => "Saturday"}).to('2014-02-10')
34
34
 
35
35
  # the one that's different
36
- assert_equal [Date.parse('2013-07-01'), Date.parse('2013-07-03')], Datebox::Relative.new.last_weeks_weekdays_as!("Monday", "Wednesday").to('2013-07-05') #fri
36
+ 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
37
37
  end
38
38
 
39
39
  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.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Borkowski
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-05 00:00:00.000000000 Z
11
+ date: 2014-03-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Offers help with managing dates and periods
14
14
  email:
@@ -18,6 +18,7 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - .gitignore
21
+ - CHANGELOG.txt
21
22
  - Gemfile
22
23
  - LICENSE
23
24
  - README.md