Class: Cal

Inherits:
Object show all
Defined in:
lib/cal.rb

Constant Summary collapse

START =
{
  'cn' => true,    # China
  'de' => 2342032, # Germany (protestant states)
  'dk' => 2342032, # Denmark
  'en' => 2361222, # English
  'es' => 2299161, # Spain
  'fi' => 2361390, # Finland
  'fr' => 2299227, # France
  'gb' => 2361222, # United Kingdom
  'gr' => 2423868, # Greece
  'hu' => 2301004, # Hungary
  'it' => 2299161, # Italy
  'jp' => true,    # Japan
  'no' => 2342032, # Norway
  'nl' => 2361222,
  'pl' => 2299161, # Poland
  'pt' => 2299161, # Portugal
  'ru' => 2421639, # Russia
  'se' => 2361390, # Sweden
  'us' => 2361222, # United States
  'os' => false,   # (old style)
  'ns' => true     # (new style)
}
DEFAULT_START =
'gb'

Instance Method Summary collapse

Constructor Details

#initializeCal

Returns a new instance of Cal.



37
38
39
# File 'lib/cal.rb', line 37

def initialize
  opt_j; opt_m; opt_t; opt_y; opt_c
end

Instance Method Details

#addmon(y, m, n) ⇒ Object



140
141
142
143
# File 'lib/cal.rb', line 140

def addmon(y, m, n)
  y, m = (y * 12 + (m - 1) + n).divmod(12)
  return y, m + 1
end

#block(xs, n) ⇒ Object



124
125
126
# File 'lib/cal.rb', line 124

def block(xs, n)
  stack(group(xs, n).collect{|ys| trans(ys).collect{|zs| zs.join('  ')}})
end

#en_pict(y, m) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cal.rb', line 86

def en_pict(y, m)
  d = (1..31).detect{|d| Date.valid_date?(y, m, d, @start)}
  fi = Date.new(y, m, d, @start)
  fi -= (fi.jd - @k + 1) % 7

  ve  = (fi..fi +  6).collect{|cu|
    %w(Su Mo Tu We Th Fr Sa)[cu.wday]
  }
  ve += (fi..fi + 41).collect{|cu|
    if cu.mon == m then cu.send(@da) end.to_s
  }

  ve = ve.collect{|e| e.rjust(@dw)}

  gr = group(ve, 7)
  gr = trans(gr) if @opt_t
  ta = gr.collect{|xs| xs.join('  ')}

  ca = %w(January February March April May June July
   August September October November December)[m - 1]
  ca = ca + ' ' + y.to_s if not @opt_y
  ca = ca  #.center(@mw)

  ta.unshift(ca)
end

#group(xs, n) ⇒ Object



112
113
114
# File 'lib/cal.rb', line 112

def group(xs, n)
  (0..xs.size / n - 1).collect{|i| xs[i * n, n]}
end

#monthly(y, m) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/cal.rb', line 132

def monthly(y, m)
 if $lang == "nl"
  return nl_pict(y,m)
  end
  return en_pict(y,m)
  #unlines(pict(y, m))
end

#nl_pict(y, m) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cal.rb', line 59

def nl_pict(y, m)
  d = (1..31).detect{|d| Date.valid_date?(y, m, d, @start)}
  fi = Date.new(y, m, d, @start)
  fi -= (fi.jd - @k + 1) % 7

  ve  = (fi..fi +  6).collect{|cu|
    %w(zo ma di wo do vr za)[cu.wday]
  }
  ve += (fi..fi + 41).collect{|cu|
    if cu.mon == m then cu.send(@da) end.to_s
  }

  ve = ve.collect{|e| e.rjust(@dw)}

  gr = group(ve, 7)
  gr = trans(gr) if @opt_t
  ta = gr.collect{|xs| xs.join('  ')}

  ca = %w(januari februari  maart  april mei juni juli
   augustus september oktober november december)[m - 1]
  ca = ca + ' ' + y.to_s if not @opt_y
  ca = ca.center(@mw)

  ta.unshift(ca)
end

#opt_c(arg = DEFAULT_START) ⇒ Object



46
# File 'lib/cal.rb', line 46

def opt_c(arg=DEFAULT_START) @start = START[arg] end

#opt_j(flag = false) ⇒ Object



41
# File 'lib/cal.rb', line 41

def opt_j(flag=false) @opt_j = flag end

#opt_m(flag = false) ⇒ Object



42
# File 'lib/cal.rb', line 42

def opt_m(flag=false) @opt_m = flag end

#opt_t(flag = false) ⇒ Object



43
# File 'lib/cal.rb', line 43

def opt_t(flag=false) @opt_t = flag end

#opt_y(flag = false) ⇒ Object



44
# File 'lib/cal.rb', line 44

def opt_y(flag=false) @opt_y = flag end


150
151
152
153
# File 'lib/cal.rb', line 150

def print(y, m)
  set_params
  if @opt_y then yearly(y) else monthly(y, m) end
end

#set_paramsObject



48
49
50
51
52
53
54
55
# File 'lib/cal.rb', line 48

def set_params
  @dw = if @opt_j then 3 else 2 end
  @mw = (@dw + 1) * 7 - 1
  @mn = if @opt_j then 2 else 3 end
  @tw = (@mw + 2) * @mn - 2
  @k  = if @opt_m then 1 else 0 end
  @da = if @opt_j then :yday else :mday end
end

#stack(xs) ⇒ Object



120
121
122
# File 'lib/cal.rb', line 120

def stack(xs)
  if xs.empty? then [] else xs[0] + stack(xs[1..-1]) end
end

#trans(xs) ⇒ Object



116
117
118
# File 'lib/cal.rb', line 116

def trans(xs)
  (0..xs[0].size - 1).collect{|i| xs.collect{|x| x[i]}}
end

#unlines(xs) ⇒ Object



128
129
130
# File 'lib/cal.rb', line 128

def unlines(xs)
  xs.collect{|x| x + "\n"}.join
end

#yearly(y) ⇒ Object



145
146
147
148
# File 'lib/cal.rb', line 145

def yearly(y)
  y.to_s.center(@tw) + "\n\n" +
    unlines(block((0..11).collect{|n| pict(*addmon(y, 1, n))}, @mn)) + "\n"
end