semantic_calendar 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -0,0 +1,138 @@
1
+ module SemanticCalendar
2
+ module Monthly
3
+ class TableCalendarBuilder
4
+
5
+ attr_accessor :t
6
+
7
+ def initialize(template, date, options = {}, &proc)
8
+ @t, @date = template, date
9
+
10
+ @year = date.year
11
+ @month = date.month
12
+ @today = (Time.respond_to?(:zone) && !(zone = Time.zone).nil? ? zone.now.to_date : Date.today)
13
+ @month_days = []
14
+
15
+ @first = Date.civil(@year, @month, 1)
16
+ @last = Date.civil(@year, @month, -1)
17
+
18
+ @first_weekday = first_day_of_week(options[:first_day_of_week] || 0)
19
+ @last_weekday = last_day_of_week(options[:first_day_of_week] || 0)
20
+
21
+ @day_names = Date::DAYNAMES.dup
22
+ @first_weekday.times { @day_names.push(@day_names.shift) }
23
+
24
+ render(options.delete(:html), &proc)
25
+ end
26
+
27
+ def render(options, &proc)
28
+ buffer = t.capture(self, &proc)
29
+ t.concat(t.content_tag(:table, buffer, options))
30
+ end
31
+
32
+ def head(options = {}, &proc)
33
+ options[:abbrev] ||= (0..2)
34
+
35
+ options[:html] ||= {}
36
+
37
+ buffer = ''
38
+ if block_given?
39
+ value = t.capture(self, &proc).gsub('{month}', Date::MONTHNAMES[@month])
40
+ buffer = t.content_tag(:tr, t.content_tag(:th, value, {:colspan => 7}))
41
+ end
42
+
43
+ day_titles = @day_names.map do |d|
44
+ d[options[:abbrev]] == d ? d[options[:abbrev]] : t.content_tag(:abbr, d[options[:abbrev]], :title => d)
45
+ end
46
+
47
+ head = [
48
+ buffer,
49
+ t.content_tag(:tr, day_titles.map { |d| t.content_tag(:th, d) }.join(''), {}, false)
50
+ ].join('')
51
+
52
+ t.content_tag(:thead, head, options.delete(:html), false)
53
+ end
54
+
55
+ def days(options = {}, &proc)
56
+ options[:html] ||= {}
57
+
58
+ collect_days(&proc)
59
+
60
+ buffer = ''
61
+ month_days = ''
62
+ @month_days.each_with_index do |d, index|
63
+ month_days << d
64
+ if (index+1) % 7 == 0
65
+ buffer << t.content_tag(:tr, month_days, {}, false)
66
+ month_days = ''
67
+ end
68
+ end
69
+
70
+ t.content_tag(:tbody, buffer, options.delete(:html), false)
71
+ end
72
+
73
+ def collect_days(&proc)
74
+ collect_previous_month_days(&proc)
75
+ collect_month_days(&proc)
76
+ collect_next_month_days(&proc)
77
+ end
78
+
79
+ def collect_month_days(&proc)
80
+ @first.upto(@last) do |d|
81
+ value, options = t.capture(d, &proc) if block_given?
82
+ @month_days << day(d, value, options)
83
+ end
84
+ end
85
+
86
+ def collect_previous_month_days(&proc)
87
+ beginning_of_week(@first, @first_weekday).upto(@first - 1) do |d|
88
+ value, options = t.capture(d, &proc) if block_given?
89
+ @month_days << day(d, value, options, 'previous-month')
90
+ end unless @first.wday == @first_weekday
91
+ end
92
+
93
+ def collect_next_month_days(&proc)
94
+ (@last + 1).upto(beginning_of_week(@last + 7, @first_weekday) - 1) do |d|
95
+ value, options = t.capture(d, &proc) if block_given?
96
+ @month_days << day(d, value, options, 'next-month')
97
+ end unless @last.wday == @last_weekday
98
+ end
99
+
100
+ def day(d, value, options, extra_class = nil)
101
+ options ||= {}
102
+ options[:class] ||= ''
103
+
104
+ value = t.content_tag(:span, d.mday, {:class => 'month-day'}) + value || ''
105
+
106
+ options[:class] = t.add_class(options[:class], weekend?(d) ? 'weekend' : 'weekday')
107
+ options[:class] = t.add_class(options[:class], 'today') if (d == @today)
108
+ options[:class] = t.add_class(options[:class], extra_class) unless extra_class.blank?
109
+
110
+ t.content_tag(:td, value, options, false)
111
+ end
112
+
113
+ private
114
+
115
+ def first_day_of_week(day)
116
+ day
117
+ end
118
+
119
+ def last_day_of_week(day)
120
+ day > 0 ? day - 1 : 6
121
+ end
122
+
123
+ def days_between(first, second)
124
+ first > second ? second + (7 - first) : second - first
125
+ end
126
+
127
+ def beginning_of_week(date, start = 1)
128
+ days_to_beg = days_between(start, date.wday)
129
+ date - days_to_beg
130
+ end
131
+
132
+ def weekend?(date)
133
+ [0, 6].include?(date.wday)
134
+ end
135
+
136
+ end
137
+ end
138
+ end
@@ -1,12 +1,11 @@
1
1
  # coding: utf-8
2
- require 'builders/table_builder'
3
- require 'builders/div_builder'
2
+ require 'builders/monthly/table_builder'
4
3
 
5
4
  module SemanticCalendar #:nodoc:
6
5
 
7
6
  module SemanticCalendarHelper
8
7
 
9
- @@builder = ::SemanticCalendar::TableCalendarBuilder
8
+ @@builder = ::SemanticCalendar::Monthly::TableCalendarBuilder
10
9
  mattr_accessor :builder
11
10
 
12
11
  def semantic_calendar(date, options = {}, &proc)
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{semantic_calendar}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jeremy Jackson"]
12
- s.date = %q{2010-10-25}
12
+ s.date = %q{2010-10-28}
13
13
  s.description = %q{Semantic calendar; a semantic way to build beautifully simple calendars in Rails}
14
14
  s.email = %q{jejacks0n@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -23,8 +23,7 @@ Gem::Specification.new do |s|
23
23
  "README.textile",
24
24
  "Rakefile",
25
25
  "VERSION",
26
- "lib/builders/div_builder.rb",
27
- "lib/builders/table_builder.rb",
26
+ "lib/builders/monthly/table_builder.rb",
28
27
  "lib/semantic_calendar.rb",
29
28
  "semantic_calendar.gemspec",
30
29
  "spec/semantic_calendar_spec.rb",
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: semantic_calendar
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeremy Jackson
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-25 00:00:00 -06:00
18
+ date: 2010-10-28 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -50,8 +50,7 @@ files:
50
50
  - README.textile
51
51
  - Rakefile
52
52
  - VERSION
53
- - lib/builders/div_builder.rb
54
- - lib/builders/table_builder.rb
53
+ - lib/builders/monthly/table_builder.rb
55
54
  - lib/semantic_calendar.rb
56
55
  - semantic_calendar.gemspec
57
56
  - spec/semantic_calendar_spec.rb
File without changes
@@ -1,132 +0,0 @@
1
- module SemanticCalendar
2
- class TableCalendarBuilder
3
-
4
- attr_accessor :t
5
-
6
- def initialize(template, date, options = {}, &proc)
7
- @t, @date = template, date
8
-
9
- @year = date.year
10
- @month = date.month
11
- @today = (Time.respond_to?(:zone) && !(zone = Time.zone).nil? ? zone.now.to_date : Date.today)
12
- @month_days = []
13
-
14
- @first = Date.civil(@year, @month, 1)
15
- @last = Date.civil(@year, @month, -1)
16
-
17
- @first_weekday = first_day_of_week(options[:first_day_of_week] || 0)
18
- @last_weekday = last_day_of_week(options[:first_day_of_week] || 0)
19
-
20
- @day_names = Date::DAYNAMES.dup
21
- @first_weekday.times { @day_names.push(@day_names.shift) }
22
-
23
- render(options.delete(:html), &proc)
24
- end
25
-
26
- def render(options, &proc)
27
- buffer = t.capture(self, &proc)
28
- t.concat(t.content_tag(:table, buffer, options))
29
- end
30
-
31
- def head(options = {}, &proc)
32
- options[:abbrev] ||= (0..2)
33
-
34
- options[:html] ||= {}
35
-
36
- buffer = ''
37
- if block_given?
38
- value = t.capture(self, &proc).gsub('{month}', Date::MONTHNAMES[@month])
39
- buffer = t.content_tag(:tr, t.content_tag(:th, value, {:colspan => 7}))
40
- end
41
-
42
- day_titles = @day_names.map do |d|
43
- d[options[:abbrev]] == d ? d[options[:abbrev]] : t.content_tag(:abbr, d[options[:abbrev]], :title => d)
44
- end
45
-
46
- head = [
47
- buffer,
48
- t.content_tag(:tr, day_titles.map { |d| t.content_tag(:th, d) }.join(''), {}, false)
49
- ].join('')
50
-
51
- t.content_tag(:thead, head, options.delete(:html), false)
52
- end
53
-
54
- def days(options = {}, &proc)
55
- options[:html] ||= {}
56
-
57
- collect_days(&proc)
58
-
59
- buffer = ''
60
- month_days = ''
61
- @month_days.each_with_index do |d, index|
62
- month_days << d
63
- if (index+1) % 7 == 0
64
- buffer << t.content_tag(:tr, month_days, {}, false)
65
- month_days = ''
66
- end
67
- end
68
-
69
- t.content_tag(:tbody, buffer, options.delete(:html), false)
70
- end
71
-
72
- def collect_days(&proc)
73
- collect_previous_month_days
74
- collect_month_days(&proc)
75
- collect_next_month_days
76
- end
77
-
78
- def collect_month_days(&proc)
79
- @first.upto(@last) do |d|
80
- value, options = t.capture(d, &proc) if block_given?
81
- @month_days << day(d, value, options)
82
- end
83
- end
84
-
85
- def collect_previous_month_days
86
- beginning_of_week(@first, @first_weekday).upto(@first - 1) do |d|
87
- @month_days << day(d, nil, :class => 'previous-month')
88
- end unless @first.wday == @first_weekday
89
- end
90
-
91
- def collect_next_month_days
92
- (@last + 1).upto(beginning_of_week(@last + 7, @first_weekday) - 1) do |d|
93
- @month_days << day(d, nil, :class => 'next-month')
94
- end unless @last.wday == @last_weekday
95
- end
96
-
97
- def day(d, value, options)
98
- value = value..blank? d.mday : value
99
- options ||= {}
100
- options[:class] ||= ''
101
-
102
- options[:class] = t.add_class(options[:class], weekend?(d) ? 'weekend' : 'weekday')
103
- options[:class] = t.add_class(options[:class], 'today') if (d == @today)
104
-
105
- t.content_tag(:td, value, options, false)
106
- end
107
-
108
- private
109
-
110
- def first_day_of_week(day)
111
- day
112
- end
113
-
114
- def last_day_of_week(day)
115
- day > 0 ? day - 1 : 6
116
- end
117
-
118
- def days_between(first, second)
119
- first > second ? second + (7 - first) : second - first
120
- end
121
-
122
- def beginning_of_week(date, start = 1)
123
- days_to_beg = days_between(start, date.wday)
124
- date - days_to_beg
125
- end
126
-
127
- def weekend?(date)
128
- [0, 6].include?(date.wday)
129
- end
130
-
131
- end
132
- end