Class: DurationRange

Inherits:
DelegateDecorator
  • Object
show all
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.



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

#rangeObject

Returns the value of attribute range.



21
22
23
# File 'lib/timespan/core_ext/range.rb', line 21

def range
  @range
end

#unitObject

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

Returns:

  • (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

Parameters:

  • Timespan (Hash)

    as hash

Returns:



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    
  # 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.



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.

Parameters:

Returns:

  • (Hash)

    Timespan in seconds



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
  # puts "mongoized: #{mongoized} - Hash"
  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

Returns:

  • (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_unitsObject



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

Returns:

  • (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

#mongoizeObject



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

#to_sObject



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

#to_strObject



50
51
52
# File 'lib/timespan/core_ext/range.rb', line 50

def to_str
  to_s
end