lib/picky/sources/wrappers/location.rb in picky-0.12.1 vs lib/picky/sources/wrappers/location.rb in picky-0.12.2
- old
+ new
@@ -6,23 +6,21 @@
attr_reader :precision, :grid
# TODO Save min and grid!
#
- def initialize backend, options = {}
- super backend
+ def initialize category, options = {}
+ super category
- @user_grid = extract_user_grid options
- @precision = extract_precision options
-
- @grid = @user_grid / (@precision + 0.5)
+ @precision = extract_precision options
+ @calculation = Calculations::Location.new extract_user_grid(options), @precision
end
#
#
def extract_user_grid options
- options[:grid] || raise # TODO
+ options[:grid] || raise("Option :grid needs to be passed to a location.")
end
# Extracts an amount of grids that this
# Precision is given in a value.
# 1 is low (up to 16.6% error), 5 is very high (up to 5% error).
#
@@ -39,49 +37,45 @@
end
# Yield the data (id, text for id) for the given type and category.
#
def harvest type, category
- reset
+ minimum = 1.0/0
# Cache. TODO Make option?
#
locations = []
# Gather min/max.
#
backend.harvest type, category do |indexed_id, location|
location = location.to_f
- @min = location if location < @min
+ minimum = location if location < minimum
locations << [indexed_id, location]
end
- # Add a margin.
- #
- marginize
+ @calculation.minimum = minimum
# Recalculate locations.
#
locations.each do |indexed_id, location|
- locations_for(location).each do |new_location|
+ locations_for(@calculation.recalculate(location)).each do |new_location|
yield indexed_id, new_location.to_s
end
end
+
+ # TODO Move to the right place.
+ #
+ category.exact[:location_minimum] = minimum
end
- def marginize
- @min -= @user_grid
- end
-
# Put location onto multiple places on a grid.
#
# Note: Always returns an integer.
#
- def locations_for location
- new_location = ((location - @min) / grid).floor
-
- min_location = new_location - precision
- max_location = new_location + precision
+ def locations_for repositioned_location
+ min_location = repositioned_location - precision
+ max_location = repositioned_location + precision
(min_location..max_location).to_a
end
end
\ No newline at end of file