Class: AnyStyle::Feature::Position

Inherits:
AnyStyle::Feature show all
Defined in:
lib/anystyle/feature/position.rb

Instance Attribute Summary collapse

Attributes inherited from AnyStyle::Feature

#precision

Instance Method Summary collapse

Methods inherited from AnyStyle::Feature

#next, #prev, #ratio

Methods included from StringUtils

canonize, count, display_chars, display_width, indent, nnum, page_break?, scrub, strip_html, transliterate

Constructor Details

#initialize(idx: :idx, seq: :seq, **opts) ⇒ Position

Returns a new instance of Position.


6
7
8
9
# File 'lib/anystyle/feature/position.rb', line 6

def initialize(idx: :idx, seq: :seq, **opts)
  super(**opts)
  @idx, @seq = idx, seq
end

Instance Attribute Details

#idxObject (readonly)

Returns the value of attribute idx


4
5
6
# File 'lib/anystyle/feature/position.rb', line 4

def idx
  @idx
end

#seqObject (readonly)

Returns the value of attribute seq


4
5
6
# File 'lib/anystyle/feature/position.rb', line 4

def seq
  @seq
end

Instance Method Details

#observe(token, **opts) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/anystyle/feature/position.rb', line 11

def observe(token, **opts)
  i = opts[idx]
  n = opts[seq].size

  case
  when i == 0 && i == n - 1
    :only
  when i == 0
    :first
  when i == n - 1
    :last
  else
    ratio i, n
  end
end