Guest User

Untitled

a guest
Feb 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. # Returns an HTML calendar. In its simplest form, this method generates a plain
  2. # calendar (which can then be customized using CSS) for a given month and year.
  3. # However, this may be customized in a variety of ways -- changing the default CSS
  4. # classes, generating the individual day entries yourself, and so on.
  5. #
  6. # The following options are required:
  7. # :year # The year number to show the calendar for.
  8. # :month # The month number to show the calendar for.
  9. #
  10. # The following are optional, available for customizing the default behaviour:
  11. # :table_class => "calendar" # The class for the <table> tag.
  12. # :month_name_class => "monthName" # The class for the name of the month, at the top of the table.
  13. # :other_month_class => "otherMonthClass" # Not implemented yet.
  14. # :day_name_class => "dayName" # The class is for the names of the weekdays, at the top.
  15. # :day_class => "day" # The class for the individual day number cells.
  16. # This may or may not be used if you specify a block (see below).
  17. # :abbrev => (0..2) # This option specifies how the day names should be abbreviated.
  18. # Use (0..2) for the first three letters, (0..0) for the first, and
  19. # (0..-1) for the entire name.
  20. #
  21. # For more customization, you can pass a code block to this method, that will get one argument, a Date object,
  22. # and return a values for the individual table cells. The block can return an array, [cell_text, cell_attrs],
  23. # cell_text being the text that is displayed and cell_attrs a hash containing the attributes for the <td> tag
  24. # (this can be used to change the <td>'s class for customization with CSS).
  25. # This block can also return the cell_text only, in which case the <td>'s class defaults to the value given in
  26. # +:day_class+. If the block returns nil, the default options are used.
  27. #
  28. # Example usage:
  29. # calendar(:year => 2005, :month => 6) # This generates the simplest possible calendar.
  30. # calendar({:year => 2005, :month => 6, :table_class => "calendar_helper"}) # This generates a calendar, as
  31. # # before, but the <table>'s class
  32. # # is set to "calendar_helper".
  33. # calendar(:year => 2005, :month => 6, :abbrev => (0..-1)) # This generates a simple calendar but shows the
  34. # # entire day name ("Sunday", "Monday", etc.) instead
  35. # # of only the first three letters.
  36. # calendar(:year => 2005, :month => 5) do |d| # This generates a simple calendar, but gives special days
  37. # if listOfSpecialDays.include?(d) # (days that are in the array listOfSpecialDays) one CSS class,
  38. # [d.mday, {:class => "specialDay"}] # "specialDay", and gives the rest of the days another CSS class,
  39. # else # "normalDay". You can also use this highlight today differently
  40. # [d.mday, {:class => "normalDay"}] # from the rest of the days, etc.
  41. # end
  42. def calendar(options = {}, &block)
  43. raise ArgumentError, "No year given" unless defined? options[:year]
  44. raise ArgumentError, "No month given" unless defined? options[:month]
  45.  
  46. block ||= Proc.new {|d| nil}
  47. options[:table_class ] ||= "calendar"
  48. options[:month_name_class ] ||= "monthName"
  49. options[:other_month_class ] ||= "otherMonth"
  50. options[:day_name_class ] ||= "dayName"
  51. options[:day_class ] ||= "day"
  52. options[:abbrev ] ||= (0..2)
  53.  
  54. first = Date.civil(options[:year], options[:month], 1)
  55. last = Date.civil(options[:year], options[:month], -1)
  56.  
  57. cal = <<EOF
  58. <table class="#{options[:table_class]}" cellspacing="4">
  59. <thead>
  60. <tr class="#{options[:month_name_class]}">
  61. <th colspan="7">#{Date::MONTHNAMES[options[:month]]} #{options[:year]}</th>
  62. </tr>
  63. <tr class="#{options[:day_name_class]}">
  64. EOF
  65. Date::DAYNAMES.each {|d| cal << " <th>#{d[options[:abbrev]]}</th>"}
  66. cal << " </tr>
  67. </thead>
  68. <tbody>
  69. <tr>"
  70. 0.upto(first.wday - 1) {|d| cal << " <td class='#{options[:other_month_class]}'></td>"} unless first.wday == 0
  71. first.upto(last) do |cur|
  72. cell_text, cell_attrs = block.call(cur)
  73. cell_text ||= cur.mday
  74. cell_attrs ||= {:class => options[:day_class]}
  75. cell_attrs = cell_attrs.map {|k, v| "#{k}='#{v}'"}.join(' ')
  76. cal << " <td #{cell_attrs}>#{cell_text}</td>"
  77. cal << " </tr>\n <tr>" if cur.wday == 6
  78. end
  79. last.wday.upto(5) {|d| cal << " <td class='#{options[:other_month_class]}'></td>"} unless last.wday == 6
  80. cal << " </tr>\n </tbody>\n</table>"
Add Comment
Please, Sign In to add comment