Module: Zmanim::Limudim::LimudCalculator

Instance Method Summary collapse

Instance Method Details

#cycle_end_calculationObject



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

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

#cycle_units_calculationObject



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

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)



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

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, ...]`


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

def default_units
  {}
end

#find_cycle(date) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/zmanim/limudim/limud_calculator.rb', line 127

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/zmanim/limudim/limud_calculator.rb', line 108

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)`



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

def fractional_units
  nil
end

#initial_cycle_dateObject

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



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

def initial_cycle_date
  nil
end

#interval_end_calculationObject



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

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
20
# 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 && cycle.end_date >= date
  units = cycle_units_calculation.(cycle)
  interval = Interval.first_for_cycle(cycle, interval_end_calculation)
  while !jewish_date.between?(interval.start_date, interval.end_date) do
    interval = if skip_interval?(interval)
                 interval.skip(interval_end_calculation)
               else
                 interval.next(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



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

def perpetual_cycle_anchor
  nil
end

#skip_interval?(interval) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/zmanim/limudim/limud_calculator.rb', line 92

def skip_interval?(interval)
  false
end

#skip_unitObject



88
89
90
# File 'lib/zmanim/limudim/limud_calculator.rb', line 88

def skip_unit
  nil
end

#starting_page(units, unit_name) ⇒ Object



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

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)

Returns:

  • (Boolean)


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

def tiered_units?
  true
end

#tiered_units_for_interval(units, interval) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/zmanim/limudim/limud_calculator.rb', line 96

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



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

def unit_for_interval(units, interval)
  return skip_unit if skip_interval?(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



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

def unit_step
  1
end