Class: Scripref::Passage

Inherits:
Struct
  • Object
show all
Includes:
Comparable
Defined in:
lib/scripref/passage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#a1Object

Returns the value of attribute a1

Returns:

  • (Object)

    the current value of a1



4
5
6
# File 'lib/scripref/passage.rb', line 4

def a1
  @a1
end

#a2Object

Returns the value of attribute a2

Returns:

  • (Object)

    the current value of a2



4
5
6
# File 'lib/scripref/passage.rb', line 4

def a2
  @a2
end

#b1Object

Returns the value of attribute b1

Returns:

  • (Object)

    the current value of b1



4
5
6
# File 'lib/scripref/passage.rb', line 4

def b1
  @b1
end

#b2Object

Returns the value of attribute b2

Returns:

  • (Object)

    the current value of b2



4
5
6
# File 'lib/scripref/passage.rb', line 4

def b2
  @b2
end

#c1Object

Returns the value of attribute c1

Returns:

  • (Object)

    the current value of c1



4
5
6
# File 'lib/scripref/passage.rb', line 4

def c1
  @c1
end

#c2Object

Returns the value of attribute c2

Returns:

  • (Object)

    the current value of c2



4
5
6
# File 'lib/scripref/passage.rb', line 4

def c2
  @c2
end

#textObject Also known as: to_s

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



4
5
6
# File 'lib/scripref/passage.rb', line 4

def text
  @text
end

#v1Object

Returns the value of attribute v1

Returns:

  • (Object)

    the current value of v1



4
5
6
# File 'lib/scripref/passage.rb', line 4

def v1
  @v1
end

#v2Object

Returns the value of attribute v2

Returns:

  • (Object)

    the current value of v2



4
5
6
# File 'lib/scripref/passage.rb', line 4

def v2
  @v2
end

Instance Method Details

#+(other) ⇒ Object



8
9
10
# File 'lib/scripref/passage.rb', line 8

def + other
  to_a.zip(other.to_a).map {|a, b| a + b}
end

#-(other) ⇒ Object



12
13
14
# File 'lib/scripref/passage.rb', line 12

def - other
  to_a.zip(other.to_a).map {|a, b| b - a}
end

#<=>(other) ⇒ Object



20
21
22
23
# File 'lib/scripref/passage.rb', line 20

def <=> other
  return unless other.kind_of? Passage
  self.make_comparable.to_a <=> other.make_comparable.to_a
end

#comparable?Boolean

Checks if the instance is comparable, that means all values are numeric and so the <=> method can be applied.

Returns:

  • (Boolean)


54
55
56
# File 'lib/scripref/passage.rb', line 54

def comparable?
  to_a.map {|e| Numeric === e}.uniq == [true]
end

#endObject

Returns an array of b2, c2, v2



73
74
75
# File 'lib/scripref/passage.rb', line 73

def end
  [b2, c2, v2]
end

#make_comparable(max: 999, ff: 3) ⇒ Object

Returns a copy which is comparable, that means all values are numeric. This is a heuristic approach.



28
29
30
# File 'lib/scripref/passage.rb', line 28

def make_comparable max: 999, ff: 3
  self.dup.make_comparable! max: max, ff: ff
end

#make_comparable!(max: 999, ff: 3) ⇒ Object

Makes the Passage instance comparable, that means all values are numeric. This is a heuristic approach.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/scripref/passage.rb', line 35

def make_comparable! max: 999, ff: 3
  self.b1 ||= 1
  self.c1 ||= 1
  self.v1 ||= 1
  self.b2 ||= max
  self.c2 ||= max
  self.v2 ||= max
  if self.v2 == :f
    self.v2 = self.v1 + 1
  end
  if self.v2 == :ff
    self.v2 = self.v1 + ff
  end
  self
end

#overlap?(passage) ⇒ Boolean

Returns true if the instance and the given passage overlap. That means both has at least one verse in common.

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/scripref/passage.rb', line 60

def overlap? passage
  fail ArgumentError, 'value must be a passage' unless passage.kind_of? Passage
  a = self.make_comparable.to_a
  b = passage.make_comparable.to_a
  [a[0..2] <=> b[3..5], b[0..2] <=> a[3..5]].max < 1
end

#startObject

Returns an array of b1, c1, v1



68
69
70
# File 'lib/scripref/passage.rb', line 68

def start
  [b1, c1, v1]
end

#to_aObject



16
17
18
# File 'lib/scripref/passage.rb', line 16

def to_a
  [b1, c1, v1, b2, c2, v2]
end