class DurationRange

Attributes

range[RW]
unit[RW]
units[RW]

Public Class Methods

allowed_unit?(unit) click to toggle source
# File lib/timespan/core_ext/range.rb, line 64
def self.allowed_unit? unit
  allowed_units.include? unit.to_sym
end
new(range, unit = :minutes) click to toggle source
Calls superclass method
# 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

Protected Class Methods

demongoize(object) click to toggle source

Deserialize a Timespan given the hash stored by Mongodb

@param [Hash] Timespan as hash @return [Timespan] deserialized Timespan

# 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) click to toggle source

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

# File lib/timespan/core_ext/range.rb, line 155
def evolve(object)
  object.__evolve_to_duration_range__.mongoize
end
mongoize(object) click to toggle source

Serialize a Hash (with DurationRange keys) or a DurationRange to a BSON serializable type.

@param [Timespan, Hash, Integer, String] value @return [Hash] Timespan in seconds

# 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
parse(duration) click to toggle source
# File lib/timespan/core_ext/range.rb, line 161
def parse duration
  if duration.kind_of? Numeric
     return Duration.new duration
  else
    case duration
    when Timespan
      duration.duration
    when Duration
      duration
    when Hash
      Duration.new duration
    when Time
      duration.to_i
    when DateTime, Date
      duration.to_time.to_i
    when String
      Duration.new parse_duration(duration)
    else
      raise ArgumentError, "Unsupported duration type: #{duration.inspect} of class #{duration.class}"
    end 
  end
end

Public Instance Methods

<=>(other_dur_range) click to toggle source
# 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) click to toggle source
# File lib/timespan/core_ext/range.rb, line 68
def allowed_unit? unit
  allowed_units.include? unit.to_sym
end
allowed_units() click to toggle source
# File lib/timespan/core_ext/range.rb, line 72
def allowed_units
  [:seconds, :minutes, :hours, :days, :weeks, :months, :years]
end
between?(duration) click to toggle source
# 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
length() click to toggle source
# File lib/timespan/core_ext/range.rb, line 60
def length
  :default
end
mongoize() click to toggle source
# File lib/timespan/core_ext/range.rb, line 102
def mongoize
  to_hash
end
time() click to toggle source
# File lib/timespan/core_ext/range.rb, line 84
def time
  min == max ? "#{min} #{unit.to_s.singularize}" : "#{min}-#{max} #{unit}"
end
to_hash() click to toggle source
# 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_s() click to toggle source
# 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_str() click to toggle source
# File lib/timespan/core_ext/range.rb, line 76
def to_str
  to_s
end

Protected Instance Methods

__evolve_to_duration_range__() click to toggle source
# File lib/timespan/core_ext/range.rb, line 108
def __evolve_to_duration_range__
  self
end