Skip to content

Commit

Permalink
Added rank as part of the array to allow easy re-sorting.
Browse files Browse the repository at this point in the history
  • Loading branch information
ciaranarcher committed Dec 31, 2012
1 parent b3e03e7 commit 5d15117
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/hacker_term.rb
Expand Up @@ -101,8 +101,14 @@ def truncate_line!(data)

def show(page_data)
draw_header
page_data.data.each_index { |i| draw_item_line(i + 1, page_data.data.fetch(i))}

page_data.data.each_index do |i|
line_data = page_data.data.fetch(i)
draw_item_line(line_data['rank'].to_i, line_data)
end

draw_footer(page_data.mean_score, page_data.median_score, page_data.mode_score)

getch
close_screen
end
Expand All @@ -128,6 +134,8 @@ def sort_on!(mode)
@data = @data.sort { |a, b| a['score'].to_f <=> b['score'].to_f }
when :comments
@data = @data.sort { |a, b| a['comments'].to_f <=> b['comments'].to_f }
when :rank
@data = @data.sort { |a, b| a['rank'].to_f <=> b['rank'].to_f }
else
throw "sorting mode #{mode} not supported"
end
Expand Down Expand Up @@ -155,14 +163,23 @@ def calculate_median_score

def add_missing_keys!
# Here we're looking to fix nodes with missing/incorrect data
counter = 1
@data.each do |item|

# Add rank (so we can re-sort in 'natural' order)
unless item.has_key? 'rank'
item['rank'] = counter.to_s
end

unless item.has_key? 'score'
item['score'] = '0'
end

unless item.has_key? 'comments'
item['comments'] = '0'
end

counter += 1
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/hacker_term_spec.rb
Expand Up @@ -123,6 +123,12 @@ module HackerTerm
@pd.data.first['title'].should == 'Third Article'
@pd.data.last['title'].should == 'First Article'
end

it 'sorts by rank of comments when requested' do
@pd.sort_on!(:rank)
@pd.data.first['title'].should == 'First Article'
@pd.data.last['title'].should == 'Third Article'
end
end
end
end

0 comments on commit 5d15117

Please sign in to comment.