Class: Scripref::Passage
- Inherits:
-
Struct
- Object
- Struct
- Scripref::Passage
- Includes:
- Comparable
- Defined in:
- lib/scripref/passage.rb
Instance Attribute Summary collapse
-
#a1 ⇒ Object
Returns the value of attribute a1.
-
#a2 ⇒ Object
Returns the value of attribute a2.
-
#b1 ⇒ Object
Returns the value of attribute b1.
-
#b2 ⇒ Object
Returns the value of attribute b2.
-
#c1 ⇒ Object
Returns the value of attribute c1.
-
#c2 ⇒ Object
Returns the value of attribute c2.
-
#text ⇒ Object
(also: #to_s)
Returns the value of attribute text.
-
#v1 ⇒ Object
Returns the value of attribute v1.
-
#v2 ⇒ Object
Returns the value of attribute v2.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=>(other) ⇒ Object
-
#comparable? ⇒ Boolean
Checks if the instance is comparable, that means all values are numeric and so the <=> method can be applied.
-
#end ⇒ Object
Returns an array of b2, c2, v2.
-
#make_comparable(max: 999, ff: 3) ⇒ Object
Returns a copy which is comparable, that means all values are numeric.
-
#make_comparable!(max: 999, ff: 3) ⇒ Object
Makes the Passage instance comparable, that means all values are numeric.
-
#overlap?(passage) ⇒ Boolean
Returns true if the instance and the given passage overlap.
-
#start ⇒ Object
Returns an array of b1, c1, v1.
- #to_a ⇒ Object
Instance Attribute Details
#a1 ⇒ Object
Returns the value of attribute a1
4 5 6 |
# File 'lib/scripref/passage.rb', line 4 def a1 @a1 end |
#a2 ⇒ Object
Returns the value of attribute a2
4 5 6 |
# File 'lib/scripref/passage.rb', line 4 def a2 @a2 end |
#b1 ⇒ Object
Returns the value of attribute b1
4 5 6 |
# File 'lib/scripref/passage.rb', line 4 def b1 @b1 end |
#b2 ⇒ Object
Returns the value of attribute b2
4 5 6 |
# File 'lib/scripref/passage.rb', line 4 def b2 @b2 end |
#c1 ⇒ Object
Returns the value of attribute c1
4 5 6 |
# File 'lib/scripref/passage.rb', line 4 def c1 @c1 end |
#c2 ⇒ Object
Returns the value of attribute c2
4 5 6 |
# File 'lib/scripref/passage.rb', line 4 def c2 @c2 end |
#text ⇒ Object Also known as: to_s
Returns the value of attribute text
4 5 6 |
# File 'lib/scripref/passage.rb', line 4 def text @text end |
#v1 ⇒ Object
Returns the value of attribute v1
4 5 6 |
# File 'lib/scripref/passage.rb', line 4 def v1 @v1 end |
#v2 ⇒ Object
Returns the value of attribute 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.
54 55 56 |
# File 'lib/scripref/passage.rb', line 54 def comparable? to_a.map {|e| Numeric === e}.uniq == [true] end |
#end ⇒ Object
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.
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 |
#start ⇒ Object
Returns an array of b1, c1, v1
68 69 70 |
# File 'lib/scripref/passage.rb', line 68 def start [b1, c1, v1] end |
#to_a ⇒ Object
16 17 18 |
# File 'lib/scripref/passage.rb', line 16 def to_a [b1, c1, v1, b2, c2, v2] end |