Class: Spellr::Wordlist

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/spellr.rb,
lib/spellr/wordlist.rb

Defined Under Namespace

Classes: NotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, name: file) ⇒ Wordlist

Returns a new instance of Wordlist.



13
14
15
16
17
# File 'lib/spellr/wordlist.rb', line 13

def initialize(file, name: file)
  path = @file = file
  @path = Pathname.pwd.join('.spellr_wordlists').join(path).expand_path
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/spellr/wordlist.rb', line 11

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/spellr/wordlist.rb', line 11

def path
  @path
end

Instance Method Details

#<<(term) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/spellr/wordlist.rb', line 35

def <<(term)
  term = term.spellr_normalize
  touch
  include_cache[term] = true
  insert_sorted(term)
  @path.write(to_a.join) # we don't need to clear the cache
end

#clean(file = @path) ⇒ Object



47
48
49
50
# File 'lib/spellr/wordlist.rb', line 47

def clean(file = @path)
  require_relative 'tokenizer'
  write(Spellr::Tokenizer.new(file, skip_key: false).normalized_terms.join)
end

#each(&block) ⇒ Object



19
20
21
22
23
# File 'lib/spellr/wordlist.rb', line 19

def each(&block)
  raise_unless_exists?

  @path.each_line(&block)
end

#exist?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/spellr/wordlist.rb', line 64

def exist?
  return @exist if defined?(@exist)

  @exist = @path.exist?
end

#include?(term) ⇒ Boolean

significantly faster than default Enumerable#include? requires terms to have been sorted

Returns:

  • (Boolean)


31
32
33
# File 'lib/spellr/wordlist.rb', line 31

def include?(term)
  include_cache[term.spellr_normalize]
end

#inspectObject



25
26
27
# File 'lib/spellr/wordlist.rb', line 25

def inspect
  "#<#{self.class.name}:#{@path}>"
end

#readObject



58
59
60
61
62
# File 'lib/spellr/wordlist.rb', line 58

def read
  raise_unless_exists?

  @path.read
end

#to_aObject



43
44
45
# File 'lib/spellr/wordlist.rb', line 43

def to_a
  @to_a ||= super
end

#touchObject



70
71
72
73
74
75
76
# File 'lib/spellr/wordlist.rb', line 70

def touch
  return if exist?

  @path.dirname.mkpath
  @path.write('')
  clear_cache
end

#write(content) ⇒ Object



52
53
54
55
56
# File 'lib/spellr/wordlist.rb', line 52

def write(content)
  @path.write(content)

  clear_cache
end