cal 0.3.0 → 0.4.0

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.
data/README.md CHANGED
@@ -20,34 +20,31 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
+ See the Rails app [https://github.com/austinthecoder/cal-app](https://github.com/austinthecoder/cal-app) for an example.
24
+
23
25
  ### Rails example
24
26
 
25
27
  ``` ruby
26
28
  # in the controller
27
- - calendar = Cal::MonthlyCalendar.new params[:date]
29
+ - calendar = Cal::MonthlyCalendar.new((params[:date] || Date.current), :start_week_on => :monday)
28
30
 
29
31
  # in the view
30
32
  %h3
31
33
  = link_to 'Previous month', url_for(:date => calendar.previous.date)
32
34
  |
33
- = calendar.month
35
+ = "#{calendar.month} #{calendar.year}"
34
36
  |
35
37
  = link_to 'Next month', url_for(:date => calendar.next.date)
36
38
 
37
39
  %table
38
40
  %thead
39
41
  %tr
40
- %th Sunday
41
- %th Monday
42
- %th Tuesday
43
- %th Wednesday
44
- %th Thursday
45
- %th Friday
46
- %th Saturday
42
+ - calendar.day_names.each do |name|
43
+ %th= name
47
44
  %tbody
48
45
  - calendar.weeks.each do |week|
49
46
  %tr
50
- - week.days.each do |day|
47
+ - week.each do |day|
51
48
  %td{:class => ('today' if day.today?)}
52
49
  = day.number
53
50
  ```
data/lib/cal.rb CHANGED
@@ -6,5 +6,6 @@ module Cal
6
6
  autoload :MonthlyCalendar, 'cal/monthly_calendar'
7
7
  autoload :Day, 'cal/day'
8
8
  autoload :Month, 'cal/month'
9
+ autoload :Year, 'cal/year'
9
10
 
10
11
  end
@@ -4,13 +4,24 @@ require 'active_support/core_ext/string/conversions'
4
4
  module Cal
5
5
  class MonthlyCalendar
6
6
 
7
+ DAY_NAMES = {
8
+ :sunday => 0,
9
+ :monday => 1,
10
+ :tuesday => 2,
11
+ :wednesday => 3,
12
+ :thursday => 4,
13
+ :friday => 5,
14
+ :saturday => 6
15
+ }
16
+
7
17
  def initialize(dateable, options = {})
8
- options = options.reverse_merge :start_week_on => :sunday
9
18
  @date = dateable.to_date
10
- @options = options
19
+ @start_week_on = options[:start_week_on] || :sunday
20
+ @month = Month.new self
21
+ @year = Year.new self
11
22
  end
12
23
 
13
- attr_reader :date
24
+ attr_reader :date, :month, :year
14
25
 
15
26
  def ==(other)
16
27
  other.is_a?(MonthlyCalendar) &&
@@ -18,16 +29,12 @@ module Cal
18
29
  other.date.month == date.month
19
30
  end
20
31
 
21
- def month
22
- @month ||= Month.new self
23
- end
24
-
25
32
  def first_day
26
- @first_day ||= Day.new date.beginning_of_month.beginning_of_week(@options[:start_week_on]), self
33
+ @first_day ||= Day.new date.beginning_of_month.beginning_of_week(@start_week_on), self
27
34
  end
28
35
 
29
36
  def last_day
30
- @last_day ||= Day.new date.end_of_month.end_of_week(@options[:start_week_on]), self
37
+ @last_day ||= Day.new date.end_of_month.end_of_week(@start_week_on), self
31
38
  end
32
39
 
33
40
  def days
@@ -46,5 +53,9 @@ module Cal
46
53
  self.class.new date.next_month
47
54
  end
48
55
 
56
+ def day_names
57
+ %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday].rotate DAY_NAMES[@start_week_on]
58
+ end
59
+
49
60
  end
50
61
  end
@@ -1,5 +1,5 @@
1
1
  module Cal
2
2
 
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
 
5
5
  end
@@ -0,0 +1,19 @@
1
+ module Cal
2
+ class Year
3
+
4
+ def initialize(calendar)
5
+ @calendar = calendar
6
+ end
7
+
8
+ attr_reader :calendar
9
+
10
+ def ==(other)
11
+ other.is_a?(Year) && other.calendar == calendar
12
+ end
13
+
14
+ def to_s
15
+ calendar.date.strftime "%Y"
16
+ end
17
+
18
+ end
19
+ end
@@ -54,6 +54,10 @@ describe Cal::MonthlyCalendar do
54
54
  it { subject.month.should == Cal::Month.new(subject) }
55
55
  end
56
56
 
57
+ describe "year" do
58
+ it { subject.year.should == Cal::Year.new(subject) }
59
+ end
60
+
57
61
  describe "first_day" do
58
62
  before { @date = Date.new 2012, 2, 23 }
59
63
 
@@ -136,4 +140,24 @@ describe Cal::MonthlyCalendar do
136
140
  end
137
141
  end
138
142
 
143
+ describe "day_names" do
144
+ it "defaults to sunday through saturday" do
145
+ subject.day_names.should == %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
146
+ end
147
+
148
+ [
149
+ %w[Monday Tuesday Wednesday Thursday Friday Saturday Sunday],
150
+ %w[Tuesday Wednesday Thursday Friday Saturday Sunday Monday],
151
+ %w[Wednesday Thursday Friday Saturday Sunday Monday Tuesday],
152
+ %w[Thursday Friday Saturday Sunday Monday Tuesday Wednesday],
153
+ %w[Friday Saturday Sunday Monday Tuesday Wednesday Thursday],
154
+ %w[Saturday Sunday Monday Tuesday Wednesday Thursday Friday]
155
+ ].each do |day_names|
156
+ it "is #{day_names[0]} through #{day_names[6]} when the weekday is set to start on #{day_names[0]}" do
157
+ @options[:start_week_on] = day_names[0].downcase.to_sym
158
+ subject.day_names.should == day_names
159
+ end
160
+ end
161
+ end
162
+
139
163
  end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cal::Year do
4
+
5
+ subject { described_class.new @calendar }
6
+
7
+ before { @calendar = OpenStruct.new }
8
+
9
+ describe "==" do
10
+ it "is true with another year with the same calendar" do
11
+ (subject == described_class.new(@calendar)).should be_true
12
+ end
13
+
14
+ it "is false with another year with a different calendar" do
15
+ (subject == described_class.new(Object.new)).should be_false
16
+ end
17
+
18
+ it "is false with a non year" do
19
+ (subject == Object.new).should be_false
20
+ end
21
+ end
22
+
23
+ describe "calendar" do
24
+ it "is the given calendar" do
25
+ subject.calendar.should == @calendar
26
+ end
27
+ end
28
+
29
+ describe "to_s" do
30
+ before { @calendar.date = Date.new 1983 }
31
+ it { subject.to_s.should == '1983' }
32
+ end
33
+
34
+ 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.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-07 00:00:00.000000000 Z
12
+ date: 2012-06-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -62,9 +62,11 @@ files:
62
62
  - lib/cal/month.rb
63
63
  - lib/cal/monthly_calendar.rb
64
64
  - lib/cal/version.rb
65
+ - lib/cal/year.rb
65
66
  - spec/cal/day_spec.rb
66
67
  - spec/cal/month_spec.rb
67
68
  - spec/cal/monthly_calendar_spec.rb
69
+ - spec/cal/year_spec.rb
68
70
  - spec/spec_helper.rb
69
71
  homepage: ''
70
72
  licenses: []
@@ -94,4 +96,5 @@ test_files:
94
96
  - spec/cal/day_spec.rb
95
97
  - spec/cal/month_spec.rb
96
98
  - spec/cal/monthly_calendar_spec.rb
99
+ - spec/cal/year_spec.rb
97
100
  - spec/spec_helper.rb