Class: Search
Defined Under Namespace
Classes: NoSelection
Instance Attribute Summary collapse
-
#all_matches ⇒ Object
readonly
Returns the value of attribute all_matches.
-
#best_matches ⇒ Object
readonly
Returns the value of attribute best_matches.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#original_matches ⇒ Object
readonly
Returns the value of attribute original_matches.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Class Method Summary collapse
Instance Method Summary collapse
- #abort ⇒ Object
- #append_search_string(string) ⇒ Object
- #backspace ⇒ Object
- #clear_query ⇒ Object
- #delete_word ⇒ Object
- #done ⇒ Object
- #done? ⇒ Boolean
- #down ⇒ Object
-
#initialize(vars) ⇒ Search
constructor
A new instance of Search.
- #max_visible_choices ⇒ Object
-
#merge(changes) ⇒ Object
Construct a new Search by merging in a hash of changes.
- #recompute_matches(previous_matches = self.original_matches) ⇒ Object
- #selection ⇒ Object
- #up ⇒ Object
Constructor Details
#initialize(vars) ⇒ Search
Returns a new instance of Search.
204 205 206 207 208 209 210 211 212 213 |
# File 'lib/selecta.rb', line 204 def initialize(vars) @config = vars.fetch(:config) @index = vars.fetch(:index) @query = vars.fetch(:query) @done = vars.fetch(:done) @original_matches = vars.fetch(:original_matches) @all_matches = vars.fetch(:all_matches) @best_matches = vars.fetch(:best_matches) @vars = vars end |
Instance Attribute Details
#all_matches ⇒ Object (readonly)
Returns the value of attribute all_matches.
202 203 204 |
# File 'lib/selecta.rb', line 202 def all_matches @all_matches end |
#best_matches ⇒ Object (readonly)
Returns the value of attribute best_matches.
202 203 204 |
# File 'lib/selecta.rb', line 202 def best_matches @best_matches end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
202 203 204 |
# File 'lib/selecta.rb', line 202 def config @config end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
202 203 204 |
# File 'lib/selecta.rb', line 202 def index @index end |
#original_matches ⇒ Object (readonly)
Returns the value of attribute original_matches.
202 203 204 |
# File 'lib/selecta.rb', line 202 def original_matches @original_matches end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
202 203 204 |
# File 'lib/selecta.rb', line 202 def query @query end |
Class Method Details
.from_config(config) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/selecta.rb', line 215 def self.from_config(config) trivial_matches = config.choices.reject(&:empty?).map do |choice| Match.trivial(choice) end search = new(:config => config, :index => 0, :query => "", :done => false, :original_matches => trivial_matches, :all_matches => trivial_matches, :best_matches => trivial_matches) if config.initial_search.empty? search else search.append_search_string(config.initial_search) end end |
Instance Method Details
#abort ⇒ Object
306 307 308 |
# File 'lib/selecta.rb', line 306 def abort merge(:aborted => true) end |
#append_search_string(string) ⇒ Object
278 279 280 281 282 |
# File 'lib/selecta.rb', line 278 def append_search_string(string) merge(:index => 0, :query => @query + string) .recompute_matches(all_matches) end |
#backspace ⇒ Object
284 285 286 287 288 |
# File 'lib/selecta.rb', line 284 def backspace merge(:index => 0, :query => @query[0...-1]) .recompute_matches end |
#clear_query ⇒ Object
290 291 292 293 294 |
# File 'lib/selecta.rb', line 290 def clear_query merge(:index => 0, :query => "") .recompute_matches end |
#delete_word ⇒ Object
296 297 298 299 300 |
# File 'lib/selecta.rb', line 296 def delete_word merge(:index => 0, :query => @query.sub(/[^ ]* *$/, "")) .recompute_matches end |
#done ⇒ Object
302 303 304 |
# File 'lib/selecta.rb', line 302 def done merge(:done => true) end |
#done? ⇒ Boolean
249 250 251 |
# File 'lib/selecta.rb', line 249 def done? @done end |
#down ⇒ Object
266 267 268 |
# File 'lib/selecta.rb', line 266 def down move_cursor(1) end |
#max_visible_choices ⇒ Object
274 275 276 |
# File 'lib/selecta.rb', line 274 def max_visible_choices [@config.visible_choices, all_matches.count].min end |
#merge(changes) ⇒ Object
Construct a new Search by merging in a hash of changes.
236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/selecta.rb', line 236 def merge(changes) vars = @vars.merge(changes) # If the query changed, throw away the old matches so that new ones will be # computed. matches_are_stale = vars.fetch(:query) != @query if matches_are_stale vars = vars.reject { |key| key == :matches } end Search.new(vars) end |
#recompute_matches(previous_matches = self.original_matches) ⇒ Object
310 311 312 313 314 315 316 317 318 319 |
# File 'lib/selecta.rb', line 310 def recompute_matches(previous_matches=self.original_matches) if self.query.empty? merge(:all_matches => original_matches, :best_matches => original_matches) else all_matches = recompute_all_matches(previous_matches) best_matches = recompute_best_matches(all_matches) merge(:all_matches => all_matches, :best_matches => best_matches) end end |
#selection ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/selecta.rb', line 253 def selection if @aborted NoSelection else match = best_matches.fetch(@index) { NoSelection } if match == NoSelection match else match.original_choice end end end |
#up ⇒ Object
270 271 272 |
# File 'lib/selecta.rb', line 270 def up move_cursor(-1) end |