cal 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/cal.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "cal/version"
2
+ require 'active_support/time'
2
3
 
3
4
  module Cal
4
5
 
@@ -3,6 +3,8 @@ require 'active_support/core_ext/module/delegation'
3
3
  module Cal
4
4
  class Day
5
5
 
6
+ include Comparable
7
+
6
8
  def initialize(date, calendar)
7
9
  @date = date
8
10
  @calendar = calendar
@@ -13,7 +15,15 @@ module Cal
13
15
  delegate :today?, :to => :date
14
16
 
15
17
  def ==(other)
16
- other.is_a?(Day) && other.calendar == calendar && other.date == date
18
+ other.is_a?(Day) && calendar == other.calendar && date == other.date
19
+ end
20
+
21
+ def <=>(other)
22
+ date <=> other.date
23
+ end
24
+
25
+ def succ
26
+ self.class.new date.tomorrow, calendar
17
27
  end
18
28
 
19
29
  def number
@@ -1,4 +1,3 @@
1
- require 'active_support/time'
2
1
  require 'active_support/core_ext/array/grouping'
3
2
  require 'active_support/core_ext/string/conversions'
4
3
 
@@ -21,12 +20,20 @@ module Cal
21
20
  @month ||= Month.new self
22
21
  end
23
22
 
23
+ def first_day
24
+ @first_day ||= Day.new date.beginning_of_month.beginning_of_week(:sunday), self
25
+ end
26
+
27
+ def last_day
28
+ @last_day ||= Day.new date.end_of_month.end_of_week(:sunday), self
29
+ end
30
+
24
31
  def days
25
- @days ||= dates.map { |date| Day.new self, date }
32
+ @days ||= first_day..last_day
26
33
  end
27
34
 
28
35
  def weeks
29
- @weeks ||= days.in_groups_of 7
36
+ @weeks ||= days.to_a.in_groups_of 7
30
37
  end
31
38
 
32
39
  def previous
@@ -37,20 +44,5 @@ module Cal
37
44
  self.class.new date.next_month
38
45
  end
39
46
 
40
- private
41
-
42
- # TODO: simplify/improve this
43
- def dates
44
- [].tap do |dates|
45
- day = date.beginning_of_month.beginning_of_week :sunday
46
- last_day = date.end_of_month.end_of_week :sunday
47
-
48
- while day <= last_day
49
- dates << day
50
- day = day.tomorrow
51
- end
52
- end
53
- end
54
-
55
47
  end
56
48
  end
@@ -1,5 +1,5 @@
1
1
  module Cal
2
2
 
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.1"
4
4
 
5
5
  end
@@ -9,6 +9,8 @@ describe Cal::Day do
9
9
  @calendar = OpenStruct.new :current_day => Date.new(2012, 1, 5)
10
10
  end
11
11
 
12
+ it { should be_a(Comparable) }
13
+
12
14
  describe "==" do
13
15
  it "is true with another day with the same calendar and date" do
14
16
  subject.should == described_class.new(@date, @calendar)
@@ -43,4 +45,19 @@ describe Cal::Day do
43
45
  end
44
46
  end
45
47
 
48
+ describe "<=>" do
49
+ it "is the result of comparing the dates" do
50
+ other_date = Date.new 2012, 2, 15
51
+ result = Object.new
52
+ subject.date.stub(:<=>) { |obj| result if obj == other_date }
53
+ (subject <=> described_class.new(other_date, Object.new)).should == result
54
+ end
55
+ end
56
+
57
+ describe "succ" do
58
+ it "is the next day" do
59
+ subject.succ.should == described_class.new(Date.new(2012, 1, 16), @calendar)
60
+ end
61
+ end
62
+
46
63
  end
@@ -51,29 +51,38 @@ describe Cal::MonthlyCalendar do
51
51
  it { subject.month.should == Cal::Month.new(subject) }
52
52
  end
53
53
 
54
- describe "days" do
55
- it "is an array of all the viewable days" do
54
+ describe "first_day" do
55
+ it "is the first viewable day on the calendar" do
56
56
  @date = Date.new 2012, 2, 23
57
+ subject.first_day.should == Cal::Day.new(Date.new(2012, 1, 29), subject)
58
+ end
59
+ end
57
60
 
58
- prev_month_days = %w[01-29 01-30 01-31]
59
- days = %w[02-01 02-02 02-03 02-04 02-05 02-06 02-07 02-08 02-09 02-10 02-11 02-12 02-13 02-14 02-15 02-16 02-17 02-18 02-19 02-20 02-21 02-22 02-23 02-24 02-25 02-26 02-27 02-28 02-29]
60
- next_month_days = %w[03-01 03-02 03-03]
61
+ describe "last_day" do
62
+ it "is the last viewable day on the calendar" do
63
+ @date = Date.new 2012, 2, 23
64
+ subject.last_day.should == Cal::Day.new(Date.new(2012, 3, 3), subject)
65
+ end
66
+ end
61
67
 
62
- subject.days.should == (prev_month_days + days + next_month_days).map do |s|
63
- Cal::Day.new subject, Date.parse("2012-#{s}")
64
- end
68
+ describe "days" do
69
+ it "is a range of all the viewable days" do
70
+ @date = Date.new 2012, 2, 23
71
+ first_day = Cal::Day.new Date.new(2012, 1, 29), subject
72
+ last_day = Cal::Day.new Date.new(2012, 3, 3), subject
73
+ subject.days.should == (first_day..last_day)
65
74
  end
66
75
  end
67
76
 
68
77
  describe "weeks" do
69
- it "is the days in groups of 7" do
78
+ it "an array of the days in groups of 7" do
70
79
  @date = Date.parse "2012-02-23"
71
80
  subject.weeks.should == [
72
- %w[01-29 01-30 01-31 02-01 02-02 02-03 02-04].map { |s| Cal::Day.new subject, Date.parse("2012-#{s}") },
73
- %w[02-05 02-06 02-07 02-08 02-09 02-10 02-11].map { |s| Cal::Day.new subject, Date.parse("2012-#{s}") },
74
- %w[02-12 02-13 02-14 02-15 02-16 02-17 02-18].map { |s| Cal::Day.new subject, Date.parse("2012-#{s}") },
75
- %w[02-19 02-20 02-21 02-22 02-23 02-24 02-25].map { |s| Cal::Day.new subject, Date.parse("2012-#{s}") },
76
- %w[02-26 02-27 02-28 02-29 03-01 03-02 03-03].map { |s| Cal::Day.new subject, Date.parse("2012-#{s}") }
81
+ %w[01-29 01-30 01-31 02-01 02-02 02-03 02-04].map { |s| Cal::Day.new Date.parse("2012-#{s}"), subject },
82
+ %w[02-05 02-06 02-07 02-08 02-09 02-10 02-11].map { |s| Cal::Day.new Date.parse("2012-#{s}"), subject },
83
+ %w[02-12 02-13 02-14 02-15 02-16 02-17 02-18].map { |s| Cal::Day.new Date.parse("2012-#{s}"), subject },
84
+ %w[02-19 02-20 02-21 02-22 02-23 02-24 02-25].map { |s| Cal::Day.new Date.parse("2012-#{s}"), subject },
85
+ %w[02-26 02-27 02-28 02-29 03-01 03-02 03-03].map { |s| Cal::Day.new Date.parse("2012-#{s}"), subject }
77
86
  ]
78
87
  end
79
88
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: