Class: Spellr::Wordlist
- Inherits:
-
Object
- Object
- Spellr::Wordlist
- Includes:
- Enumerable
- Defined in:
- lib/spellr.rb,
lib/spellr/wordlist.rb
Defined Under Namespace
Classes: NotFound
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #<<(term) ⇒ Object
- #clean(file = @path) ⇒ Object
- #each(&block) ⇒ Object
- #exist? ⇒ Boolean
-
#include?(term) ⇒ Boolean
significantly faster than default Enumerable#include? requires terms to have been sorted.
-
#initialize(file, name: file) ⇒ Wordlist
constructor
A new instance of Wordlist.
- #inspect ⇒ Object
- #read ⇒ Object
- #touch ⇒ Object
- #words ⇒ Object (also: #to_a)
- #write(content) ⇒ Object
Constructor Details
#initialize(file, name: file) ⇒ Wordlist
Returns a new instance of Wordlist.
13 14 15 16 17 18 |
# File 'lib/spellr/wordlist.rb', line 13 def initialize(file, name: file) path = @file = file @path = Pathname.pwd.join('.spellr_wordlists').join(path). @name = name @include = {} end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/spellr/wordlist.rb', line 11 def name @name end |
#path ⇒ Object (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
39 40 41 42 43 44 45 |
# File 'lib/spellr/wordlist.rb', line 39 def <<(term) term = term.spellr_normalize touch @include[term] = true insert_sorted(term) @path.write(words.join) # we don't need to clear the cache end |
#clean(file = @path) ⇒ Object
52 53 54 55 |
# File 'lib/spellr/wordlist.rb', line 52 def clean(file = @path) require_relative 'tokenizer' write(Spellr::Tokenizer.new(file, skip_key: false).normalized_terms.join) end |
#each(&block) ⇒ Object
20 21 22 23 24 |
# File 'lib/spellr/wordlist.rb', line 20 def each(&block) raise_unless_exists? @path.each_line(&block) end |
#exist? ⇒ Boolean
69 70 71 72 73 |
# File 'lib/spellr/wordlist.rb', line 69 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
32 33 34 35 36 37 |
# File 'lib/spellr/wordlist.rb', line 32 def include?(term) term = term.spellr_normalize @include.fetch(term) do @include[term] = words.bsearch { |value| term <=> value } end end |
#inspect ⇒ Object
26 27 28 |
# File 'lib/spellr/wordlist.rb', line 26 def inspect "#<#{self.class.name}:#{@path}>" end |
#read ⇒ Object
63 64 65 66 67 |
# File 'lib/spellr/wordlist.rb', line 63 def read raise_unless_exists? @path.read end |
#touch ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/spellr/wordlist.rb', line 75 def touch return if exist? @path.dirname.mkpath @path.write('') clear_cache end |
#words ⇒ Object Also known as: to_a
47 48 49 |
# File 'lib/spellr/wordlist.rb', line 47 def words @words ||= (exist? ? @path.readlines : []) end |
#write(content) ⇒ Object
57 58 59 60 61 |
# File 'lib/spellr/wordlist.rb', line 57 def write(content) @path.write(content) clear_cache end |