Class: Doublespeak::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/doublespeak/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_source, options = {}) ⇒ Core

Returns a new instance of Core.



6
7
8
9
10
11
12
13
14
15
# File 'lib/doublespeak/core.rb', line 6

def initialize(data_source, options = {})
  @data_source = data_source
  @format_data = options[:format_data] || ->(x) { "#{x}" }

  @display = options[:display] || Display.new(options)

  @query = options[:query] || ""
  @selected_index = options[:selected_index] || 0
  @saved_candidates = options[:saved_candidates] || []
end

Instance Attribute Details

#data_sourceObject (readonly)

Returns the value of attribute data_source.



3
4
5
# File 'lib/doublespeak/core.rb', line 3

def data_source
  @data_source
end

#displayObject (readonly)

Returns the value of attribute display.



3
4
5
# File 'lib/doublespeak/core.rb', line 3

def display
  @display
end

#format_dataObject (readonly)

Returns the value of attribute format_data.



3
4
5
# File 'lib/doublespeak/core.rb', line 3

def format_data
  @format_data
end

#queryObject (readonly)

Returns the value of attribute query.



4
5
6
# File 'lib/doublespeak/core.rb', line 4

def query
  @query
end

#saved_candidatesObject (readonly)

Returns the value of attribute saved_candidates.



4
5
6
# File 'lib/doublespeak/core.rb', line 4

def saved_candidates
  @saved_candidates
end

#selected_indexObject (readonly)

Returns the value of attribute selected_index.



4
5
6
# File 'lib/doublespeak/core.rb', line 4

def selected_index
  @selected_index
end

Instance Method Details

#back_upObject



46
47
48
# File 'lib/doublespeak/core.rb', line 46

def back_up
  query.chop!
end

#entry(c) ⇒ Object



55
56
57
58
59
# File 'lib/doublespeak/core.rb', line 55

def entry(c)
  if query.length < display.width
    query.concat(c)
  end
end

#find_candidatesObject



50
51
52
53
# File 'lib/doublespeak/core.rb', line 50

def find_candidates
  @selected_index = 0
  @saved_candidates = query.strip.to_s.empty? ? [] : data_source.call(query)
end

#finish_upObject



26
27
28
29
30
31
32
33
# File 'lib/doublespeak/core.rb', line 26

def finish_up
  display.move_to_origin
  display.clear_to_end_of_line
  display.set_cursor_visible

  display.write display.format_selected_result.call(format_data.call(candidate)) unless candidate.nil?
  display.write "\n"
end

#increment_selection(i) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/doublespeak/core.rb', line 35

def increment_selection(i)
  @selected_index = selected_index + i
  if saved_candidates.size == 0
    @selected_index = 0
  elsif selected_index < 0
    @selected_index = saved_candidates.size - 1
  elsif selected_index >= saved_candidates.size
    @selected_index = 0
  end
end

#renderObject



17
18
19
20
21
22
23
24
# File 'lib/doublespeak/core.rb', line 17

def render
  display.move_to_origin
  display.clear_to_end_of_line

  display.write (query + status_line)

  display.move_to_origin
end