Class: CSKit::Lesson::Lesson

Inherits:
Object
  • Object
show all
Defined in:
lib/cskit/lesson/lesson.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sections) ⇒ Lesson

Returns a new instance of Lesson.



31
32
33
# File 'lib/cskit/lesson/lesson.rb', line 31

def initialize(sections)
  @sections = sections
end

Instance Attribute Details

#sectionsObject (readonly)

Returns the value of attribute sections.



9
10
11
# File 'lib/cskit/lesson/lesson.rb', line 9

def sections
  @sections
end

Class Method Details

.from_file(file) ⇒ Object



12
13
14
15
# File 'lib/cskit/lesson/lesson.rb', line 12

def from_file(file)
  ext = File.extname(file)[1..-1]
  send(:"from_#{ext}", File.read(file))
end

.from_json(data) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cskit/lesson/lesson.rb', line 17

def from_json(data)
  sections = JSON.parse(data).map do |section_data|
    Section.new(
      section_data['section'],
      section_data['readings'].inject({}) do |ret, (k, v)|
        ret[k.to_sym] = v; ret
      end
    )
  end

  new(sections)
end

Instance Method Details

#each_formatted_reading(formatters) ⇒ Object



45
46
47
48
49
50
# File 'lib/cskit/lesson/lesson.rb', line 45

def each_formatted_reading(formatters)
  each_reading(formatters.keys) do |section, citation, volume, readings|
    formatter = formatters[volume]
    yield section, citation, volume, formatter.format_readings(readings)
  end
end

#each_formatted_section(formatters) {|last_section, current_texts| ... } ⇒ Object

Yields:

  • (last_section, current_texts)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cskit/lesson/lesson.rb', line 52

def each_formatted_section(formatters)
  last_section = nil
  current_texts = nil

  each_formatted_reading(formatters) do |section, citation, volume, text|
    if section.name != (last_section.name rescue nil)
      yield section, current_texts if current_texts
      current_texts = {}
    end

    last_section = section
    formatter = formatters[volume]
    current_texts[volume] = formatter.join([current_texts[volume], text])
  end

  yield last_section, current_texts if current_texts
end

#each_reading(volumes) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/cskit/lesson/lesson.rb', line 35

def each_reading(volumes)
  sections.each do |section|
    volumes.each do |volume|
      section.each_reading_group_for(volume) do |readings, citation|
        yield section, citation, volume, readings
      end
    end
  end
end