Class: Wordlist::Builder
- Inherits:
-
Object
- Object
- Wordlist::Builder
- Defined in:
- lib/wordlist/builder.rb
Overview
Parses text and builds a wordlist file.
Instance Attribute Summary collapse
-
#format ⇒ :txt, ...
readonly
The format of the wordlist file.
-
#lexer ⇒ Lexer
readonly
The word lexer.
-
#path ⇒ String
readonly
Path of the wordlist.
-
#unique_filter ⇒ UniqueFilter
readonly
The unique filter.
Class Method Summary collapse
-
.open(path, **kwargs) {|builder| ... } ⇒ Builder
Creates a new Builder object with the given arguments, opens the word-list file, passes the builder object to the given block then finally closes the word-list file.
Instance Method Summary collapse
-
#add(word) ⇒ self
(also: #<<, #push)
Appends the given word to the wordlist file, only if it has not been previously added.
-
#append(words) ⇒ self
(also: #concat)
Add the given words to the word-list.
-
#append? ⇒ Boolean
Determines if the builder will append new words to the existing wordlist or overwrite it.
-
#close ⇒ Object
Closes the word-list file.
-
#closed? ⇒ Boolean
Indicates whether the wordlist builder has been closed.
-
#comment(message) ⇒ Object
Writes a comment line to the wordlist file.
-
#initialize(path, format: Format.infer(path), append: false, **kwargs) ⇒ Builder
constructor
Creates a new word-list Builder object.
-
#parse(text) ⇒ Object
Parses the given text, adding each unique word to the word-list file.
-
#parse_file(path) ⇒ Object
Parses the contents of the file at the given path, adding each unique word to the word-list file.
Constructor Details
#initialize(path, format: Format.infer(path), append: false, **kwargs) ⇒ Builder
Creates a new word-list Builder object.
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/wordlist/builder.rb', line 90 def initialize(path, format: Format.infer(path), append: false, **kwargs) @path = ::File.(path) @format = format @append = append @lexer = Lexer.new(**kwargs) @unique_filter = UniqueFilter.new load! if append? && ::File.file?(@path) open! end |
Instance Attribute Details
#format ⇒ :txt, ... (readonly)
The format of the wordlist file.
26 27 28 |
# File 'lib/wordlist/builder.rb', line 26 def format @format end |
#lexer ⇒ Lexer (readonly)
The word lexer.
31 32 33 |
# File 'lib/wordlist/builder.rb', line 31 def lexer @lexer end |
#path ⇒ String (readonly)
Path of the wordlist
21 22 23 |
# File 'lib/wordlist/builder.rb', line 21 def path @path end |
#unique_filter ⇒ UniqueFilter (readonly)
The unique filter.
36 37 38 |
# File 'lib/wordlist/builder.rb', line 36 def unique_filter @unique_filter end |
Class Method Details
.open(path, **kwargs) {|builder| ... } ⇒ Builder
Creates a new Builder object with the given arguments, opens the word-list file, passes the builder object to the given block then finally closes the word-list file.
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/wordlist/builder.rb', line 124 def self.open(path,**kwargs) builder = new(path,**kwargs) if block_given? begin yield builder ensure builder.close end end return builder end |
Instance Method Details
#add(word) ⇒ self Also known as: <<, push
Appends the given word to the wordlist file, only if it has not been previously added.
168 169 170 171 172 173 174 |
# File 'lib/wordlist/builder.rb', line 168 def add(word) if @unique_filter.add?(word) write(word) end return self end |
#append(words) ⇒ self Also known as: concat
Add the given words to the word-list.
188 189 190 191 |
# File 'lib/wordlist/builder.rb', line 188 def append(words) words.each { |word| add(word) } return self end |
#append? ⇒ Boolean
Determines if the builder will append new words to the existing wordlist or overwrite it.
144 145 146 |
# File 'lib/wordlist/builder.rb', line 144 def append? @append end |
#close ⇒ Object
Closes the word-list file.
225 226 227 228 229 230 |
# File 'lib/wordlist/builder.rb', line 225 def close unless @io.closed? @io.close @unique_filter.clear end end |
#closed? ⇒ Boolean
Indicates whether the wordlist builder has been closed.
237 238 239 |
# File 'lib/wordlist/builder.rb', line 237 def closed? @io.closed? end |
#comment(message) ⇒ Object
Writes a comment line to the wordlist file.
154 155 156 |
# File 'lib/wordlist/builder.rb', line 154 def comment() write("# #{}") end |
#parse(text) ⇒ Object
Parses the given text, adding each unique word to the word-list file.
201 202 203 204 205 |
# File 'lib/wordlist/builder.rb', line 201 def parse(text) @lexer.parse(text) do |word| add(word) end end |
#parse_file(path) ⇒ Object
Parses the contents of the file at the given path, adding each unique word to the word-list file.
214 215 216 217 218 219 220 |
# File 'lib/wordlist/builder.rb', line 214 def parse_file(path) ::File.open(path) do |file| file.each_line do |line| parse(line) end end end |