datebox 0.0.4 → 0.1

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: a8f9797007395a67f85af5df4b2772f8cd19e261
4
- data.tar.gz: bfe77be8897b4cd70ce2d84ca87c1c28843f034c
3
+ metadata.gz: 85d1a9e887e9af4219ad6816854c19b3142f70d0
4
+ data.tar.gz: 62cef3dbe4579ceb7309402f2e6917d6a1044c54
5
5
  SHA512:
6
- metadata.gz: c9adf391fdc13d71f26192c9ed439b904dbb4d306961fb42e4ca2e1aa019ecaf7087a8457fc60dcfc1c899c8560e0e9aa134158154e9c5f49c7f0f3700457474
7
- data.tar.gz: 9636eb62528967d2052ecc8e5f1c3e86213d4e20f3799c6aac25c3219901a7820b6522a826bbcb852a75f7520ec2c504d876d8fc330545c4de6cdb8b40ca0dde
6
+ metadata.gz: 731f49fc4373398eb10840785606e87b904e923e2a47579da37150200f58c36901440f6a211313b08c1f630e67503986e86599a400fb24f7d3c0e4ba60de7d83
7
+ data.tar.gz: e26b6327ad42596463a81b8d0e6efabd59c125c1ef5239cf63e8178668981cf8ebabcce597009853b0f21b0daf6675f9d615aa0309bed995e6af3bffd930549f
@@ -2,6 +2,8 @@ module Datebox
2
2
  class Period
3
3
  attr_reader :from, :to
4
4
 
5
+ PREDEFINED = [:day, :n_days, :week, :month, :year]
6
+
5
7
  def initialize(from, to)
6
8
  @from = from.is_a?(Date) ? from : Date.parse(from)
7
9
  @to = to.is_a?(Date) ? to : Date.parse(to)
@@ -17,21 +19,34 @@ module Datebox
17
19
  end
18
20
 
19
21
  def split_dates(period, options = {})
20
- self.class.split_dates(from, to, period, options)
22
+ raise "Expected one of: #{Period::PREDEFINED}" unless Period::PREDEFINED.include?(period.to_sym)
23
+ self.class.split_dates(from, to, period.to_sym, options)
21
24
  end
22
25
 
23
26
  class << self
24
27
  def split_dates(start_date, end_date, period, options = {})
25
- return (start_date..end_date).to_a if period == "day"
26
- return split_monthly_dates(start_date, end_date) if period == "month"
27
- if period =~ /week/
28
- return split_weekly_dates(start_date, end_date, options.merge({last_weekday: "Saturday"})) if period == "week_ss"
29
- return split_weekly_dates(start_date, end_date, options)
28
+ period = period.to_sym
29
+
30
+ return (start_date..end_date).to_a if period == :day
31
+ return split_days_dates(start_date, end_date, options) if period == :n_days
32
+ return split_weekly_dates(start_date, end_date, options) if period == :week
33
+ return split_monthly_dates(start_date, end_date) if period == :month
34
+ return split_yearly_dates(start_date, end_date) if period == :year
35
+ end
36
+
37
+ def split_days_dates(start_date, end_date, options = {})
38
+ days = options[:days] || options['days']
39
+ raise "days must be specified" if days.nil?
40
+
41
+ end_dates = []
42
+ (start_date..end_date).to_a.reverse.each_slice(days) do |range|
43
+ end_dates << range.first
30
44
  end
45
+ end_dates.sort
31
46
  end
32
47
 
33
48
  def split_weekly_dates(start_date, end_date, options = {})
34
- last_weekday = options[:last_weekday] || "Sunday"
49
+ last_weekday = options[:last_weekday] || options['last_weekday'] || 'Sunday'
35
50
  end_dates = []
36
51
  end_of_week = (end_date.downto end_date - 6).to_a.find { |d| d.strftime("%A") == last_weekday }
37
52
  while end_of_week - 6 >= start_date
@@ -40,7 +55,7 @@ module Datebox
40
55
  end
41
56
  end_dates.sort
42
57
  end
43
-
58
+
44
59
  def split_monthly_dates(start_date, end_date)
45
60
  end_dates = []
46
61
  beginning_of_month = ::Date.parse("#{end_date.year}-#{end_date.month}-01").next_month
@@ -52,6 +67,18 @@ module Datebox
52
67
  end
53
68
  end_dates.sort
54
69
  end
70
+
71
+ def split_yearly_dates(start_date, end_date)
72
+ end_dates = []
73
+ beginning_of_year = ::Date.parse("#{end_date.year}-01-01").next_year
74
+ end_of_year = (beginning_of_year - 1 == end_date) ? end_date : beginning_of_year.prev_year - 1
75
+ while beginning_of_year.prev_year >= start_date
76
+ end_dates << end_of_year
77
+ beginning_of_year = ::Date.parse("#{end_of_year.year}-01-01")
78
+ end_of_year = beginning_of_year - 1
79
+ end
80
+ end_dates.sort
81
+ end
55
82
  end
56
83
 
57
84
  end
@@ -1,7 +1,6 @@
1
1
  module Datebox
2
2
  class Relative
3
3
 
4
- @period = nil
5
4
  @period_proc = nil
6
5
 
7
6
  def to(relative_to)
@@ -9,14 +8,12 @@ module Datebox
9
8
  @period_proc.call relative_to
10
9
  end
11
10
 
12
- def last(period)
13
- periods = [:day, :week, :week_ms, :week_ss, :month, :year] # week monday-sunday & week sunday-saturday
14
- raise "Expected one of: #{periods}" unless periods.include?(period)
15
- case period
11
+ def last(period, options = {})
12
+ raise "Expected one of: #{Period::PREDEFINED}" unless Period::PREDEFINED.include?(period.to_sym)
13
+ case period.to_sym
16
14
  when :day then day_before
17
- when :week then last_week
18
- when :week_ms then last_week
19
- when :week_ss then last_week("Saturday")
15
+ when :n_days then last_n_days(options)
16
+ when :week then last_week(options)
20
17
  when :month then last_month
21
18
  when :year then last_year
22
19
  end
@@ -31,8 +28,19 @@ module Datebox
31
28
  @period_proc = Proc.new {|relative_to| Period.new(relative_to - 1, relative_to - 1) }
32
29
  self
33
30
  end
31
+
32
+ def last_n_days(options = {})
33
+ days = (options[:days] || options['days']).to_i
34
+ inclusive = (options[:exclusive] || options['exclusive']) ? false : true # inclusive by default
35
+ days = 1 if days.nil? || days <= 0 # days should always > 0 since it only return last x days
36
+ @period_proc = inclusive ?
37
+ Proc.new {|relative_to| Period.new(relative_to - days + 1, relative_to) } :
38
+ Proc.new {|relative_to| Period.new(relative_to - days, relative_to - 1) }
39
+ self
40
+ end
34
41
 
35
- def last_week(last_weekday = "Sunday")
42
+ def last_week(options = {})
43
+ last_weekday = options[:last_weekday] || options['last_weekday'] || 'Sunday'
36
44
  @period_proc = Proc.new do |relative_to|
37
45
  end_date = (relative_to.downto relative_to - 6).to_a.find { |d| d.strftime("%A") == last_weekday }
38
46
  Period.new(end_date - 6, end_date)
@@ -1,3 +1,3 @@
1
1
  module Datebox
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1"
3
3
  end
@@ -6,17 +6,20 @@ class TestPeriod < Test::Unit::TestCase
6
6
  #day
7
7
  assert_equal [Date.today - 1], Datebox::Period.split_dates(Date.today - 1, Date.today - 1, "day")
8
8
  assert_equal [Date.today - 3, Date.today - 2, Date.today - 1], Datebox::Period.split_dates(Date.today - 3, Date.today - 1, "day")
9
+
9
10
  #week
10
11
  assert_equal [], Datebox::Period.split_dates(Date.parse("2013-06-15"), Date.parse("2013-06-22"), "week") #sat to sat
11
12
  assert_equal [Date.parse("2013-06-23")], Datebox::Period.split_dates(Date.parse("2013-06-14"), Date.parse("2013-06-27"), "week") #fri to thu
12
13
  assert_equal [Date.parse("2013-06-23")], Datebox::Period.split_dates(Date.parse("2013-06-17"), Date.parse("2013-06-23"), "week") #mon to sun
13
- assert_equal [Date.parse("2014-02-15")], Datebox::Period.split_dates(Date.parse("2014-02-09"), Date.parse("2014-02-15"), "week_ss") #sun to sat
14
+ assert_equal [Date.parse("2014-02-15")], Datebox::Period.split_dates(Date.parse("2014-02-09"), Date.parse("2014-02-15"), "week", {:last_weekday => "Saturday"}) #sun to sat
14
15
  assert_equal [Date.parse("2013-06-16"), Date.parse("2013-06-23")], Datebox::Period.split_dates(Date.parse("2013-06-10"), Date.parse("2013-06-27"), "week") #mon to thu
16
+
15
17
  #month
16
18
  assert_equal [], Datebox::Period.split_dates(Date.parse("2013-01-02"), Date.parse("2013-01-31"), "month")
17
19
  assert_equal [Date.parse("2013-01-31")], Datebox::Period.split_dates(Date.parse("2013-01-01"), Date.parse("2013-01-31"), "month")
18
20
  assert_equal [Date.parse("2013-01-31")], Datebox::Period.split_dates(Date.parse("2012-12-02"), Date.parse("2013-02-02"), "month")
19
21
  assert_equal [Date.parse("2013-01-31"), Date.parse("2013-02-28")], Datebox::Period.split_dates(Date.parse("2012-12-10"), Date.parse("2013-03-03"), "month")
22
+
20
23
  #instance method test
21
24
  assert_equal [Date.parse("2013-06-16"), Date.parse("2013-06-23")], Datebox::Period.new("2013-06-10", "2013-06-27").split_dates("week") #mon to thu
22
25
  end
@@ -7,15 +7,20 @@ class TestRelative < Test::Unit::TestCase
7
7
  assert_equal [Date.parse('2013-07-07')], Datebox::Relative.new.same_day.to('2013-07-07').dates
8
8
  assert_equal [Date.parse('2013-07-06')], Datebox::Relative.new.day_before.to('2013-07-07').dates
9
9
 
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')
13
+
10
14
  # week
11
15
  assert_equal Datebox::Period.new('2013-07-01', '2013-07-07'), Datebox::Relative.new.last_week.to('2013-07-09') #tue
12
- assert_equal Datebox::Period.new('2013-06-30', '2013-07-06'), Datebox::Relative.new.last_week("Saturday").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
13
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
14
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
19
 
16
20
  # month
17
21
  assert_equal Datebox::Period.new('2013-06-01', '2013-06-30'), Datebox::Relative.new.last_month.to('2013-07-09')
18
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')
19
24
 
20
25
  # year
21
26
  assert_equal Datebox::Period.new('2012-01-01', '2012-12-31'), Datebox::Relative.new.last_year.to('2013-07-09')
@@ -25,7 +30,7 @@ class TestRelative < Test::Unit::TestCase
25
30
  assert_equal Datebox::Period.new('2013-06-01', '2013-06-01'), Datebox::Relative.new.last(:day).to('2013-06-02')
26
31
  assert_equal Datebox::Period.new('2013-06-01', '2013-06-30'), Datebox::Relative.new.last(:month).to('2013-07-09')
27
32
  assert_equal Datebox::Period.new('2014-02-03', '2014-02-09'), Datebox::Relative.new.last(:week).to('2014-02-10')
28
- assert_equal Datebox::Period.new('2014-02-02', '2014-02-08'), Datebox::Relative.new.last(:week_ss).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')
29
34
 
30
35
  # the one that's different
31
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
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.0.4
4
+ version: '0.1'
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-02-12 00:00:00.000000000 Z
11
+ date: 2014-03-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Offers help with managing dates and periods
14
14
  email: