class Periodical::Filter::Period
Keep count sorted objects per period.
Constants
- ORDER
Given times a and b, should we prefer a?
Attributes
count[R]
Public Class Methods
new(count)
click to toggle source
@param count the number of items we should retain.
# File lib/periodical/filter.rb, line 39 def initialize(count) @count = count end
Public Instance Methods
filter(values, keep: :old) { |value| ... }
click to toggle source
@param order can be a key in ORDER
or a lambda. @param block is applied to the value and should typically return a Time instance.
# File lib/periodical/filter.rb, line 45 def filter(values, keep: :old, &block) slots = {} keep = ORDER.fetch(keep, keep) values.each do |value| time = block_given? ? yield(value) : value granular_key = key(time) # We filter out this value if the slot is already full and we prefer the existing value. if existing_value = slots[granular_key] existing_time = block_given? ? yield(existing_value) : existing_value next if keep.call(existing_time, time) end slots[granular_key] = value end sorted_values = slots.values.sort return sorted_values.first(@count) end
key(t)
click to toggle source
# File lib/periodical/filter.rb, line 69 def key(t) raise NotImplementedError end
mktime(year, month=1, day=1, hour=0, minute=0, second=0)
click to toggle source
# File lib/periodical/filter.rb, line 73 def mktime(year, month=1, day=1, hour=0, minute=0, second=0) return Time.new(year, month, day, hour, minute, second) end