danielharan (owner)

Revisions

gist: 24352 Download_button fork
public
Public Clone URL: git://gist.github.com/24352.git
Text only
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Example use:
# weigh_by_relative_importance(@report.items, :cell_value, [2,4,6,8,10]) do |item,weight|
# xml.movie :lat => item.latitude, :long => item.longitude, :title => item.city,
# :height => weight, :width => weight
# end
# Code inspired by http://www.juixe.com/techknow/index.php/2006/07/15/acts-as-taggable-tag-cloud/
# and meant to be usable for items on a map (as above) or tags
module MapHelper
  def weigh_by_relative_importance(items, method_name, classes)
    sorted = items.collect {|item| item.send(method_name)}.sort
    min, max = sorted.first, sorted.last
    
    divisor = ((max - min) / classes.size) + 1
    
    items.each do |item|
      yield item, classes[(item.send(method_name) - min) / divisor]
    end
  end
end