Class: DurationRange

Inherits:
DelegateDecorator
  • Object
show all
Includes:
Comparable
Defined in:
lib/timespan/core_ext/range.rb

Direct Known Subclasses

LongDurationRange, ShortDurationRange

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.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/timespan/core_ext/range.rb', line 25

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

#rangeObject

Returns the value of attribute range.



23
24
25
# File 'lib/timespan/core_ext/range.rb', line 23

def range
  @range
end

#unitObject Also known as: units

Returns the value of attribute unit.



23
24
25
# File 'lib/timespan/core_ext/range.rb', line 23

def unit
  @unit
end

Class Method Details

.allowed_unit?(unit) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/timespan/core_ext/range.rb', line 64

def self.allowed_unit? unit
  allowed_units.include? unit.to_sym
end

.demongoize(object) ⇒ Timespan

Deserialize a Timespan given the hash stored by Mongodb

Parameters:

  • Timespan (Hash)

    as hash

Returns:



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/timespan/core_ext/range.rb', line 138

def demongoize(object)
  return if !object
  
  demongoized = case object
  when Hash
    object.__evolve_to_duration_range__
  when Range
    object.__evolve_to_duration_range__
  else
    raise "Unable to demongoize DurationRange from: #{object}"
  end    
  # puts "demongoized: #{demongoized} - #{demongoized.class}"
  demongoized
end

.evolve(object) ⇒ Object

Converts the object that was supplied to a criteria and converts it into a database friendly form.



155
156
157
# File 'lib/timespan/core_ext/range.rb', line 155

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.

Parameters:

Returns:

  • (Hash)

    Timespan in seconds



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/timespan/core_ext/range.rb', line 120

def mongoize object
  mongoized = case object
  when DurationRange then object.mongoize
  when Hash
    object
  when Range
    object.send(:seconds).mongoize
  else
    object
  end
  # puts "mongoized: #{mongoized} - Hash"
  mongoized
end

Instance Method Details

#<=>(other_dur_range) ⇒ Object



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

def <=> other_dur_range
  min_secs = self.min
  max_secs = self.max
  omin_secs = other_dur_range.min
  omax_secs = other_dur_range.max

  # puts "self: #{self.inspect} vs #{other_dur_range.inspect} #{other_dur_range.class}"

  if min_secs == omin_secs && max_secs == omax_secs
    return 0
  end

  if min_secs < omin_secs || (min_secs == omin_secs && max_secs < omax_secs) 
    -1
  else
    1
  end
end

#allowed_unit?(unit) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/timespan/core_ext/range.rb', line 68

def allowed_unit? unit
  allowed_units.include? unit.to_sym
end

#allowed_unitsObject



72
73
74
# File 'lib/timespan/core_ext/range.rb', line 72

def allowed_units
  [:seconds, :minutes, :hours, :days, :weeks, :months, :years]
end

#between?(duration) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
# File 'lib/timespan/core_ext/range.rb', line 88

def between? duration
  obj = case duration
  when Duration
    duration
  else
    Duration.new duration
  end
  obj.total >= min && obj.total <= max
end

#lengthObject



60
61
62
# File 'lib/timespan/core_ext/range.rb', line 60

def length
  :default
end

#mongoizeObject



102
103
104
# File 'lib/timespan/core_ext/range.rb', line 102

def mongoize
  to_hash
end

#timeObject



84
85
86
# File 'lib/timespan/core_ext/range.rb', line 84

def time
  min == max ? "#{min} #{unit.to_s.singularize}" : "#{min}-#{max} #{unit}"
end

#to_hashObject



98
99
100
# File 'lib/timespan/core_ext/range.rb', line 98

def to_hash
  {:from => range.min.to_i, :to => range.max.to_i, unit: unit.to_s, length: length.to_s}
end

#to_sObject



80
81
82
# File 'lib/timespan/core_ext/range.rb', line 80

def to_s
  range.min.nil? ? 'no duration range' : "#{range.min} to #{range.max} #{unit}"
end

#to_strObject



76
77
78
# File 'lib/timespan/core_ext/range.rb', line 76

def to_str
  to_s
end