Class: Mongoid::Tracking::ReaderExtender
- Inherits:
-
Object
- Object
- Mongoid::Tracking::ReaderExtender
show all
- Defined in:
- lib/mongoid/tracking/reader_extender.rb
Overview
ReaderExtender is used in cases where we need to return an integer (class Numeric) while extending their contents. It would allow to perform advanced calculations in some situations:
Example:
a = visits.today
b = visits.yesterday
c = a + b
Instance Method Summary
collapse
Constructor Details
#initialize(number, hours) ⇒ ReaderExtender
Returns a new instance of ReaderExtender.
22
23
24
25
|
# File 'lib/mongoid/tracking/reader_extender.rb', line 22
def initialize(number, hours)
@total = number
@hours = hours
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &blk) ⇒ Object
85
86
87
88
|
# File 'lib/mongoid/tracking/reader_extender.rb', line 85
def method_missing(name, *args, &blk)
ret = @total.send(name, *args, &blk)
ret.is_a?(Numeric) ? ReaderExtender.new(ret, @hours) : ret
end
|
Instance Method Details
#+(other) ⇒ Object
66
67
68
69
|
# File 'lib/mongoid/tracking/reader_extender.rb', line 66
def +(other)
return @total + other unless other.is_a?(ReaderExtender)
self.class.new(other + @total, @hours.zip(other.hourly).map!(&:sum))
end
|
#<(other) ⇒ Object
51
52
53
|
# File 'lib/mongoid/tracking/reader_extender.rb', line 51
def <(other)
@total < other
end
|
#<=(other) ⇒ Object
54
55
56
|
# File 'lib/mongoid/tracking/reader_extender.rb', line 54
def <=(other)
@total <= other
end
|
#<=>(other) ⇒ Object
47
48
49
|
# File 'lib/mongoid/tracking/reader_extender.rb', line 47
def <=>(other)
@total <=> other
end
|
#==(other) ⇒ Object
43
44
45
|
# File 'lib/mongoid/tracking/reader_extender.rb', line 43
def ==(other)
@total == other
end
|
#>(other) ⇒ Object
58
59
60
|
# File 'lib/mongoid/tracking/reader_extender.rb', line 58
def >(other)
@total > other
end
|
#as_json(options = nil) ⇒ Object
75
76
77
|
# File 'lib/mongoid/tracking/reader_extender.rb', line 75
def as_json(options = nil)
@total
end
|
#coerce(other) ⇒ Object
71
72
73
|
# File 'lib/mongoid/tracking/reader_extender.rb', line 71
def coerce(other)
[self.to_i, other]
end
|
#hourly ⇒ Object
27
28
29
|
# File 'lib/mongoid/tracking/reader_extender.rb', line 27
def hourly
@hours
end
|
#to_f ⇒ Object
35
36
37
|
# File 'lib/mongoid/tracking/reader_extender.rb', line 35
def to_f
@total.to_f
end
|
#to_i ⇒ Object
39
40
41
|
# File 'lib/mongoid/tracking/reader_extender.rb', line 39
def to_i
@total.to_i
end
|
#to_s ⇒ Object
31
32
33
|
# File 'lib/mongoid/tracking/reader_extender.rb', line 31
def to_s
@total.to_s
end
|