html_skeleton 0.2.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,38 +1,52 @@
1
- HTML Calendar
2
- =============
1
+ HtmlSkeleton
2
+ ============
3
+
4
+ HtmlSkeleton provides the frame for a calendar or a table,
5
+ i.e. no loops are required to build up the HTML structure.
6
+ Filling the skeleton is done via parameters and, in particular,
7
+ with procs (Proc.new, lambda).
8
+ See below Default Options.
9
+
10
+ Install
11
+ -------
12
+
13
+ gem 'html_skeleton'
14
+
15
+ rake # for testing
16
+
17
+
18
+ Calendar
19
+ ========
3
20
 
4
21
  A simple helper for creating an HTML calendar.
5
22
  The "calendar" method will be available to your view templates.
6
23
 
7
- Blocks (Proc.new) may be supplied by the user to generate
8
- particular HTML-code for a day, a month or a year.
9
- As an example (see below) clicking a day triggers an action.
10
-
24
+ Procs may be supplied to generate
25
+ particular HTML-code for a day or the year.
26
+ In the example below clicking a day triggers an action.
11
27
 
12
28
  Examples
13
29
  --------
30
+ HtmlSkeleton.new.calendar # calendar for current year
31
+ HtmlSkeleton.new.calendar :year = 2012 # calendar for year 2012
32
+ HtmlSkeleton.new.calendar :year = 2012, :month => 8 # calendar for August 2012
14
33
 
15
- calendar # calendar for current year
16
- calendar :year = 2012 # calendar for year 2012
17
- calendar :year = 2012, :month => 8 # calendar for August 2012
18
-
19
- calendar {|date|
20
- link ="/#{controller_name}/toggle/#{@row.id}?date=#{date}"
21
- style = @row.holidays.include?(date.to_s) ?
34
+ HtmlSkeleton.new.calendar {|date|
35
+ link ="/#{controller_name}/toggle/#{@resource.id}?date=#{date}"
36
+ style = @resource.holidays.include?(date.to_s) ?
22
37
  'font-weight:bold; color:red' : ''
23
38
  %Q{ <a style="#{style}" href="#{link}"> #{date.day.to_s} </a>}
24
39
  }
25
40
 
26
-
27
41
  Default Options
28
42
  ---------------
29
- :year => DateTime.now.year,
30
- :calendar_class => 'calendar',
43
+ :year => DateTime.now.year,
44
+ :title => DateTime.now.year,
45
+ :calendar_class => 'skeleton',
31
46
  :day_names => Date::DAYNAMES.dup,
47
+ :month_names => Date::MONTHNAMES,
32
48
  :abbrev => (0..1),
33
- :day_block => block || Proc.new {|d| d.day.to_s},
34
- :month_block => Proc.new {|d| d.strftime('%B')},
35
- :year_block => Proc.new {|d| d.to_s},
49
+ :cell_proc => block || lambda {|d| d.day.to_s},
36
50
  :first_day_of_week => 1
37
51
 
38
52
 
@@ -40,3 +54,58 @@ Inspired by calendar_helper:
40
54
 
41
55
  * Jeremy Voorhis -- http://jvoorhis.com
42
56
  * Geoffrey Grosenbach -- http://nubyonrails.com
57
+
58
+
59
+ Table
60
+ =====
61
+
62
+ A simple helper for creating an HTML table.
63
+
64
+ Table only takes care of the HTML tags and expects lambdas/strings to
65
+ be supplied by the user.
66
+
67
+ Examples
68
+ --------
69
+ rows = %w{a bb ccc}
70
+ cols = %w{1 22}
71
+ HtmlSkeleton.new.table(rows, cols) {|row, col|
72
+ col == '1' ? '<td>bingo</td>' : '<td></td>'
73
+ }
74
+
75
+ HtmlSkeleton.new.table(@users, %w{email address},
76
+ :th_attribute => lambda { |col| col.name },
77
+ :legend => 'Users') { |row, col|
78
+ "<td>#{ row.send(col) }</td>"
79
+ }
80
+
81
+ stripes = %w{odd even}
82
+ proc = lambda{ |row| k = stripes.shift; stripes << k; %Q{class="#{k}"} }
83
+ HtmlSkeleton.new.table(@users, %w{email address},
84
+ :tr_attribute => proc,
85
+ :legend => 'Users') { |row, col|
86
+ "<td>#{ row.send(col) }</td>"
87
+ }
88
+
89
+ Default Options
90
+ ---------------
91
+ :legend => nil,
92
+ :col_legend => lambda(&:to_s),
93
+ :row_legend => lambda(&:id),
94
+ :th_attribute => lambda { |col| nil },
95
+ :tr_attribute => lambda { |row| nil },
96
+ :table_class => 'skeleton',
97
+ :cell_proc => block || lambda {|row, col| "<td>#{row} #{col}</td>"}
98
+
99
+
100
+ Curious?
101
+ --------
102
+
103
+ github.com/cthulhu666/easy_table
104
+ github.com/giniedp/fancygrid
105
+ github.com/hunterae/table-for
106
+ github.com/jgdavey/tabletastic
107
+ github.com/lunich/table_for
108
+ github.com/watu/table_builder
109
+ ruby-toolbox.com/projects/tableasy
110
+
111
+ Copyright (c) 2012 [Dittmar Krall], released under the MIT license.
data/lib/html_skeleton.rb CHANGED
@@ -1,18 +1,10 @@
1
1
  require 'date'
2
+ require 'html_skeleton_calendar'
3
+ require 'html_skeleton_table'
2
4
 
3
5
  class HtmlSkeleton
4
6
  attr_reader :options
5
7
 
6
- def table(rows, cols, options = {}, &block)
7
- set_table_options(options, &block)
8
- <<-EOS
9
- <table class="#{@options[:table_class]}">
10
- #{table_header(cols)}
11
- #{table_body(rows, cols)}
12
- </table>
13
- EOS
14
- end
15
-
16
8
  def calendar(options = {}, &block)
17
9
  set_calendar_options(options, &block)
18
10
  frame = @options[:month] ? 'div' : 'table'
@@ -21,51 +13,17 @@ class HtmlSkeleton
21
13
  a_year(@options[:year])
22
14
  %Q{<#{frame} class="#{@options[:calendar_class]}"> #{body} </#{frame}>}
23
15
  end
24
- end
25
-
26
16
 
27
- # table methods ######################################################
28
- class HtmlSkeleton
29
- protected
30
- def defaultProc
31
- Proc.new(&:to_s)
32
- end
33
-
34
- def set_table_options(options, &block)
35
- @options = {
36
- :title => '',
37
- :table_class => 'skeleton',
38
- :legend_proc => defaultProc,
39
- :header_proc => defaultProc,
40
- :cell_proc => block || Proc.new {|row, col| "<td>#{row} #{col}</td>"},
41
- }.merge options
42
- end
43
-
44
- def table_header(cols)
45
- proc = @options[:header_proc]
46
- col_header = cols.collect { |col| "<th>#{proc.call(col)}</th>" }.join
47
- %Q{<thead><th class="legend">#{@options[:title]}</th>#{col_header}</thead>}
48
- end
49
-
50
- def table_body(rows, cols)
51
- legend_proc = @options[:legend_proc]
52
- rows.collect { |row|
53
- cells = table_row(row, cols)
54
- %Q{<tr><td class="legend">#{legend_proc.call(row)}</td>#{cells}</tr>}
55
- }.join("\n")
56
- end
57
-
58
- def table_row(row, cols)
59
- cell_proc = @options[:cell_proc]
60
- cols.collect { |col| cell_proc.call(row, col) }.join('')
17
+ def table(rows, cols, options = {}, &block)
18
+ set_table_options(options, &block)
19
+ <<-EOS
20
+ <table class="#{@options[:table_class]}">
21
+ #{table_header(cols)}
22
+ #{table_body(rows, cols)}
23
+ </table>
24
+ EOS
61
25
  end
62
26
 
63
- end
64
-
65
-
66
- # calendar methods ###################################################
67
- class HtmlSkeleton
68
- attr_reader :day_header
69
27
 
70
28
  protected
71
29
  def set_calendar_options(options, &block)
@@ -76,7 +34,7 @@ class HtmlSkeleton
76
34
  :calendar_class => 'skeleton',
77
35
  :month_names => Date::MONTHNAMES,
78
36
  :abbrev => (0..1),
79
- :cell_proc => block || Proc.new {|d| d.day.to_s},
37
+ :cell_proc => block || lambda {|d| d.day.to_s},
80
38
  :first_day_of_week => 1
81
39
  }.merge options
82
40
 
@@ -90,63 +48,17 @@ class HtmlSkeleton
90
48
  }.join('')
91
49
  end
92
50
 
93
- def a_year(year)
94
- rows, cols = 3, 4
95
- body = (0..rows - 1).collect {|y|
96
- str = (1..cols).collect {|x| "<td>#{a_month(year, y * cols + x)}</td>"}
97
- "<tr>#{str.join('')}</tr>"
98
- }
99
- <<-EOS
100
- <thead><th colspan="2">#{@options[:title]}</th></thead>
101
- #{body.join('')}
102
- EOS
103
- end
104
-
105
- def a_month(year, month)
106
- title = @options[:month_names][month]
107
- <<-EOS
108
- <table class="month">
109
- <tr class="monthName"><th colspan="7">#{title}</th></tr>
110
- <tr class="dayName">#{@day_header}</tr>
111
- #{days_of_month(year, month)}
112
- </table>
113
- EOS
114
- end
115
-
116
- def days_of_month(year, month)
117
- first_weekday = @options[:first_day_of_week]
118
- last_weekday = first_weekday > 0 ? first_weekday - 1 : 6
119
- cell_proc = @options[:cell_proc]
120
- today = (Time.respond_to?(:zone) &&
121
- !(zone = Time.zone).nil? ? zone.now.to_date : Date.today)
122
-
123
- first = Date.civil(year, month, 1)
124
- last = Date.civil(year, month, -1)
125
-
126
- cal = '<tr>'
127
- cal << '<td></td>' * days_between(first_weekday, first.wday)
128
- first.upto(last) {|cur|
129
- cal << a_day(cur, today, cell_proc)
130
- cal << '</tr> <tr>' if cur.wday == last_weekday
131
- }
132
- cal << '<td></td>' * days_between((last + 1).wday, first_weekday + 7)
133
- cal << '</tr>'
134
- end
135
-
136
- def a_day(date, today, block)
137
- attrs = 'day'
138
- attrs += ' weekendDay' if weekend?(date)
139
- attrs += ' today' if date == today
140
- "<td class=\"#{attrs}\">#{block.call(date)}</td>"
141
- # "<td class=\"#{attrs}\">##</td>"
142
- end
143
-
144
- def weekend?(date)
145
- [0, 6].include?(date.wday)
146
- end
51
+ def set_table_options(options, &block)
52
+ @options = {
53
+ :legend => nil,
54
+ :col_legend => lambda(&:to_s),
55
+ :row_legend => lambda(&:id),
56
+ :th_attribute => lambda { |col| nil },
57
+ :tr_attribute => lambda { |row| nil },
147
58
 
148
- def days_between(first, second)
149
- first > second ? second + (7 - first) : second - first
59
+ :table_class => 'skeleton',
60
+ :cell_proc => block || lambda {|row, col| "<td>#{row} #{col}</td>"},
61
+ }.merge options
150
62
  end
151
63
 
152
64
  end
@@ -0,0 +1,67 @@
1
+ require 'date'
2
+
3
+ # calendar methods ###################################################
4
+ class HtmlSkeleton
5
+ attr_reader :day_header
6
+
7
+ protected
8
+ def a_year(year)
9
+ rows, cols = 3, 4
10
+ body = (0..rows - 1).collect {|y|
11
+ str = (1..cols).collect {|x| "<td>#{a_month(year, y * cols + x)}</td>"}
12
+ "<tr>#{str.join('')}</tr>"
13
+ }
14
+ <<-EOS
15
+ <thead><th colspan="2">#{@options[:title]}</th></thead>
16
+ #{body.join('')}
17
+ EOS
18
+ end
19
+
20
+ def a_month(year, month)
21
+ title = @options[:month_names][month]
22
+ <<-EOS
23
+ <table class="month">
24
+ <tr class="monthName"><th colspan="7">#{title}</th></tr>
25
+ <tr class="dayName">#{@day_header}</tr>
26
+ #{days_of_month(year, month)}
27
+ </table>
28
+ EOS
29
+ end
30
+
31
+ def days_of_month(year, month)
32
+ first_weekday = @options[:first_day_of_week]
33
+ last_weekday = first_weekday > 0 ? first_weekday - 1 : 6
34
+ cell_proc = @options[:cell_proc]
35
+ today = (Time.respond_to?(:zone) &&
36
+ !(zone = Time.zone).nil? ? zone.now.to_date : Date.today)
37
+
38
+ first = Date.civil(year, month, 1)
39
+ last = Date.civil(year, month, -1)
40
+
41
+ cal = '<tr>'
42
+ cal << '<td></td>' * days_between(first_weekday, first.wday)
43
+ first.upto(last) {|cur|
44
+ cal << a_day(cur, today, cell_proc)
45
+ cal << '</tr> <tr>' if cur.wday == last_weekday
46
+ }
47
+ cal << '<td></td>' * days_between((last + 1).wday, first_weekday + 7)
48
+ cal << '</tr>'
49
+ end
50
+
51
+ def a_day(date, today, block)
52
+ attrs = 'day'
53
+ attrs += ' weekendDay' if weekend?(date)
54
+ attrs += ' today' if date == today
55
+ "<td class=\"#{attrs}\">#{block.call(date)}</td>"
56
+ # "<td class=\"#{attrs}\">##</td>"
57
+ end
58
+
59
+ def weekend?(date)
60
+ [0, 6].include?(date.wday)
61
+ end
62
+
63
+ def days_between(first, second)
64
+ first > second ? second + (7 - first) : second - first
65
+ end
66
+
67
+ end
@@ -0,0 +1,35 @@
1
+ require 'date'
2
+
3
+ # table methods ######################################################
4
+ class HtmlSkeleton
5
+ protected
6
+ def table_header(cols)
7
+ legend = @options[:legend]
8
+ th_attribute = @options[:th_attribute]
9
+ return '' unless legend
10
+
11
+ proc = @options[:col_legend]
12
+ col_header = cols.collect { |col|
13
+ "<th #{th_attribute.call(col)}>#{proc.call(col)}</th>"
14
+ }.join
15
+ %Q{<thead><th class="legend">#{legend}</th>#{col_header}</thead>}
16
+ end
17
+
18
+ def table_body(rows, cols)
19
+ legend = @options[:legend]
20
+ row_legend = @options[:row_legend]
21
+ tr_attribute = @options[:tr_attribute]
22
+ rows.collect { |row|
23
+ rlegend = legend ? %Q{<td class="legend">#{row_legend.call(row)}</td>}
24
+ : ''
25
+ cells = table_row(row, cols)
26
+ %Q{<tr #{tr_attribute.call(row)}>#{rlegend}#{cells}</tr>}
27
+ }.join("\n")
28
+ end
29
+
30
+ def table_row(row, cols)
31
+ cell_proc = @options[:cell_proc]
32
+ cols.collect { |col| cell_proc.call(row, col) }.join('')
33
+ end
34
+
35
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_skeleton
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.4
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-08-23 00:00:00.000000000 Z
12
+ date: 2012-09-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -36,9 +36,11 @@ extensions: []
36
36
  extra_rdoc_files: []
37
37
  files:
38
38
  - lib/html_skeleton.rb
39
+ - lib/html_skeleton_calendar.rb
40
+ - lib/html_skeleton_table.rb
39
41
  - README.md
40
42
  - MIT-LICENSE
41
- homepage:
43
+ homepage: http://matique.de
42
44
  licenses: []
43
45
  post_install_message:
44
46
  rdoc_options: []