Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.91 KB | None | 0 0
  1. $LOAD_PATH.unshift File.join(File.dirname(__FILE__),'../..','ext_pr1/lib')
  2.  require 'ext_pr1_v4'
  3.  
  4. # Teilufgabe 1
  5. # Transformation der Wochentage in OO
  6.  
  7. # Konstanten für Werte
  8. DAY_SYM_SEQ = [:Mo, :Di, :Mi, :Do, :Fr, :Sa, :So]
  9. DAY_NUM_SEQ = [1, 2, 3, 4, 5, 6, 7]
  10. # Konstanten für Indices
  11. DAYS_IN_WEEK = DAY_SYM_SEQ.size
  12. DAY_INDEX_SEQ = (0...DAYS_IN_WEEK)
  13.  
  14. # Klasse für Tag
  15. # Day ::= DaySym | DayNum
  16.  
  17. def_class(:Day,[:day]) {
  18.   def invariant?
  19.     day.sym? or day.num?
  20.   end
  21.  
  22.   def to_day_num
  23.     idx + 1
  24.   end
  25.  
  26.   def to_day_sym
  27.     DAY_SYM_SEQ[to_idx]
  28.   end
  29.  
  30.   def to_idx
  31.   DAY_SYM_SEQ.index(sym)
  32.   end
  33. }
  34.  
  35. # Klasse für die Wochentage aus DAY_SYM_SEQ
  36. # DaySym ::= DaySym[:sym] :: {:Mo, :Di, :Mi, :Do, :Fr, :Sa, :So}
  37. # Tests {DaySym[:Mo] => DayNum[1], DaySym[:Mo] => DaySym[:Mo], DaySym[:Mo] =>
  38. #     }
  39. def_class(:DaySym,[:sym]) {
  40.   def invariant?
  41.     sym.in?(DAY_SYM_SEQ) and DAY_SYM_SEQ.include?(sym)
  42.   end
  43.  
  44.   def to_day_num
  45.     idx + 1
  46.   end
  47.  
  48.   def to_day_sym
  49.     DAY_SYM_SEQ[to_idx]
  50.   end
  51.  
  52.   def to_idx
  53.   DAY_SYM_SEQ.index(sym)
  54.   end
  55.  
  56. }
  57.  
  58. # Klasse für die Wochentage aus DAY_NUM_SEQ
  59. # DayNum ::= DayNum[:num] :: {1,2,3,4,5,6,7}
  60. # Tests {DayNum[1] => DayNum[1], DayNum[7] => DayNum[7],
  61. #        DayNum[0] => Err, DayNum[8] => Err}
  62. def_class(:DayNum, [:num]) {
  63.   def invariant?
  64.     num.int? and DAY_NUM_SEQ.include?(num)
  65.   end
  66.  
  67.   def to_day_num
  68.     idx + 1
  69.   end
  70.  
  71.   def to_day_sym
  72.     DAY_SYM_SEQ[idx]
  73.   end
  74.  
  75.   def to_idx
  76.   DAY_NUM_SEQ.index(num)
  77.   end
  78. }
  79.  
  80. # Klasse für Index von Tag
  81. # DayIdx ::= DayIdx[:idx] :: {(0...DAYS_IN_WEEK)}
  82. def_class(:DayIdx,[:idx]) {
  83.   def invariant?
  84.     idx.int? and DAY_NUM_SEQ.index.include?(idx)
  85.   end
  86.  
  87.    def to_day_num
  88.     idx + 1
  89.   end
  90.  
  91.   def to_day_sym
  92.     DAY_SYM_SEQ[idx]
  93.   end
  94. }
  95.  
  96.  
  97. # Hilfsmethoden:
  98. # Ermittelt den Index des angegebenen num im Array DAY_NUM_SEQ (Hilfsmethode)
  99. #def to_idx
  100. #  DAY_NUM_SEQ.index(num)
  101. #end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement