farski-calendar_for 0.0.2 → 0.0.3
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/VERSION.yml +1 -1
- data/lib/calendar.rb +34 -0
- data/lib/calendar_for.rb +5 -1
- data/lib/helper.rb +102 -0
- metadata +8 -5
data/VERSION.yml
CHANGED
data/lib/calendar.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module CalendarFor
|
2
|
+
class Calendar
|
3
|
+
def initialize(aTime = Time.now)
|
4
|
+
@time = aTime
|
5
|
+
@weeks_start_on = 0 # 0 = sunday
|
6
|
+
@weekday_names = %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
|
7
|
+
end
|
8
|
+
|
9
|
+
def month
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
def week
|
14
|
+
week = []
|
15
|
+
7.times do |i|
|
16
|
+
week << @time.midnight.monday.advance(:days => (week_start-1))
|
17
|
+
end
|
18
|
+
week
|
19
|
+
end
|
20
|
+
|
21
|
+
def day
|
22
|
+
[@time.mday]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module CalendarHelper
|
27
|
+
def calendar_for(date_or_time_object_or_parameters = Time.now, *args, &block)
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
c=CalendarFor::Calendar.new()
|
34
|
+
puts c.week
|
data/lib/calendar_for.rb
CHANGED
@@ -86,7 +86,11 @@ module CalendarHelper
|
|
86
86
|
elsif current_date.month != timeframe_starts_on.month
|
87
87
|
html_class = options[:html_classes][:out_of_timeframe]
|
88
88
|
end
|
89
|
-
|
89
|
+
if options[:explicit_cells]
|
90
|
+
table_cells << yield(current_date)
|
91
|
+
else
|
92
|
+
table_cells << content_tag(:td, yield(current_date), :class => html_class)
|
93
|
+
end
|
90
94
|
end
|
91
95
|
table_rows << content_tag(:tr, table_cells.join)
|
92
96
|
end
|
data/lib/helper.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
module CalendarHelper
|
2
|
+
|
3
|
+
def calendar_for(date_or_time_object_or_parameters = Time.now, *args, &block)
|
4
|
+
raise ArgumentError, "Missing block" unless block_given?
|
5
|
+
|
6
|
+
options = default_missing_options(args.extract_options!)
|
7
|
+
options[:week_starts_on] = numeralize_week_starts_on_option(options)
|
8
|
+
options[:scope_starts_on] = chronolize_scope_starts_on_option(date_or_time_object_or_parameters)
|
9
|
+
options[:view_parameters] = bound_view_in_scope(options)
|
10
|
+
options[:scope_starts_on] = adjust_for_long_term_scope(options)
|
11
|
+
|
12
|
+
html = build_table_view(options, block)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def default_missing_options(options)
|
18
|
+
options[:week_starts_on] ||= 0
|
19
|
+
options[:scope] ||= :month
|
20
|
+
options[:weekday_names] ||= ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
|
21
|
+
options[:surpress_weekday_headers] ||= false
|
22
|
+
|
23
|
+
return options
|
24
|
+
end
|
25
|
+
|
26
|
+
def numeralize_week_starts_on_option(options)
|
27
|
+
case options[:week_starts_on]
|
28
|
+
when Integer then options[:week_starts_on]
|
29
|
+
when String, Symbol then options[:weekday_names].index(options[:week_starts_on].to_s)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def chronolize_scope_starts_on_option(time)
|
34
|
+
case time
|
35
|
+
when Date, Time then time
|
36
|
+
when String then Chronic.parse(time)
|
37
|
+
when Hash then Time.local(time[:year], time[:month], time[:day])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def bound_view_in_scope(options)
|
42
|
+
view_params = Hash.new
|
43
|
+
week_start_adjuster = (options[:week_starts_on] - 1)
|
44
|
+
|
45
|
+
case options[:scope]
|
46
|
+
when :day
|
47
|
+
view_params[:days] = 1
|
48
|
+
view_params[:weeks] = 1
|
49
|
+
view_params[:months] = 1
|
50
|
+
view_params[:starts_on] = options[:scope_starts_on].midnight
|
51
|
+
when :week
|
52
|
+
view_params[:weeks] = 1
|
53
|
+
view_params[:months] = 1
|
54
|
+
view_params[:starts_on] = options[:scope_starts_on].monday.midnight.advance(:days => week_start_adjuster)
|
55
|
+
when :quarter
|
56
|
+
view_params[:months] = 4
|
57
|
+
view_params[:starts_on] = options[:scope_starts_on].beginning_of_quarter.monday.midnight.advance(:days => week_start_adjuster)
|
58
|
+
when :year
|
59
|
+
view_params[:months] = 12
|
60
|
+
view_params[:starts_on] = options[:scope_starts_on].beginning_of_year.monday.midnight.advance(:days => week_start_adjuster)
|
61
|
+
when Integer
|
62
|
+
view_params[:months] = options[:scope]
|
63
|
+
view_params[:starts_on] = options[:scope_starts_on].beginning_of_month.monday.midnight.advance(:days => week_start_adjuster)
|
64
|
+
end
|
65
|
+
|
66
|
+
return view_params
|
67
|
+
end
|
68
|
+
|
69
|
+
def adjust_for_long_term_scope(options)
|
70
|
+
case options[:scope]
|
71
|
+
when :quarter then options[:scope_starts_on].beginning_of_quarter
|
72
|
+
when :year then options[:scope_starts_on].beginning_of_year
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def build_table_view(options, block)
|
77
|
+
table = Markup::Element.new(:table)
|
78
|
+
|
79
|
+
headers = build_table_headers(options)
|
80
|
+
table.push(headers) unless options[:surpress_weekday_headers] == true
|
81
|
+
|
82
|
+
rows = build_table_rows(options)
|
83
|
+
table.push(rows)
|
84
|
+
end
|
85
|
+
|
86
|
+
def build_months(options)
|
87
|
+
options[:view_parameters][:months].times do |m|
|
88
|
+
buid
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def build_table_headers(options)
|
93
|
+
tr = Markup::Element.new(:table)
|
94
|
+
options[:weekday_names].each { |name| Markup::Element.new(:th) { name }.inject_into(tr) }
|
95
|
+
end
|
96
|
+
|
97
|
+
def build_table_rows(options)
|
98
|
+
options[:view_parameters][:months].times do |m|
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: farski-calendar_for
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Kalafarski
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-02-12 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -24,14 +24,17 @@ extra_rdoc_files: []
|
|
24
24
|
files:
|
25
25
|
- README.rdoc
|
26
26
|
- VERSION.yml
|
27
|
+
- lib/calendar.rb
|
27
28
|
- lib/calendar_for.rb
|
29
|
+
- lib/helper.rb
|
28
30
|
- test/helper_test.rb
|
29
31
|
- test/test_helper.rb
|
30
|
-
has_rdoc:
|
32
|
+
has_rdoc: true
|
31
33
|
homepage: http://github.com/farski/calendar_for
|
32
34
|
post_install_message:
|
33
|
-
rdoc_options:
|
34
|
-
|
35
|
+
rdoc_options:
|
36
|
+
- --inline-source
|
37
|
+
- --charset=UTF-8
|
35
38
|
require_paths:
|
36
39
|
- lib
|
37
40
|
required_ruby_version: !ruby/object:Gem::Requirement
|