Class: AnyStyle::Feature::Affix

Inherits:
AnyStyle::Feature show all
Defined in:
lib/anystyle/feature/affix.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(size: 4, prefix: true, suffix: false) ⇒ Affix

Returns a new instance of Affix.



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

def initialize(size: 4, prefix: true, suffix: false)
  @size, @suffix = size, (suffix || !prefix)
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

Instance Method Details

#build(chars) ⇒ Object



30
31
32
# File 'lib/anystyle/feature/affix.rb', line 30

def build(chars)
  (1..size).map { |n| yield chars.take(n) }
end

#extract(token) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/anystyle/feature/affix.rb', line 14

def extract(token)
  if suffix?
    token.chars.reverse.take(size)
  else
    token.chars.take(size)
  end
end

#join(chars) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/anystyle/feature/affix.rb', line 22

def join(chars)
  if suffix?
    chars.reverse.join('')
  else
    chars.join('')
  end
end

#observe(token, **opts) ⇒ Object



10
11
12
# File 'lib/anystyle/feature/affix.rb', line 10

def observe(token, **opts)
  build(extract(token)) { |chars| join(chars) }
end

#prefix?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/anystyle/feature/affix.rb', line 38

def prefix?
  !suffix?
end

#suffix?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/anystyle/feature/affix.rb', line 34

def suffix?
  !!@suffix
end