Class: Bible::BibleIterator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bible/iterator.rb

Overview

Will iterate over verses in a given reference, using the lookup given.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ref, lookup = nil) ⇒ BibleIterator

Returns a new instance of BibleIterator.



18
19
20
21
22
23
# File 'lib/bible/iterator.rb', line 18

def initialize(ref, lookup = nil)
  raise "A lookup class must be provided if no default is set" if lookup.nil? && @@default_lookup.nil?
  raise "A reference to look up must be provided" if ref.nil?
  @reference = ref
  @lookup = lookup || @@default_lookup
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



25
26
27
# File 'lib/bible/iterator.rb', line 25

def method_missing(sym, *args)
  @reference.__send__(sym, *args)
end

Instance Attribute Details

#referenceObject (readonly)

Returns the value of attribute reference.



7
8
9
# File 'lib/bible/iterator.rb', line 7

def reference
  @reference
end

Class Method Details

.default_lookupObject

default lookup, if none selected



10
11
12
# File 'lib/bible/iterator.rb', line 10

def self.default_lookup
  @@default_lookup ||= nil
end

.default_lookup=(value) ⇒ Object



14
15
16
# File 'lib/bible/iterator.rb', line 14

def self.default_lookup=(value)
  @@default_lookup = value
end

Instance Method Details

#each(&blk) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/bible/iterator.rb', line 47

def each(&blk)
  if @reference.single_book?
    iterate_book(@reference.book, &blk) 
  else
    @reference.books.each { |b| iterate_book(b, &blk) }
  end
end

#inspectObject



75
76
77
# File 'lib/bible/iterator.rb', line 75

def inspect
  self.to_s
end

#iterate_book(book, &blk) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/bible/iterator.rb', line 37

def iterate_book(book, &blk)
  if book.single_chapter?
    iterate_chapter(book.chapter, &blk)
  else
    book.chapters.each { |c|
      iterate_chapter(c, &blk)
    }
  end
end

#iterate_chapter(chapter, &blk) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/bible/iterator.rb', line 29

def iterate_chapter(chapter, &blk)
  if chapter.single_verse?
    blk.call(@lookup.get_ref(chapter.book.book_symbol, chapter.chapter_number, chapter.verse.verse_number), chapter.book.book_symbol, chapter.chapter_number, chapter.verse.verse_number)
  else
    chapter.verses.each { |v| blk.call(@lookup.get_ref(chapter.book.book_symbol, chapter.chapter_number, v.verse_number), chapter.book.book_symbol, chapter.chapter_number, v.verse_number) }
  end
end

#to_sObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bible/iterator.rb', line 55

def to_s
  s = ""
  v, b, c = nil, nil, nil

  self.each do |text, book_symbol, chapter_number, verse_number|
    if book_symbol != b && chapter_number != c
      s << "\n\n" unless s.nil? || s.strip == ""
      s << book_symbol.to_s << ", Chapter " << chapter_number.to_s << "\n\n"
    elsif chapter_number != c
      s << "\n\n" unless s.nil? || s.strip == ""
      s << "Chapter " << chapter_number.to_s << "\n\n"
    elsif (! v.nil?) && v != verse_number - 1
      s << "\n\n"
    end
    b, c, v = book_symbol, chapter_number, verse_number
    s << text
  end
  s
end