Class: HintableLevenshtein::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/hintable_levenshtein/rule_set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#matcherObject (readonly)

Returns the value of attribute matcher.



38
39
40
# File 'lib/hintable_levenshtein/rule_set.rb', line 38

def matcher
  @matcher
end

#typeObject (readonly)

Returns the value of attribute type.



38
39
40
# File 'lib/hintable_levenshtein/rule_set.rb', line 38

def type
  @type
end

Class Method Details

.delete(matcher = nil) ⇒ Object



26
27
28
# File 'lib/hintable_levenshtein/rule_set.rb', line 26

def self.delete(matcher = nil)
  new(:delete, matcher)
end

.insert(matcher = nil) ⇒ Object



30
31
32
# File 'lib/hintable_levenshtein/rule_set.rb', line 30

def self.insert(matcher = nil)
  new(:insert, matcher)
end

.substitute(matcher = {}) ⇒ Object



34
35
36
# File 'lib/hintable_levenshtein/rule_set.rb', line 34

def self.substitute(matcher = {})
  new(:substitute, matcher)
end

Instance Method Details

#match?(event) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hintable_levenshtein/rule_set.rb', line 40

def match?(event)
  if event.type == type
    case type
    when :delete, :insert
      ret = case matcher
      when nil
        true
      when String
        !matcher.index(event.chr).nil?
      when Array
        matcher.include?(event.chr)
      else
        matcher === event.chr
      end
    when :substitute
      matcher.empty? || matcher[event.from] == event.to || matcher[event.to] == event.from
    end
  end
end

#to_sObject



60
61
62
# File 'lib/hintable_levenshtein/rule_set.rb', line 60

def to_s
  "#{type} #{matcher.inspect}"
end