Class: FortuneTeller::Chooser

Inherits:
Object
  • Object
show all
Defined in:
lib/fortune_teller/chooser.rb

Constant Summary collapse

LOCATIONS =
[ :tl, :tr, :bl, :br ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_tl_panel, _tr_panel, options = {}) ⇒ Chooser

Returns a new instance of Chooser.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fortune_teller/chooser.rb', line 25

def initialize(_tl_panel, _tr_panel, options = {})
  @ui = options[:ui]
  @tl_panel = _tl_panel
  Logger.log "tl_panel: #{@tl_panel}"
  @tr_panel = _tr_panel
  Logger.log "tr_panel: #{@tr_panel}"
  @bl_panel = options[:bl_panel] || NilPanel.new(@ui)
  Logger.log "bl_panel: #{@bl_panel}"
  @br_panel = options[:br_panel] || NilPanel.new(@ui)
  Logger.log "br_panel: #{@br_panel}"
  @list_renderer = options[:list_renderer] || ListRenderer.new
  define_selection_aliases
end

Instance Attribute Details

#bl_panelObject (readonly)

Returns the value of attribute bl_panel.



23
24
25
# File 'lib/fortune_teller/chooser.rb', line 23

def bl_panel
  @bl_panel
end

#br_panelObject (readonly)

Returns the value of attribute br_panel.



23
24
25
# File 'lib/fortune_teller/chooser.rb', line 23

def br_panel
  @br_panel
end

#list_rendererObject (readonly)

Returns the value of attribute list_renderer.



24
25
26
# File 'lib/fortune_teller/chooser.rb', line 24

def list_renderer
  @list_renderer
end

#tl_panelObject (readonly)

Returns the value of attribute tl_panel.



23
24
25
# File 'lib/fortune_teller/chooser.rb', line 23

def tl_panel
  @tl_panel
end

#tr_panelObject (readonly)

Returns the value of attribute tr_panel.



23
24
25
# File 'lib/fortune_teller/chooser.rb', line 23

def tr_panel
  @tr_panel
end

#uiObject (readonly)

Returns the value of attribute ui.



24
25
26
# File 'lib/fortune_teller/chooser.rb', line 24

def ui
  @ui
end

Class Method Details

.random_choice(choices) ⇒ Object



19
20
21
# File 'lib/fortune_teller/chooser.rb', line 19

def self.random_choice(choices)
  choices[rand(choices.size)]
end

.reduce_choices!(choices, by = random_choice(choices)) ⇒ Object



15
16
17
# File 'lib/fortune_teller/chooser.rb', line 15

def self.reduce_choices!( choices, by=random_choice(choices) )
  choices.delete(by)
end

.subset(list, num = list.size) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/fortune_teller/chooser.rb', line 7

def self.subset(list, num=list.size)
  Logger.log("got list: #{list.inspect}, num: #{num.inspect}")
  choices = list.dup
  num.times.collect {|num|
    reduce_choices!(choices)
  }
end

Instance Method Details

#chooseObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fortune_teller/chooser.rb', line 70

def choose
  choice = nil
  begin
    ui.clear_screen
    choice = ui.prompt( list_renderer.render( selections | [:exit] ) )
    Logger.log("got: #{choice.inspect}")
  end until( valid?( choice ) )
  Logger.log("trying it...")
  ui.clear_screen(1)
  # need to unalias methods...
  send( choice )
end

#define_selection_aliasesObject



52
53
54
55
56
# File 'lib/fortune_teller/chooser.rb', line 52

def define_selection_aliases
  selections_hash.each_pair do |k,v|
    self.class.send(:alias_method, v.to_s.to_sym, k)
  end
end

#exitObject Also known as: Q, q



62
63
64
65
66
# File 'lib/fortune_teller/chooser.rb', line 62

def exit
  yield if block_given?
  Logger.log "exiting..."
  Kernel.exit
end

#selectionsObject



48
49
50
# File 'lib/fortune_teller/chooser.rb', line 48

def selections
  selections_hash.values
end

#selections_hashObject



39
40
41
42
43
44
45
46
# File 'lib/fortune_teller/chooser.rb', line 39

def selections_hash
  LOCATIONS.reduce({}) {|memo, location|
    if (value = send( "#{location}_panel" )).present?
      memo[location] = value
    end
    memo
  }
end

#valid?(choice) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/fortune_teller/chooser.rb', line 58

def valid? choice
  respond_to? choice
end