Class: Bible::BibleRefParser::Book::SingleBook

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book_symbol) ⇒ SingleBook

Argument should be a symbol from BookInfo class representing the book.



186
187
188
# File 'lib/bible/parser.rb', line 186

def initialize(book_symbol)
  @book_symbol = book_symbol
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/bible/parser.rb', line 239

def method_missing(symbol, *args)
  if ! @fixedUp
    case symbol
    # when chapters is referenced, remove the chapter method
    # and add chapters method so this book has multiple chapters.
    when :chapters
      c = self.chapter
      class << self
        attr_reader :chapters
        undef_method :chapter, :chapter=
      end
      remove_instance_variable :@chapter if defined?(@chapter)

      @chapters = Chapters.new(self)
      # If no initial chapter has been set, set it to 1
      if c.nil?
        @chapters << Chapter.new(self, 1)
      else
        @chapters << c
      end

      @chapters
    when :verse, :verse= 
      class << self
        define_method :verse do
          @chapter.verse  
        end
  
        define_method :verse= do |value|
          @chapter.verse = value
        end
      end
      
      self.__send__(symbol, *args)
    else
      super(symbol, *args)
    end
  else
    super(symbol, *args)
  end
end

Instance Attribute Details

#book_symbolObject

Returns the value of attribute book_symbol.



183
184
185
# File 'lib/bible/parser.rb', line 183

def book_symbol
  @book_symbol
end

#chapterObject

Returns the value of attribute chapter.



183
184
185
# File 'lib/bible/parser.rb', line 183

def chapter
  @chapter
end

Instance Method Details

#==(value) ⇒ Object

Assumes value is a symbol and compares it to the book represented here.



220
221
222
223
224
225
# File 'lib/bible/parser.rb', line 220

def ==(value)
  return false if value.nil? || @book_symbol.nil?
  if value.is_a?(Symbol)
    return value == @book_symbol
  end
end

#bookObject



190
191
192
# File 'lib/bible/parser.rb', line 190

def book
  self
end

#fixupObject

Fixes the book reference. If chapter is nil, assumed to refer to entire book. If chapters ends in -1, assumed to refere to chapters to end of book. If any chapters are contained in this book, they will all be fixed up.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/bible/parser.rb', line 197

def fixup
  # No chapters at all means ALL chapters, so create the range for fixup.
  if ! has_chapter?
    book = Books[@book_symbol]
    # By reference self.chapters, a refernce to Chapter 1 is created.
    # More verbosely, we could assign self.chapter = Chapter 1 and then
    # reference self.chapters, but there isn't much point.
    self.chapters << Chapter.new(self, -1)
    @chapters.fixup
  elsif single_chapter?
    @chapter.fixup
    if @chapter.single_verse?
      self.verse = @chapter.verse
    end
  else
    @chapters.fixup
  end
  # Don't want reference to chapters method to create chapters after fixup
  @fixedUp = true
end

#has_chapter?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/bible/parser.rb', line 227

def has_chapter?
  (respond_to?(:chapter) && ! @chapter.nil?) || (respond_to?(:chapters) && ! @chapters.nil?)
end

#inspectObject



281
282
283
284
285
286
287
# File 'lib/bible/parser.rb', line 281

def inspect
  if single_chapter?
    "#{@book_symbol} " + @chapter.inspect
  else
    "#{@book_symbol} " + @chapters.inspect
  end
end

#single_chapter?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/bible/parser.rb', line 231

def single_chapter?
  respond_to?(:chapter)
end

#single_verse?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/bible/parser.rb', line 235

def single_verse?
  single_chapter? && @chapter.single_verse?
end