lib/redistat/finder.rb in redistat-0.1.1 vs lib/redistat/finder.rb in redistat-0.2.0

- old
+ new

@@ -38,14 +38,18 @@ end end attr_reader :options - def initialize(options = {}) - @options = options + def initialize(opts = {}) + set_options(opts) end + def options + @options ||= {} + end + def all(reload = false) @result = nil if reload @result ||= find end @@ -63,62 +67,68 @@ def each_with_index(&block) all.each_with_index(&block) end + def children + build_key.children.map { |key| + self.class.new(options.merge(:label => key.label.to_s)) + } + end + def connection_ref(ref) - reset! if @options[:connection_ref] != ref - @options[:connection_ref] = ref + reset! if options[:connection_ref] != ref + options[:connection_ref] = ref self end def scope(scope) - reset! if @options[:scope] != scope - @options[:scope] = scope + reset! if !options[:scope].nil? && options[:scope].to_s != scope + options[:scope] = Scope.new(scope) self end def label(label) - reset! if @options[:label] != label - @options[:label] = label + reset! if !options[:label].nil? && options[:label].to_s != label + options[:label] = Label.new(label) self end def dates(from, till) from(from).till(till) end alias :date :dates def from(date) - reset! if @options[:from] != date - @options[:from] = date + reset! if options[:from] != date + options[:from] = date self end def till(date) - reset! if @options[:till] != date - @options[:till] = date + reset! if options[:till] != date + options[:till] = date self end alias :until :till def depth(unit) - reset! if @options[:depth] != unit - @options[:depth] = unit + reset! if options[:depth] != unit + options[:depth] = unit self end def interval(unit) - reset! if @options[:interval] != unit - @options[:interval] = unit + reset! if options[:interval] != unit + options[:interval] = unit self end - def find(options = {}) - set_options(options) + def find(opts = {}) + set_options(opts) raise InvalidOptions.new if !valid_options? - if @options[:interval].nil? || !@options[:interval] + if options[:interval].nil? || !options[:interval] find_by_magic else find_by_interval end end @@ -128,18 +138,18 @@ def set_options(opts = {}) opts = opts.clone opts.each do |key, value| self.send(key, opts.delete(key)) if self.respond_to?(key) end - @options.merge!(opts) + self.options.merge!(opts) end - def find_by_interval(options = {}) + def find_by_interval raise InvalidOptions.new if !valid_options? key = build_key - col = Collection.new(@options) - col.total = Result.new(@options) + col = Collection.new(options) + col.total = Result.new(options) build_date_sets.each do |set| set[:add].each do |date| result = Result.new result.date = Date.new(date).to_time db.hgetall("#{key.prefix}#{date}").each do |k, v| @@ -150,15 +160,15 @@ end end col end - def find_by_magic(options = {}) + def find_by_magic raise InvalidOptions.new if !valid_options? - key = Key.new(@options[:scope], @options[:label]) - col = Collection.new(@options) - col.total = Result.new(@options) + key = build_key + col = Collection.new(options) + col.total = Result.new(options) col << col.total build_date_sets.each do |set| sum = Result.new sum = summarize_add_keys(set[:add], key, sum) sum = summarize_rem_keys(set[:rem], key, sum) @@ -172,20 +182,20 @@ def reset! @result = nil end def valid_options? - return true if !@options[:scope].blank? && !@options[:label].blank? && !@options[:from].blank? && !@options[:till].blank? + return true if !options[:scope].blank? && !options[:label].blank? && !options[:from].blank? && !options[:till].blank? false end def build_date_sets - Finder::DateSet.new(@options[:from], @options[:till], @options[:depth], @options[:interval]) + Finder::DateSet.new(options[:from], options[:till], options[:depth], options[:interval]) end def build_key - Key.new(@options[:scope], @options[:label]) + Key.new(options[:scope], options[:label]) end def summarize_add_keys(sets, key, sum) sets.each do |date| db.hgetall("#{key.prefix}#{date}").each do |k, v| @@ -203,10 +213,10 @@ end sum end def db - super(@options[:connection_ref]) + super(options[:connection_ref]) end end end \ No newline at end of file