Class: Engram::Provenance::Span

Inherits:
Object
  • Object
show all
Defined in:
lib/engram/provenance.rb

Constant Summary collapse

OFFSET_UNIT =
"unicode_codepoint"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_offset:, end_offset:, offset_unit: OFFSET_UNIT) ⇒ Span

Returns a new instance of Span.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/engram/provenance.rb', line 20

def initialize(start_offset:, end_offset:, offset_unit: OFFSET_UNIT)
  unless start_offset.is_a?(Integer) && end_offset.is_a?(Integer) &&
      start_offset >= 0 && end_offset > start_offset
    raise ArgumentError, "span offsets must be non-negative integers with end_offset > start_offset"
  end
  raise ArgumentError, "unsupported offset unit" unless offset_unit.to_s == OFFSET_UNIT

  @start_offset = start_offset
  @end_offset = end_offset
  @offset_unit = OFFSET_UNIT
  freeze
end

Instance Attribute Details

#end_offsetObject (readonly)

Returns the value of attribute end_offset.



18
19
20
# File 'lib/engram/provenance.rb', line 18

def end_offset
  @end_offset
end

#offset_unitObject (readonly)

Returns the value of attribute offset_unit.



18
19
20
# File 'lib/engram/provenance.rb', line 18

def offset_unit
  @offset_unit
end

#start_offsetObject (readonly)

Returns the value of attribute start_offset.



18
19
20
# File 'lib/engram/provenance.rb', line 18

def start_offset
  @start_offset
end

Instance Method Details

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



35
36
37
# File 'lib/engram/provenance.rb', line 35

def ==(other)
  other.is_a?(self.class) && to_h == other.to_h
end

#end_exclusive?Boolean

Returns:

  • (Boolean)


33
# File 'lib/engram/provenance.rb', line 33

def end_exclusive? = true

#hashObject



40
# File 'lib/engram/provenance.rb', line 40

def hash = to_h.hash

#to_hObject



42
43
44
# File 'lib/engram/provenance.rb', line 42

def to_h
  {"start_offset" => start_offset, "end_offset" => end_offset, "offset_unit" => offset_unit}
end