lib/supernova/criteria.rb in supernova-0.1.0 vs lib/supernova/criteria.rb in supernova-0.1.1

- old
+ new

@@ -1,7 +1,7 @@ class Supernova::Criteria - attr_accessor :filters, :options, :clazz + attr_accessor :filters, :search_options, :clazz class << self def method_missing(*args) scope = self.new if scope.respond_to?(args.first) @@ -17,27 +17,27 @@ end def initialize(clazz = nil) self.clazz = clazz self.filters = {} - self.options = {} + self.search_options = {} end def for_classes(clazzes) merge_filters :classes, [clazzes].flatten end def order(order_option) - merge_options :order, order_option + merge_search_options :order, order_option end def limit(limit_option) - merge_options :limit, limit_option + merge_search_options :limit, limit_option end def group_by(group_option) - merge_options :group_by, group_option + merge_search_options :group_by, group_option end def search(query) merge_filters :search, query end @@ -45,30 +45,58 @@ def with(filters) merge_filters :with, filters end def select(fields) - merge_options :select, fields + merge_search_options :select, fields end def conditions(filters) merge_filters :conditions, filters end - def paginate(options) - merge_options :pagination, options + def paginate(pagination_options) + merge_search_options :pagination, pagination_options end + + def near(*coordinates) + merge_search_options :geo_center, normalize_coordinates(*coordinates) + end + + def within(distance) + merge_search_options :geo_distance, distance + end + + def options(options_hash) + merge_search_options :custom_options, options_hash + end + + def normalize_coordinates(*args) + flattened = args.flatten + if (lat = read_first_attribute(flattened.first, :lat, :latitude)) && (lng = read_first_attribute(flattened.first, :lng, :lon, :longitude)) + { :lat => lat.to_f, :lng => lng.to_f } + elsif flattened.length == 2 + { :lat => flattened.first.to_f, :lng => flattened.at(1).to_f } + end + end + + def read_first_attribute(object, *keys) + keys.each do |key| + return object.send(key) if object.respond_to?(key) + end + nil + end def merge_filters(key, value) - merge_filters_or_options(self.filters, key, value) + merge_filters_or_search_options(self.filters, key, value) end - def merge_options(key, value) - merge_filters_or_options(self.options, key, value) + def merge_search_options(key, value) + merge_filters_or_search_options(self.search_options, key, value) end - def merge_filters_or_options(reference, key, value) + def merge_filters_or_search_options(reference, key, value) if value.is_a?(Hash) reference[key] ||= Hash.new reference[key].merge!(value) else reference[key] = value @@ -90,11 +118,11 @@ def merge(other_criteria) other_criteria.filters.each do |key, value| self.merge_filters(key, value) end - other_criteria.options.each do |key, value| - self.merge_options(key, value) + other_criteria.search_options.each do |key, value| + self.merge_search_options(key, value) end self end def method_missing(*args, &block) \ No newline at end of file