Class: Periodical::Filter::Period

Inherits:
Object
  • Object
show all
Defined in:
lib/periodical/filter.rb

Overview

Keep count sorted objects per period.

Direct Known Subclasses

Daily, Hourly, Monthly, Quarterly, Weekly, Yearly

Constant Summary collapse

KeepOldest =
Proc.new do |t1, t2|
	t1 > t2
end
KeepYoungest =
Proc.new do |t1, t2|
	t1 < t2
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(count) ⇒ Period

Returns a new instance of Period.



37
38
39
# File 'lib/periodical/filter.rb', line 37

def initialize(count)
	@count = count
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



68
69
70
# File 'lib/periodical/filter.rb', line 68

def count
  @count
end

Instance Method Details

#filter(values, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/periodical/filter.rb', line 41

def filter(values, options = {})
	slots = {}

	keep = (options[:keep] == :youngest) ? KeepYoungest : KeepOldest

	values.each do |value|
		k = key(value)

		# We want to keep the newest backup if possible (<).
		next if slots.key?(k) and keep.call(value, slots[k])

		slots[k] = value
	end

	sorted_values = slots.values.sort

	return sorted_values[0...@count]
end

#key(t) ⇒ Object

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/periodical/filter.rb', line 60

def key(t)
	raise NotImplementedError
end

#mktime(year, month = 1, day = 1, hour = 0, minute = 0, second = 0) ⇒ Object



64
65
66
# File 'lib/periodical/filter.rb', line 64

def mktime(year, month=1, day=1, hour=0, minute=0, second=0)
	return Time.new(year, month, day, hour, minute, second)
end