Class: Verser::Reference

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Reference

Returns a new instance of Reference.



7
8
9
10
# File 'lib/verser/reference.rb', line 7

def initialize(input)
  @input = input
  parse
end

Instance Attribute Details

#bookObject (readonly)

Returns the value of attribute book.



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

def book
  @book
end

#from_chapterObject (readonly)

Returns the value of attribute from_chapter.



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

def from_chapter
  @from_chapter
end

#from_verseObject (readonly)

Returns the value of attribute from_verse.



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

def from_verse
  @from_verse
end

#inputObject (readonly)

Returns the value of attribute input.



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

def input
  @input
end

#to_chapterObject (readonly)

Returns the value of attribute to_chapter.



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

def to_chapter
  @to_chapter
end

#to_verseObject (readonly)

Returns the value of attribute to_verse.



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

def to_verse
  @to_verse
end

Instance Method Details

#parseObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/verser/reference.rb', line 16

def parse
  @input.downcase.gsub(/\s/,'').match /(\d)?([a-zéèïëô]+)\s*(\d+)([.,:](\d+)-?((\d+)[.,:])?(\d+)?)?/

  @book = $1.nil? ? $2 : $1+$2
  @from_chapter = $3.to_i == 0 ? nil : $3.to_i
  @to_chapter = $7.to_i == 0 ? nil : $7.to_i
  
  if $5.to_i > 0
    @from_verse = $5.to_i
    @to_verse = $8.to_i > 0 ? $8.to_i : nil
  else
    @from_verse = @to_verse = nil
  end

  validate!
end

#valid?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/verser/reference.rb', line 12

def valid?
  @valid ||= false
end

#validate!Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/verser/reference.rb', line 33

def validate!
  @valid = true
  @valid = false if @book.nil? or @book.empty?
  @valid = false if @from_chapter.nil?

  if @to_chapter.nil?
    unless @from_verse.nil? or @to_verse.nil?
      @valid &&= (@from_verse < @to_verse)
    end
  end
  

  @valid
end