Sha256: 426d443e475e057a01716698825c34e1bc48ac5da729dc1a4773675ca802e96d
Contents?: true
Size: 1.83 KB
Versions: 2
Compression:
Stored size: 1.83 KB
Contents
module Popularity module ContainerMethods def self.included(base) base.class_eval do def results @results || [] end def add_result(result) @results ||= [] if @results.size > 0 verify_type = @results.first.name if verify_type == result.name @results << result else raise "ResultTypeError", "types must be the same within a results container" end else @results << result end end def as_json(options = {}) result_property_names = [] results_json = self.results.collect do |r| json = {} r.class.property_names.each do |name| json[name.to_s] = r.send(name.to_sym) end json["total"] = r.total {r.url => json} end json = aggregate_json json["_results"] = results_json json end def aggregate_json json = {} names = if self.class.respond_to?(:property_names) self.class.property_names else self.results.first.class.property_names end names.each do |name| json[name.to_s] = self.send(name.to_sym) end json["total"] = self.total json end def method_missing(method_sym, *arguments, &block) return 0 unless @results collection = @results.collect do |result| result.send(method_sym, *arguments) end if collection.all? { |t| t.is_a?(Fixnum) } collection.reduce(:+) else collection.flatten end end end end end class ResultsContainer include Popularity::ContainerMethods end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
popularity-0.3.0 | lib/popularity/results_container.rb |
popularity-0.2.1 | lib/popularity/results_container.rb |