Class: Bible::BibleRefParser::Chapters

Inherits:
Object
  • Object
show all
Includes:
RangeComparisons
Defined in:
lib/bible/parser.rb

Overview

Represents range or discontinuous series of chaptesr

Instance Method Summary collapse

Methods included from RangeComparisons

#compare_ranges

Constructor Details

#initialize(book) ⇒ Chapters

Returns a new instance of Chapters.



535
536
537
538
# File 'lib/bible/parser.rb', line 535

def initialize(book)
  @chapters = []
  @book = book
end

Instance Method Details

#<<(value) ⇒ Object



552
553
554
555
556
557
558
559
560
561
562
563
# File 'lib/bible/parser.rb', line 552

def <<(value)
  unless value.nil? || @chapters.empty? || @chapters.last.nil? || value == -1
    raise "Cannot add a chapter reference in reverse order" if @chapters.last > value
    unless @chapters.last.chapter_number == value
      @chapters.last.succ.upto(value) { |c|
        @chapters << c
      }
    end
  else
    @chapters << value
  end
end

#==(value) ⇒ Object



577
578
579
# File 'lib/bible/parser.rb', line 577

def ==(value)
  compare_ranges(@chapters, value, "chapters")
end

#[](index) ⇒ Object



573
574
575
# File 'lib/bible/parser.rb', line 573

def [](index)
  return @chapters.compact[index]
end

#eachObject

Returns Chapter objects for each chapter, or -1 if “end of chapters” is indicated. If chapters is empty, will be a no-op



591
592
593
594
595
596
# File 'lib/bible/parser.rb', line 591

def each
  return if @chapters.empty?
  @chapters.each { |chapter|
    yield chapter unless chapter.nil?
  }
end

#fixupObject



540
541
542
543
544
545
546
547
548
549
550
# File 'lib/bible/parser.rb', line 540

def fixup
  if @chapters.last == -1
    book = Books[@book.book_symbol]
    @chapters.pop
    @chapters.last.succ.upto(book.chapters.last) do |c|
      @chapters << c
    end
  end

  @chapters.compact.each { |c| c.fixup }
end

#inspectObject



581
582
583
584
585
586
587
# File 'lib/bible/parser.rb', line 581

def inspect
  @chapters.inject("") { |val, c|
    val << "," unless val.empty?
    val << c.inspect unless c.nil?
    val
  }
end

#lastObject



569
570
571
# File 'lib/bible/parser.rb', line 569

def last
  @chapters.last
end

#lengthObject



565
566
567
# File 'lib/bible/parser.rb', line 565

def length
  @chapters.length
end