Class: FortuneTeller::Chooser
- Inherits:
-
Object
- Object
- FortuneTeller::Chooser
- Defined in:
- lib/fortune_teller/chooser.rb
Constant Summary collapse
- LOCATIONS =
[ :tl, :tr, :bl, :br ]
Instance Attribute Summary collapse
-
#bl_panel ⇒ Object
readonly
Returns the value of attribute bl_panel.
-
#br_panel ⇒ Object
readonly
Returns the value of attribute br_panel.
-
#list_renderer ⇒ Object
readonly
Returns the value of attribute list_renderer.
-
#tl_panel ⇒ Object
readonly
Returns the value of attribute tl_panel.
-
#tr_panel ⇒ Object
readonly
Returns the value of attribute tr_panel.
-
#ui ⇒ Object
readonly
Returns the value of attribute ui.
Class Method Summary collapse
- .random_choice(choices) ⇒ Object
- .reduce_choices!(choices, by = random_choice(choices)) ⇒ Object
- .subset(list, num = list.size) ⇒ Object
Instance Method Summary collapse
- #choose ⇒ Object
- #define_selection_aliases ⇒ Object
- #exit ⇒ Object (also: #Q, #q)
-
#initialize(_tl_panel, _tr_panel, options = {}) ⇒ Chooser
constructor
A new instance of Chooser.
- #selections ⇒ Object
- #selections_hash ⇒ Object
- #valid?(choice) ⇒ Boolean
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, = {}) @ui = [:ui] @tl_panel = _tl_panel Logger.log "tl_panel: #{@tl_panel}" @tr_panel = _tr_panel Logger.log "tr_panel: #{@tr_panel}" @bl_panel = [:bl_panel] || NilPanel.new(@ui) Logger.log "bl_panel: #{@bl_panel}" @br_panel = [:br_panel] || NilPanel.new(@ui) Logger.log "br_panel: #{@br_panel}" @list_renderer = [:list_renderer] || ListRenderer.new define_selection_aliases end |
Instance Attribute Details
#bl_panel ⇒ Object (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_panel ⇒ Object (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_renderer ⇒ Object (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_panel ⇒ Object (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_panel ⇒ Object (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 |
#ui ⇒ Object (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 |
Instance Method Details
#choose ⇒ Object
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_aliases ⇒ Object
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 |
#exit ⇒ Object 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 |
#selections ⇒ Object
48 49 50 |
# File 'lib/fortune_teller/chooser.rb', line 48 def selections selections_hash.values end |
#selections_hash ⇒ Object
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
58 59 60 |
# File 'lib/fortune_teller/chooser.rb', line 58 def valid? choice respond_to? choice end |