diff options
author | Abinoam P. Marques Jr <abinoam@gmail.com> | 2016-06-03 10:14:32 -0300 |
---|---|---|
committer | Abinoam P. Marques Jr <abinoam@gmail.com> | 2016-06-03 14:02:09 -0300 |
commit | 29448eb49337225e2158d77bf089947ee0cb8e8f (patch) | |
tree | bc7949a79c799ab7fbd8303b6a79c9ecc11d0f1c /lib/highline/menu.rb | |
parent | 4780a3382fede3480a2be638f67093d9a808b1a1 (diff) | |
download | highline-29448eb49337225e2158d77bf089947ee0cb8e8f.tar.gz |
Extract compound logic from Menu#to_ary
This reduces complexity of the code improving
code climate and flog score.
Diffstat (limited to 'lib/highline/menu.rb')
-rw-r--r-- | lib/highline/menu.rb | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/lib/highline/menu.rb b/lib/highline/menu.rb index 2adc244..e2e2475 100644 --- a/lib/highline/menu.rb +++ b/lib/highline/menu.rb @@ -484,17 +484,25 @@ class HighLine # This method returns all menu items to be displayed, complete with # indexes. # - def to_ary( ) + def to_ary + @items.map.with_index { |item, ix| decorate_item(item.text.to_s, ix) } + end + + def decorate_item(text, ix) + decorated, non_decorated = mark_for_decoration(text, ix) + decorate_index(decorated) + non_decorated + end + + def mark_for_decoration(text, ix) case @index when :number - @items.map { |i| decorate_index("#{@items.index(i) + 1}#{@index_suffix}") + "#{i.text}" } + ["#{ix + 1}#{@index_suffix}", text] when :letter - l_index = "`" - @items.map { |i| decorate_index("#{l_index.succ!}#{@index_suffix}") + "#{i.text}" } + ["#{('a'.ord + ix).chr}#{@index_suffix}", text] when :none - @items.map { |i| decorate_index("#{i.text}") } + [text, ""] else - @items.map { |i| decorate_index("#{index}#{@index_suffix}") + "#{i.text}" } + ["#{index}#{@index_suffix}", text] end end |