Skip to content

Instantly share code, notes, and snippets.

@MichaelXavier
Created July 2, 2010 18:26
Show Gist options
  • Save MichaelXavier/461704 to your computer and use it in GitHub Desktop.
Save MichaelXavier/461704 to your computer and use it in GitHub Desktop.
# Call and output
prod = Product.create(:hive_id => 1214241241414241, :name => "whatever")
res = prod.min_max_avg
# Error
Failure/Error: res = prod.min_max_avg
keys must be strings or symbols
# /home/michael/.rvm/gems/ruby-1.8.7-p249/gems/bson-1.0.3/lib/../lib/bson/bson_c.rb:24:in `serialize'
# /home/michael/.rvm/gems/ruby-1.8.7-p249/gems/bson-1.0.3/lib/../lib/bson/bson_c.rb:24:in `serialize'
# /home/michael/.rvm/gems/ruby-1.8.7-p249/gems/mongo-1.0.3/lib/../lib/mongo/cursor.rb:364:in `construct_query_message'
# /home/michael/.rvm/gems/ruby-1.8.7-p249/gems/mongo-1.0.3/lib/../lib/mongo/cursor.rb:346:in `send_initial_query'
# /home/michael/.rvm/gems/ruby-1.8.7-p249/gems/mongo-1.0.3/lib/../lib/mongo/cursor.rb:324:in `refill_via_get_more'
# /home/michael/.rvm/gems/ruby-1.8.7-p249/gems/mongo-1.0.3/lib/../lib/mongo/cursor.rb:319:in `num_remaining'
# /home/michael/.rvm/gems/ruby-1.8.7-p249/gems/mongo-1.0.3/lib/../lib/mongo/cursor.rb:92:in `has_next?'
# /home/michael/.rvm/gems/ruby-1.8.7-p249/gems/mongo-1.0.3/lib/../lib/mongo/cursor.rb:205:in `to_a'
# /home/michael/.rvm/gems/ruby-1.8.7-p249/gems/plucky-0.3.2/lib/plucky/query.rb:75:in `all'
# /home/michael/.rvm/gems/ruby-1.8.7-p249/gems/mongo_mapper-0.8.2/lib/mongo_mapper/plugins/querying/decorator.rb:25:in `all'
# ./app/models/series.rb:21:in `aggregatable_prices'
# ./app/models/product.rb:33:in `min_max_avg'
# /home/michael/.rvm/gems/ruby-1.8.7-p249@global/gems/activesupport-3.0.0.beta4/lib/active_support/core_ext/object/returning.rb:39:in `returning'
# ./app/models/product.rb:32:in `min_max_avg'
# ./spec/models/product_spec.rb:72
class Product
include MongoMapper::Document
key :hive_id, String, :required => true, :index => true
key :name, String, :required => true
timestamps!
validates_uniqueness_of :hive_id
many :prices
# Returns an array with the min, max and average for the range defined under
# the Prices aggregatable scope
def min_max_avg
@min_max_avg ||= returning({}) do |res|
# Chokes HERE on aggregatable_prices
totals = Series.aggregatable_prices(:amount, :qty).inject({:min => nil, :max => nil, :sum => 0.0, :qty => 0}) {|h,price|
# yadda yadda yadda.
}
end
end
end
class Series
include MongoMapper::Document
AGGREGATION_INTERVAL = 5.days
key :started_on, Time, :required => true
key :ended_on, Time
many :prices
# This scope is what we use for calculating min, max and average
scope :aggregatable, lambda {where(:started_on.gte => AGGREGATION_INTERVAL.ago)}
class << self
def aggregatable_prices(*select_cols)
finder_options = select_cols.empty? ? {} : {:fields => select_cols}
aggregatable.all.collect {|s| s.prices.all(finder_options)}.flatten
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment