Class: Pericope
- Inherits:
-
Object
- Object
- Pericope
- Defined in:
- lib/pericope.rb,
lib/pericope/cli.rb,
lib/pericope/version.rb
Defined Under Namespace
Classes: CLI
Constant Summary collapse
- VERSION =
"0.5.3"
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.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#original_string ⇒ Object
readonly
Returns the value of attribute original_string.
-
#ranges ⇒ Object
readonly
Returns the value of attribute ranges.
Class Method Summary collapse
- .book_name_regexes ⇒ Object
- .book_names ⇒ Object
- .extract(text) ⇒ Object
- .get_max_verse(book, chapter) ⇒ Object
- .parse(text, &block) ⇒ Object
- .parse_one(text) ⇒ Object
- .rsub(text) ⇒ Object
- .split(text, pattern = nil) ⇒ Object
- .sub(text) ⇒ Object
Instance Method Summary collapse
- #book_has_chapters? ⇒ Boolean
-
#initialize(string_or_array) ⇒ Pericope
constructor
A new instance of Pericope.
- #intersects?(pericope) ⇒ Boolean
- #report ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #well_formatted_reference ⇒ Object
Constructor Details
#initialize(string_or_array) ⇒ Pericope
Returns a new instance of Pericope.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/pericope.rb', line 29 def initialize(string_or_array) case string_or_array when String, MatchData match = string_or_array.is_a?(String) ? Pericope.match_one(string_or_array) : string_or_array raise "no pericope found in #{string_or_array} (#{string_or_array.class})" if match.nil? @original_string = match.to_s @index = match.begin(0) set_book match.instance_variable_get('@book') @ranges = parse_reference(match[1]) when Array set_book Pericope.get_book(string_or_array.first) @ranges = Pericope.group_array_into_ranges(string_or_array) else raise ArgumentError, "#{string_or_array.class} is not a recognized input for pericope" end end |
Instance Attribute Details
#book ⇒ Object (readonly)
Returns the value of attribute book.
7 8 9 |
# File 'lib/pericope.rb', line 7 def book @book end |
#book_chapter_count ⇒ Object (readonly)
Returns the value of attribute book_chapter_count.
7 8 9 |
# File 'lib/pericope.rb', line 7 def book_chapter_count @book_chapter_count end |
#book_name ⇒ Object (readonly)
Returns the value of attribute book_name.
7 8 9 |
# File 'lib/pericope.rb', line 7 def book_name @book_name end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
7 8 9 |
# File 'lib/pericope.rb', line 7 def index @index end |
#original_string ⇒ Object (readonly)
Returns the value of attribute original_string.
7 8 9 |
# File 'lib/pericope.rb', line 7 def original_string @original_string end |
#ranges ⇒ Object (readonly)
Returns the value of attribute ranges.
7 8 9 |
# File 'lib/pericope.rb', line 7 def ranges @ranges end |
Class Method Details
.book_name_regexes ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/pericope.rb', line 19 def self.book_name_regexes @@book_name_regexes ||= begin book_abbreviations.map do |book| [book, Regexp.new("\\b#{book[1]}\\b.? (#{ValidReference})", true)] end end end |
.book_names ⇒ Object
15 16 17 |
# File 'lib/pericope.rb', line 15 def self.book_names @@book_names ||= ["Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy", "Joshua", "Judges", "Ruth", "1 Samuel", "2 Samuel", "1 Kings", "2 Kings", "1 Chronicles", "2 Chronicles", "Ezra", "Nehemiah", "Esther", "Job", "Psalm", "Proverbs", "Ecclesiastes", "Song of Songs", "Isaiah", "Jeremiah", "Lamentations", "Ezekiel", "Daniel", "Hosea", "Joel", "Amos", "Obadiah", "Jonah", "Micah", "Nahum", "Habakkuk", "Zephaniah", "Haggai", "Zechariah", "Malachi", "Matthew", "Mark", "Luke", "John", "Acts", "Romans", "1 Corinthians", "2 Corinthians", "Galatians", "Ephesians", "Philippians", "Colossians", "1 Thessalonians", "2 Thessalonians", "1 Timothy", "2 Timothy", "Titus", "Philemon", "Hebrews", "James", "1 Peter", "2 Peter", "1 John ", "2 John", "3 John", "Jude", "Revelation"] end |
.extract(text) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/pericope.rb', line 99 def self.extract(text) segments = split(text) text = "" pericopes = [] segments.each do |segment| if segment.is_a?(String) text << segment else pericopes << segment end end {:text => text, :pericopes => pericopes} end |
.get_max_verse(book, chapter) ⇒ Object
227 228 229 230 |
# File 'lib/pericope.rb', line 227 def self.get_max_verse(book, chapter) id = (book * 1000000) + (chapter * 1000) chapter_verse_counts[id] end |
.parse(text, &block) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/pericope.rb', line 64 def self.parse(text, &block) pericopes = [] match_all text do |match| pericope = Pericope.new(match) if block_given? yield pericope else pericopes << pericope end end block_given? ? text : pericopes end |
.parse_one(text) ⇒ Object
57 58 59 60 |
# File 'lib/pericope.rb', line 57 def self.parse_one(text) match = match_one(text) match ? Pericope.new(match) : nil end |
.rsub(text) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/pericope.rb', line 128 def self.rsub(text) text.gsub(/\{\{(\d{7,8} ?)+\}\}/) do |match| ids = match[2...-2].split.collect(&:to_i) Pericope.new(ids).to_s end end |
.split(text, pattern = nil) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/pericope.rb', line 79 def self.split(text, pattern=nil) matches = match_all(text) # find every pericope in the text matches = matches.sort {|a,b| a.begin(0) <=> b.begin(0)} # put them in the order of their occurrence segments = [] start = 0 for match in matches pretext = text.slice(start...match.begin(0)) segments.concat(pattern ? pretext.split(pattern).delete_if{|s| s.length==0} : [pretext]) if (pretext.length>0) segments << Pericope.new(match) start = match.end(0) end pretext = text.slice(start...text.length) segments.concat(pattern ? pretext.split(pattern).delete_if{|s| s.length==0} : [pretext]) if (pretext.length>0) segments end |
.sub(text) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/pericope.rb', line 115 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
51 52 53 |
# File 'lib/pericope.rb', line 51 def book_has_chapters? (book_chapter_count > 1) end |
#intersects?(pericope) ⇒ Boolean
214 215 216 217 218 219 220 221 222 223 |
# File 'lib/pericope.rb', line 214 def intersects?(pericope) return false unless pericope.is_a?(Pericope) return false unless (self.book == pericope.book) for self_range in self.ranges for other_range in pericope.ranges return true if (self_range.max >= other_range.min) and (self_range.min <= other_range.max) end end return false end |
#report ⇒ Object
143 144 145 |
# File 'lib/pericope.rb', line 143 def report " #{self.original_string} => #{self}" end |
#to_a ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/pericope.rb', line 149 def to_a # one range per chapter chapter_ranges = [] for range in ranges min_chapter = Pericope.get_chapter(range.min) max_chapter = Pericope.get_chapter(range.max) if (min_chapter==max_chapter) chapter_ranges << range else chapter_ranges << Range.new(range.min, Pericope.get_last_verse(book, min_chapter)) for chapter in (min_chapter+1)...max_chapter chapter_ranges << Range.new( Pericope.get_first_verse(book, chapter), Pericope.get_last_verse(book, chapter)) end chapter_ranges << Range.new(Pericope.get_first_verse(book, max_chapter), range.max) end end chapter_ranges.inject([]) {|array, range| array.concat(range.to_a)} end |
#to_s ⇒ Object
137 138 139 |
# File 'lib/pericope.rb', line 137 def to_s "#{book_name} #{self.well_formatted_reference}" end |
#well_formatted_reference ⇒ Object
173 174 175 176 177 178 179 180 181 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 |
# File 'lib/pericope.rb', line 173 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 if !self.book_has_chapters? ranges.map do |range| min_chapter = Pericope.get_chapter(range.min) min_verse = Pericope.get_verse(range.min) max_chapter = Pericope.get_chapter(range.max) max_verse = Pericope.get_verse(range.max) s = "" if (min_verse==1) and (max_verse>=Pericope.get_max_verse(book, max_chapter)) s << min_chapter.to_s if max_chapter > min_chapter s << "-#{max_chapter}" end 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 |