Class: DurationRange
- Inherits:
-
DelegateDecorator
- Object
- DelegateDecorator
- DurationRange
show all
- Defined in:
- lib/timespan/core_ext/range.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(range, unit = :minutes) ⇒ DurationRange
Returns a new instance of DurationRange.
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/timespan/core_ext/range.rb', line 23
def initialize range, unit = :minutes
range = (0..60) if range.min == nil || range.max == nil
super(range, except: %w{to_s to_str})
unit = unit.to_s.pluralize.to_sym
unless allowed_unit? unit
raise ArgumentError, "Unit #{unit} not valid, only: #{allowed_units} are valid"
end
@unit = unit
@range = range
end
|
Instance Attribute Details
Returns the value of attribute range.
21
22
23
|
# File 'lib/timespan/core_ext/range.rb', line 21
def range
@range
end
|
Returns the value of attribute unit.
21
22
23
|
# File 'lib/timespan/core_ext/range.rb', line 21
def unit
@unit
end
|
Class Method Details
.allowed_unit?(unit) ⇒ Boolean
38
39
40
|
# File 'lib/timespan/core_ext/range.rb', line 38
def self.allowed_unit? unit
allowed_units.include? unit.to_sym
end
|
.demongoize(object) ⇒ Timespan
Deserialize a Timespan given the hash stored by Mongodb
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/timespan/core_ext/range.rb', line 102
def demongoize(object)
return if !object
demongoized = case object
when Hash
object.__evolve_to_duration_range__
else
raise "Unable to demongoize DurationRange from: #{object}"
end
demongoized
end
|
.evolve(object) ⇒ Object
Converts the object that was supplied to a criteria and converts it into a database friendly form.
117
118
119
|
# File 'lib/timespan/core_ext/range.rb', line 117
def evolve(object)
object.__evolve_to_duration_range__.mongoize
end
|
.mongoize(object) ⇒ Hash
Serialize a Hash (with DurationRange keys) or a DurationRange to a BSON serializable type.
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/timespan/core_ext/range.rb', line 84
def mongoize object
mongoized = case object
when DurationRange then object.mongoize
when Hash
object
when Range
object.send(:seconds).mongoize
else
object
end
mongoized
end
|
Instance Method Details
#__evolve_to_duration_range__ ⇒ Object
58
59
60
|
# File 'lib/timespan/core_ext/range.rb', line 58
def __evolve_to_duration_range__
self
end
|
#allowed_unit?(unit) ⇒ Boolean
42
43
44
|
# File 'lib/timespan/core_ext/range.rb', line 42
def allowed_unit? unit
allowed_units.include? unit.to_sym
end
|
#allowed_units ⇒ Object
46
47
48
|
# File 'lib/timespan/core_ext/range.rb', line 46
def allowed_units
[:seconds, :minutes, :hours, :days, :weeks, :months, :years]
end
|
#between?(duration) ⇒ Boolean
66
67
68
69
70
71
72
73
74
|
# File 'lib/timespan/core_ext/range.rb', line 66
def between? duration
obj = case duration
when Duration
duration
else
Duration.new duration
end
obj.total >= min && obj.total <= max
end
|
62
63
64
|
# File 'lib/timespan/core_ext/range.rb', line 62
def mongoize
{:from => range.min.to_i, :to => range.max.to_i}
end
|
54
55
56
|
# File 'lib/timespan/core_ext/range.rb', line 54
def to_s
range.min.nil? ? 'no duration range' : "#{range.min} to #{range.max} #{unit}"
end
|
50
51
52
|
# File 'lib/timespan/core_ext/range.rb', line 50
def to_str
to_s
end
|