Class: AutoCorrect::Strategery

Inherits:
Object
  • Object
show all
Defined in:
lib/auto-correct/strategery.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(one, other, space: false, reverse: false) ⇒ Strategery

Returns a new instance of Strategery.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/auto-correct/strategery.rb', line 6

def initialize(one, other, space: false, reverse: false)
  @space = space
  @reverse = reverse

  @add_space_rules = [
    /(#{one})(#{other})/u,
    /(#{other})(#{one})/u
  ]

  @remove_space_rules = [
    /(#{one})#{SPACE}+(#{other})/u,
    /(#{other})#{SPACE}+(#{one})/u
  ]
end

Instance Attribute Details

#add_space_rulesObject (readonly)

Returns the value of attribute add_space_rules.



4
5
6
# File 'lib/auto-correct/strategery.rb', line 4

def add_space_rules
  @add_space_rules
end

#remove_space_rulesObject (readonly)

Returns the value of attribute remove_space_rules.



4
5
6
# File 'lib/auto-correct/strategery.rb', line 4

def remove_space_rules
  @remove_space_rules
end

#reverseObject (readonly)

Returns the value of attribute reverse.



3
4
5
# File 'lib/auto-correct/strategery.rb', line 3

def reverse
  @reverse
end

#spaceObject (readonly)

Returns the value of attribute space.



3
4
5
# File 'lib/auto-correct/strategery.rb', line 3

def space
  @space
end

Instance Method Details

#add_space(str) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/auto-correct/strategery.rb', line 25

def add_space(str)
  r0, r1 = add_space_rules
  str = str.gsub(r0) { "#$1 #$2" }
  if self.reverse
    str = str.gsub(r1) { "#$1 #$2" }
  end
  str
end

#format(str) ⇒ Object



21
22
23
# File 'lib/auto-correct/strategery.rb', line 21

def format(str)
  self.space ? add_space(str) : remove_space(str)
end

#remove_space(str) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/auto-correct/strategery.rb', line 34

def remove_space(str)
  r0, r1 = remove_space_rules
  str = str.gsub(r0) { "#$1 #$2" }
  if self.reverse
    str = str.gsub(r1) { "#$1 #$2" }
  end
  str
end