Class: Pericope::Range

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pericope/range.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first, last) ⇒ Range

Returns a new instance of Range.



5
6
7
8
# File 'lib/pericope/range.rb', line 5

def initialize(first, last)
  @begin = first
  @end = last
end

Instance Attribute Details

#beginObject (readonly)

Returns the value of attribute begin.



10
11
12
# File 'lib/pericope/range.rb', line 10

def begin
  @begin
end

#endObject (readonly)

Returns the value of attribute end.



11
12
13
# File 'lib/pericope/range.rb', line 11

def end
  @end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



13
14
15
16
17
18
19
# File 'lib/pericope/range.rb', line 13

def ==(other)
  return true if equal? other

  other.kind_of?(Pericope::Range) and
    self.begin == other.begin and
    self.end == other.end
end

#eachObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pericope/range.rb', line 27

def each
  return to_enum unless block_given?

  if self.begin == self.end
    yield self.begin
    return self
  end

  current = self.begin
  last_verse = self.end.whole
  while current < last_verse
    yield current
    current = current.succ
  end

  if self.end.partial?
    "a".upto(self.end.letter).each do |letter|
      yield Verse.new(self.end.book, self.end.chapter, self.end.verse, letter)
    end
  else
    yield self.end
  end

  self
end

#hashObject



23
24
25
# File 'lib/pericope/range.rb', line 23

def hash
  self.begin.hash ^ self.end.hash
end

#inspectObject



53
54
55
# File 'lib/pericope/range.rb', line 53

def inspect
  "#{self.begin.to_id}..#{self.end.to_id}"
end