Class: Mericope
- Inherits:
-
Object
- Object
- Mericope
- Defined in:
- lib/mericope.rb,
lib/mericope/cli.rb,
lib/mericope/version.rb
Defined Under Namespace
Classes: CLI
Constant Summary collapse
- VERSION =
"0.3.0"
Instance Attribute Summary collapse
-
#book ⇒ Object
readonly
Returns the value of attribute book.
-
#book_chapter_count ⇒ Object
readonly
Returns the value of attribute book_chapter_count.
-
#book_name ⇒ Object
readonly
Returns the value of attribute book_name.
-
#original_string ⇒ Object
readonly
Returns the value of attribute original_string.
-
#ranges ⇒ Object
readonly
Returns the value of attribute ranges.
Class Method Summary collapse
- .___split_segments_by_pattern(segments, pattern) ⇒ Object
- .book_has_chapters?(book) ⇒ Boolean
- .book_name_regexes ⇒ Object
- .book_names ⇒ Object
- .extract(text) ⇒ Object
- .get_first_verse(book, chapter) ⇒ Object
- .get_last_verse(book, chapter) ⇒ Object
- .get_max_chapter(book) ⇒ Object
- .get_max_verse(book, chapter) ⇒ Object
- .get_next_verse(id) ⇒ Object
- .get_start_of_next_chapter(id) ⇒ Object
- .parse(text) ⇒ Object
-
.parse_one(text) ⇒ Object
Differs from Mericope.new in that it won’t raise an exception if text does not contain a mericope but will return nil instead.
- .rsub(text) ⇒ Object
- .split(text, pattern = nil) ⇒ Object
- .sub(text) ⇒ Object
Instance Method Summary collapse
- #book_has_chapters? ⇒ Boolean
-
#initialize(arg) ⇒ Mericope
constructor
A new instance of Mericope.
- #intersects?(mericope) ⇒ Boolean
- #report ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #well_formatted_reference ⇒ Object
Constructor Details
#initialize(arg) ⇒ Mericope
Returns a new instance of Mericope.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mericope.rb', line 22 def initialize(arg) case arg when String attributes = Mericope.match_one(arg) raise "no mericope found in #{arg} (#{arg.class})" if attributes.nil? @original_string = attributes[:original_string] set_book attributes[:book] @ranges = attributes[:ranges] when Array arg = arg.map(&:to_i) set_book Mericope.get_book(arg.first) @ranges = Mericope.group_array_into_ranges(arg) else attributes = arg @original_string = attributes[:original_string] set_book attributes[:book] @ranges = attributes[:ranges] end end |
Instance Attribute Details
#book ⇒ Object (readonly)
Returns the value of attribute book.
6 7 8 |
# File 'lib/mericope.rb', line 6 def book @book end |
#book_chapter_count ⇒ Object (readonly)
Returns the value of attribute book_chapter_count.
6 7 8 |
# File 'lib/mericope.rb', line 6 def book_chapter_count @book_chapter_count end |
#book_name ⇒ Object (readonly)
Returns the value of attribute book_name.
6 7 8 |
# File 'lib/mericope.rb', line 6 def book_name @book_name end |
#original_string ⇒ Object (readonly)
Returns the value of attribute original_string.
6 7 8 |
# File 'lib/mericope.rb', line 6 def original_string @original_string end |
#ranges ⇒ Object (readonly)
Returns the value of attribute ranges.
6 7 8 |
# File 'lib/mericope.rb', line 6 def ranges @ranges end |
Class Method Details
.___split_segments_by_pattern(segments, pattern) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/mericope.rb', line 106 def self.___split_segments_by_pattern(segments, pattern) segments2 = [] segments.each do |segment| if segment.is_a? Mericope segments2 << segment else segments2.concat(segment.split(pattern)) end end segments2 end |
.book_has_chapters?(book) ⇒ Boolean
46 47 48 |
# File 'lib/mericope.rb', line 46 def self.book_has_chapters?(book) book_chapter_counts[book - 1] >= 1 end |
.book_name_regexes ⇒ Object
17 18 19 20 |
# File 'lib/mericope.rb', line 17 def self.book_name_regexes @book_name_regexes ||= book_abbreviations. map { |book_number, book_regex| [book_number, /\b#{book_regex}\b/] } end |
.book_names ⇒ Object
12 13 14 15 |
# File 'lib/mericope.rb', line 12 def self.book_names @book_names ||= chapter_verse_count_books. map{ |r| r[2] }.uniq end |
.extract(text) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/mericope.rb', line 118 def self.extract(text) puts "DEPRECATION NOTICE: the 'extract' method will be removed in Mericope 0.7.0" segments = split(text) text = "" mericopes = [] segments.each do |segment| if segment.is_a?(String) text << segment else mericopes << segment end end {:text => text, :mericopes => mericopes} end |
.get_first_verse(book, chapter) ⇒ Object
241 242 243 |
# File 'lib/mericope.rb', line 241 def self.get_first_verse(book, chapter) get_id(book, chapter, 1) end |
.get_last_verse(book, chapter) ⇒ Object
245 246 247 |
# File 'lib/mericope.rb', line 245 def self.get_last_verse(book, chapter) get_id(book, chapter, get_max_verse(book, chapter)) end |
.get_max_chapter(book) ⇒ Object
237 238 239 |
# File 'lib/mericope.rb', line 237 def self.get_max_chapter(book) book_chapter_counts[book - 1] end |
.get_max_verse(book, chapter) ⇒ Object
232 233 234 235 |
# File 'lib/mericope.rb', line 232 def self.get_max_verse(book, chapter) id = (book * 1000000) + (chapter * 1000) chapter_verse_counts[id] end |
.get_next_verse(id) ⇒ Object
249 250 251 |
# File 'lib/mericope.rb', line 249 def self.get_next_verse(id) id + 1 end |
.get_start_of_next_chapter(id) ⇒ Object
253 254 255 256 257 258 |
# File 'lib/mericope.rb', line 253 def self.get_start_of_next_chapter(id) book = get_book(id) chapter = get_chapter(id) + 1 verse = 1 get_id(book, chapter, verse) end |
.parse(text) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/mericope.rb', line 63 def self.parse(text) mericopes = [] match_all(text) do |attributes| mericope = Mericope.new(attributes) if block_given? yield mericope else mericopes << mericope end end block_given? ? text : mericopes end |
.parse_one(text) ⇒ Object
Differs from Mericope.new in that it won’t raise an exception if text does not contain a mericope but will return nil instead.
56 57 58 59 60 61 |
# File 'lib/mericope.rb', line 56 def self.parse_one(text) parse(text) do |mericope| return mericope end nil end |
.rsub(text) ⇒ Object
144 145 146 147 148 149 |
# File 'lib/mericope.rb', line 144 def self.rsub(text) text.gsub(/\{\{(\d{7,8} ?)+\}\}/) do |match| ids = match[2...-2].split.collect(&:to_i) Mericope.new(ids).to_s end end |
.split(text, pattern = nil) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mericope.rb', line 76 def self.split(text, pattern=nil) puts "DEPRECATION NOTICE: split will no longer accept a 'pattern' argument in Mericope 0.7.0" if pattern segments = [] start = 0 match_all(text) do |attributes, match| pretext = text.slice(start...match.begin(0)) if pretext.length > 0 segments << pretext yield pretext if block_given? end mericope = Mericope.new(attributes) segments << mericope yield mericope if block_given? start = match.end(0) end pretext = text.slice(start...text.length) if pretext.length > 0 segments << pretext yield pretext if block_given? end segments = ___split_segments_by_pattern(segments, pattern) if pattern segments end |
.sub(text) ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/mericope.rb', line 133 def self.sub(text) segments = split(text) segments.inject("") do |text, segment| if segment.is_a?(String) text << segment else text << "{{#{segment.to_a.join(" ")}}}" end end end |
Instance Method Details
#book_has_chapters? ⇒ Boolean
50 51 52 |
# File 'lib/mericope.rb', line 50 def book_has_chapters? book_chapter_count >= 1 end |
#intersects?(mericope) ⇒ Boolean
219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/mericope.rb', line 219 def intersects?(mericope) return false unless mericope.is_a?(Mericope) return false unless (self.book == mericope.book) self.ranges.each do |self_range| mericope.ranges.each do |other_range| return true if (self_range.end >= other_range.begin) and (self_range.begin <= other_range.end) end end false end |
#report ⇒ Object
155 156 157 158 |
# File 'lib/mericope.rb', line 155 def report puts "DEPRECATION NOTICE: the 'report' method will be removed in Mericope 0.7.0" " #{self.original_string} => #{self}" end |
#to_a ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/mericope.rb', line 160 def to_a # one range per chapter chapter_ranges = [] ranges.each do |range| min_chapter = Mericope.get_chapter(range.begin) max_chapter = Mericope.get_chapter(range.end) if min_chapter == max_chapter chapter_ranges << range else chapter_ranges << Range.new(range.begin, Mericope.get_last_verse(book, min_chapter)) for chapter in (min_chapter+1)...max_chapter chapter_ranges << Range.new( Mericope.get_first_verse(book, chapter), Mericope.get_last_verse(book, chapter)) end chapter_ranges << Range.new(Mericope.get_first_verse(book, max_chapter), range.end) end end chapter_ranges.inject([]) {|array, range| array.concat(range.to_a)} end |
#to_s ⇒ Object
151 152 153 |
# File 'lib/mericope.rb', line 151 def to_s "#{book_name} #{self.well_formatted_reference}" end |
#well_formatted_reference ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/mericope.rb', line 182 def well_formatted_reference recent_chapter = nil # e.g. in 12:1-8, remember that 12 is the chapter when we parse the 8 recent_chapter = 1 unless book_has_chapters? ranges.map do |range| min_chapter = Mericope.get_chapter(range.begin) min_verse = Mericope.get_verse(range.begin) max_chapter = Mericope.get_chapter(range.end) max_verse = Mericope.get_verse(range.end) s = "" if min_verse == 1 and max_verse >= Mericope.get_max_verse(book, max_chapter) s << min_chapter.to_s s << "-#{max_chapter}" if max_chapter > min_chapter else if recent_chapter == min_chapter s << min_verse.to_s else recent_chapter = min_chapter s << "#{min_chapter}:#{min_verse}" end if range.count > 1 s << "-" if min_chapter == max_chapter s << max_verse.to_s else recent_chapter = max_chapter s << "#{max_chapter}:#{max_verse}" end end end s end.join(", ") end |