Class: BiblePassage::Reference

Inherits:
Object
  • Object
show all
Defined in:
lib/bible_passage/reference.rb

Direct Known Subclasses

InvalidReference

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book_key, from_chapter = nil, from_verse = nil, to_chapter = nil, to_verse = nil, options = {}) ⇒ Reference

Returns a new instance of Reference.



128
129
130
131
132
133
134
135
136
137
# File 'lib/bible_passage/reference.rb', line 128

def initialize(book_key, from_chapter = nil, from_verse = nil, 
               to_chapter = nil, to_verse = nil, options = {})
  @raise_errors = options.has_key?(:raise_errors) ? options[:raise_errors] : true
  @data_store = options[:data_store] || BookDataStore.new
  self.book_key = book_key
  self.from_chapter = int_param(from_chapter)
  self.from_verse = int_param(from_verse)
  self.to_chapter = calculate_to_chapter(to_chapter)
  self.to_verse = calculate_to_verse(to_verse)
end

Instance Attribute Details

#bookObject (readonly)

Returns the value of attribute book.



124
125
126
# File 'lib/bible_passage/reference.rb', line 124

def book
  @book
end

#book_keyObject

Returns the value of attribute book_key.



124
125
126
# File 'lib/bible_passage/reference.rb', line 124

def book_key
  @book_key
end

#childObject (readonly)

Returns the value of attribute child.



124
125
126
# File 'lib/bible_passage/reference.rb', line 124

def child
  @child
end

#errorObject (readonly)

Returns the value of attribute error.



5
6
7
# File 'lib/bible_passage/reference.rb', line 5

def error
  @error
end

#parent=(value) ⇒ Object (writeonly)

Sets the attribute parent

Parameters:

  • value

    the value to set the attribute parent to.



126
127
128
# File 'lib/bible_passage/reference.rb', line 126

def parent=(value)
  @parent = value
end

Class Method Details

.parse(passage, options = {}) ⇒ Object

The main method used for parsing passage strings



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bible_passage/reference.rb', line 14

def parse(passage, options = {})
  options = {raise_errors: true}.merge(options)
  translator = options.delete(:translator) || BookKeyTranslator.new
  data_store = options[:data_store] ||= BookDataStore.new
  match = match_passage_format(passage)
  if !match
    message = "#{passage} is not a valid reference"
    if options[:raise_errors]
      raise InvalidReferenceError.new(message)
    else
      return InvalidReference.new(message)
    end
  end
  book_key = translator.keyify(match[1], options[:raise_errors]) || (return InvalidReference.new("#{match[1]} is not a valid book"))
  if data_store.number_of_chapters(book_key) == 1
    ref = process_single_chapter_match(book_key, match, options)
  else
    ref = process_multi_chapter_match(book_key, match, options)
  end
  ref.parse_child(match[6].gsub(/^,\s*/, '')) if match[6]
  ref
end

.parse_child(passage, parent, options = {}) ⇒ Object

Parses a child reference in a compound passage string



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bible_passage/reference.rb', line 39

def parse_child(passage, parent, options = {})
  if match_passage_format(passage)
    ref = parse(passage, options)
  else
    match = passage.match(/\s*(\d+)?:?(\d+)?\s*(-?)\s*(\d+)?:?(\d+)?\s*(,.+)?$/)
    book_key = parent.book_key
    attrs = parent.inheritable_attributes
    if attrs[:from_chapter]
      if match[2]
        attrs[:from_chapter] = match[1].to_i 
        attrs[:from_verse] = match[2].to_i
      else
        attrs[:from_verse] = match[1].to_i
      end
    else
      attrs[:from_chapter] = match[1].to_i
      if match[2]
        attrs[:from_verse] = match[2].to_i
      else
      end
    end
    if match[5]
      attrs[:to_chapter] = int_param(match[4])
      attrs[:to_verse] = int_param(match[5])
    elsif attrs[:from_verse]
      attrs[:to_verse] = int_param(match[4]) 
    else
      attrs[:to_chapter] = int_param(match[4])
    end
    ref = new(book_key, attrs[:from_chapter], attrs[:from_verse], 
        attrs[:to_chapter], attrs[:to_verse])
  end
  ref.parent = parent
  ref
end

Instance Method Details

#==(other) ⇒ Object



246
247
248
# File 'lib/bible_passage/reference.rb', line 246

def ==(other)
  attributes == other.attributes
end

#attributesObject



239
240
241
242
243
244
# File 'lib/bible_passage/reference.rb', line 239

def attributes
  %w{book_key from_chapter from_verse to_chapter to_verse}.
    inject({}) do |memo, attr_key|
      memo.merge(attr_key.to_sym => send(attr_key))
  end
end

#from_chapterObject



148
149
150
# File 'lib/bible_passage/reference.rb', line 148

def from_chapter
  @from_chapter || 1
end

#from_chapter=(val) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/bible_passage/reference.rb', line 152

def from_chapter=(val)
  if val
    if val < 1 || val > @data_store.number_of_chapters(book_key)
      @error = "#{book} doesn't have a chapter #{val}"
      raise InvalidReferenceError.new(@error) if @raise_errors
    end
    @inherit_book_key = true
    @from_chapter = val
  end
end

#from_verseObject



163
164
165
# File 'lib/bible_passage/reference.rb', line 163

def from_verse
  @from_verse || 1
end

#from_verse=(val) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# File 'lib/bible_passage/reference.rb', line 167

def from_verse=(val)
  if val && valid?
    if val > @data_store.number_of_verses(book_key, from_chapter) || val < 1
      @error = "#{book} #{from_chapter} doesn't have a verse #{val}"
      raise InvalidReferenceError.new(@error) if @raise_errors
    end
    @inherit_chapter = true
    @from_verse = val
  end
end

#inheritable_attributesObject



250
251
252
253
254
255
# File 'lib/bible_passage/reference.rb', line 250

def inheritable_attributes
  out = {}
  out[:book_key] = book_key if @inherit_book_key
  out[:from_chapter] = to_chapter if @inherit_chapter
  out
end

#parse_child(child_passage) ⇒ Object



139
140
141
# File 'lib/bible_passage/reference.rb', line 139

def parse_child(child_passage)
  @child = self.class::parse_child(child_passage, self)
end

#to_chapterObject



178
179
180
# File 'lib/bible_passage/reference.rb', line 178

def to_chapter
  @to_chapter || from_chapter
end

#to_chapter=(val) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/bible_passage/reference.rb', line 182

def to_chapter=(val)
  if val
    if val < from_chapter
      @error = "to_chapter cannot be before from_chapter"
      raise InvalidReferenceError.new(@error) if @raise_errors
    end
    if val > @data_store.number_of_chapters(book_key)
      @error = "#{book} doesn't have a chapter #{val}"
      raise InvalidReferenceError.new(@error) if @raise_errors
    end
    @to_chapter = val
  end
end

#to_sObject



214
215
216
217
218
219
220
221
222
223
# File 'lib/bible_passage/reference.rb', line 214

def to_s
  return nil if !valid?
  if @parent
    out = to_s_child
  else
    out = to_s_root
  end
  out << child.to_s if child
  out
end

#to_verseObject



196
197
198
# File 'lib/bible_passage/reference.rb', line 196

def to_verse
  @to_verse || from_verse
end

#to_verse=(val) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/bible_passage/reference.rb', line 200

def to_verse=(val)
  if val && valid?
    if val < from_verse && single_chapter_passage?
      @error = "to_verse cannot be before from_verse"
      raise InvalidReferenceError.new(@error) if @raise_errors
    end
    if val > @data_store.number_of_verses(book_key, to_chapter)
      @error = "#{book} #{to_chapter} doesn't have a verse #{val}"
      raise InvalidReferenceError.new(@error) if @raise_errors
    end
    @to_verse = val
  end
end

#valid?Boolean

Returns:

  • (Boolean)


257
258
259
# File 'lib/bible_passage/reference.rb', line 257

def valid?
  @error.nil?
end

#whole_book?Boolean

Returns:

  • (Boolean)


229
230
231
232
233
# File 'lib/bible_passage/reference.rb', line 229

def whole_book?
  from_chapter == 1 && from_verse == 1 && 
    to_chapter == @data_store.number_of_chapters(book_key) &&
    to_verse == @data_store.number_of_verses(book_key, to_chapter)
end

#whole_chapter?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/bible_passage/reference.rb', line 235

def whole_chapter?
  whole_chapters? && from_chapter == to_chapter
end

#whole_chapters?Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/bible_passage/reference.rb', line 225

def whole_chapters?
  from_verse == 1 && to_verse_last_in_chapter?
end