Module: Zmanim::Limudim::LimudCalculator

Instance Method Summary collapse

Instance Method Details

#cycle_end_calculationObject



69
70
71
# File 'lib/zmanim/limudim/limud_calculator.rb', line 69

def cycle_end_calculation
  ->(start_date, iteration){ start_date }
end

#cycle_units_calculationObject



77
78
79
# File 'lib/zmanim/limudim/limud_calculator.rb', line 77

def cycle_units_calculation
  ->(cycle){ default_units }
end

#default_starting_pageObject

Change this when using page numbers that do not generally start from one. (e.g. Talmud Bavli pages start from 2)



61
62
63
# File 'lib/zmanim/limudim/limud_calculator.rb', line 61

def default_starting_page
  1
end

#default_unitsObject

For tiered units, this would be a Hash in the format:

`{some_name: last_page, ...}`

or:

`{maseches: {perek_number => mishnayos, ...}, ...}`.

For simple units, use an Array in the format:

`[:some_name, ...]`


49
50
51
# File 'lib/zmanim/limudim/limud_calculator.rb', line 49

def default_units
  {}
end

#find_cycle(date) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/zmanim/limudim/limud_calculator.rb', line 121

def find_cycle(date)
  if initial_cycle_date
    Cycle.from_cycle_initiation(initial_cycle_date, cycle_end_calculation, date)
  elsif perpetual_cycle_anchor
    Cycle.from_perpetual_anchor(perpetual_cycle_anchor, cycle_end_calculation, date)
  else
    raise 'Cycle cannot be determined without an initial date or an anchor'
  end
end

#find_offset_units(units, targets) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/zmanim/limudim/limud_calculator.rb', line 102

def find_offset_units(units, targets)
  units.reduce(targets) do |t, (name, attributes)|
    if attributes.is_a?(Numeric)
      start = starting_page(units, name)
      length = (attributes - start) + 1
      t.select{|o, p| o == 0} + t.reject{|o,p| o == 0}.map do |o, p|
        o <= length ?
             [0, p + [name, (start + o) - 1]] :
             [o - length, p]
      end
    else
      t.select{|o, p| o == 0} +
      find_offset_units(attributes, t.reject{|o,p| o == 0}.map{|o, p| [o, p + [name]]}).map do |o, p|
        o == 0 ? [o, p] : [o, p[0..-2]]
      end
    end
  end
end

#fractional_unitsObject

Set if units are applied fractionally (indicated by a fractional unit_step). For example, an amud yomi calculator would set ‘%w(a b)`



55
56
57
# File 'lib/zmanim/limudim/limud_calculator.rb', line 55

def fractional_units
  nil
end

#initial_cycle_dateObject

Jewish Date on which the first cycle starts (if not perpetual)



22
23
24
# File 'lib/zmanim/limudim/limud_calculator.rb', line 22

def initial_cycle_date
  nil
end

#interval_end_calculationObject



73
74
75
# File 'lib/zmanim/limudim/limud_calculator.rb', line 73

def interval_end_calculation
  ->(cycle, start_date){ start_date }
end

#limud(date) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/zmanim/limudim/limud_calculator.rb', line 5

def limud(date)
  jewish_date = jewish_date(date)
  cycle = find_cycle(jewish_date)
  return nil unless cycle
  units = cycle_units_calculation.(cycle)
  interval = cycle.first_interval(interval_end_calculation)
  while !jewish_date.between?(interval.start_date, interval.end_date) do
    interval = interval.next(interval_end_calculation)
    while !jewish_date.between?(interval.start_date, interval.end_date) && skip_interval?(interval)
      interval = interval.skip(interval_end_calculation)
    end
  end
  unit = unit_for_interval(units, interval)
  Zmanim::Limudim::Limud.new(interval, unit)
end

#perpetual_cycle_anchorObject

Anchor on which a cycle resets (where relevant) e.g. for Parsha this would be a Day-of-Year anchor



28
29
30
# File 'lib/zmanim/limudim/limud_calculator.rb', line 28

def perpetual_cycle_anchor
  nil
end

#skip_interval?(interval) ⇒ Boolean



86
87
88
# File 'lib/zmanim/limudim/limud_calculator.rb', line 86

def skip_interval?(interval)
  false
end

#starting_page(units, unit_name) ⇒ Object



65
66
67
# File 'lib/zmanim/limudim/limud_calculator.rb', line 65

def starting_page(units, unit_name)
  default_starting_page
end

#tiered_units?Boolean

Are units components of some larger grouping? (e.g. pages or mishnayos)



38
39
40
# File 'lib/zmanim/limudim/limud_calculator.rb', line 38

def tiered_units?
  true
end

#tiered_units_for_interval(units, interval) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/zmanim/limudim/limud_calculator.rb', line 90

def tiered_units_for_interval(units, interval)
  iteration = interval.iteration
  offset = ((iteration - 1) * unit_step) + 1
  offset2 = (offset - 1) + unit_step if unit_step > 1
  offsets = [offset, offset2].compact
  targets = offsets.map{|o| [o, []]}
  results = find_offset_units(units, targets)
  return nil unless results.map(&:first).uniq == [0]
  paths = results.map(&:last)
  Unit.new(*paths)
end

#unit_for_interval(units, interval) ⇒ Object



81
82
83
84
# File 'lib/zmanim/limudim/limud_calculator.rb', line 81

def unit_for_interval(units, interval)
  return tiered_units_for_interval(units, interval) if tiered_units?
  Unit.new(*units[interval.iteration-1])
end

#unit_stepObject

Number of units to apply over an iteration



33
34
35
# File 'lib/zmanim/limudim/limud_calculator.rb', line 33

def unit_step
  1
end