Class: BiblePassage::Reference
- Inherits:
-
Object
- Object
- BiblePassage::Reference
- Defined in:
- lib/bible_passage/reference.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#book ⇒ Object
readonly
Returns the value of attribute book.
-
#book_key ⇒ Object
Returns the value of attribute book_key.
-
#child ⇒ Object
readonly
Returns the value of attribute child.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#parent ⇒ Object
writeonly
Sets the attribute parent.
Class Method Summary collapse
-
.parse(passage, options = {}) ⇒ Object
The main method used for parsing passage strings.
-
.parse_child(passage, parent, options = {}) ⇒ Object
Parses a child reference in a compound passage string.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #attributes ⇒ Object
- #from_chapter ⇒ Object
- #from_chapter=(val) ⇒ Object
- #from_verse ⇒ Object
- #from_verse=(val) ⇒ Object
- #inheritable_attributes ⇒ Object
-
#initialize(book_key, from_chapter = nil, from_verse = nil, to_chapter = nil, to_verse = nil, options = {}) ⇒ Reference
constructor
A new instance of Reference.
- #parse_child(child_passage) ⇒ Object
- #to_chapter ⇒ Object
- #to_chapter=(val) ⇒ Object
- #to_s ⇒ Object
- #to_verse ⇒ Object
- #to_verse=(val) ⇒ Object
- #valid? ⇒ Boolean
- #whole_book? ⇒ Boolean
- #whole_chapter? ⇒ Boolean
- #whole_chapters? ⇒ Boolean
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, = {}) @raise_errors = .has_key?(:raise_errors) ? [:raise_errors] : true @data_store = [: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
#book ⇒ Object (readonly)
Returns the value of attribute book.
124 125 126 |
# File 'lib/bible_passage/reference.rb', line 124 def book @book end |
#book_key ⇒ Object
Returns the value of attribute book_key.
124 125 126 |
# File 'lib/bible_passage/reference.rb', line 124 def book_key @book_key end |
#child ⇒ Object (readonly)
Returns the value of attribute child.
124 125 126 |
# File 'lib/bible_passage/reference.rb', line 124 def child @child end |
#error ⇒ Object (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
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, = {}) = {raise_errors: true}.merge() translator = .delete(:translator) || BookKeyTranslator.new data_store = [:data_store] ||= BookDataStore.new match = match_passage_format(passage) if !match = "#{passage} is not a valid reference" if [:raise_errors] raise InvalidReferenceError.new() else return InvalidReference.new() end end book_key = translator.keyify(match[1], [: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, ) else ref = process_multi_chapter_match(book_key, match, ) 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, = {}) if match_passage_format(passage) ref = parse(passage, ) 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 |
#attributes ⇒ Object
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_chapter ⇒ Object
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_verse ⇒ Object
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_attributes ⇒ Object
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_chapter ⇒ Object
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_s ⇒ Object
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_verse ⇒ Object
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
257 258 259 |
# File 'lib/bible_passage/reference.rb', line 257 def valid? @error.nil? end |
#whole_book? ⇒ 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
235 236 237 |
# File 'lib/bible_passage/reference.rb', line 235 def whole_chapter? whole_chapters? && from_chapter == to_chapter end |
#whole_chapters? ⇒ Boolean
225 226 227 |
# File 'lib/bible_passage/reference.rb', line 225 def whole_chapters? from_verse == 1 && to_verse_last_in_chapter? end |