module Temporal::Mathematics

Public Class Methods

negation(klass) click to toggle source
# File lib/temporal/mathematics.rb, line 27
def self.negation klass
  klass.class_eval do
    begin
      alias :temporal_negation :-@
    rescue
      def temporal_negation
        raise NoMethodError.new( "undefined method `-@' for #{self.to_s}:#{self.class.to_s}" )
      end
    end
    def -@
      return temporal_negation unless self.class == Temporal::Shift or self.class == Range
      if exclude_end?
        -first...-last
      else
        -first..-last
      end
    end
  end
end
operators(klass) click to toggle source
# File lib/temporal/mathematics.rb, line 47
def self.operators klass
  klass.class_eval do
    begin
      alias :temporal_addition :+
    rescue
      def temporal_addition target
        raise NoMethodError.new( "undefined method `+' for #{self.to_s}:#{self.class.to_s}" )
      end
    end
    def + target
      if target.class == Range
        if self.class == Range
          raise Temporal::Anomaly.new( "Missmatched Range end exclusions" ) if exclude_end? != target.exclude_end?
          if target.exclude_end?
            return (target.first + first)...(target.last + last)
          else
            return (target.first + first)..(target.last + last)
          end
        else
          if target.exclude_end?
            return (target.first + self)...(target.last + self)
          else
            return (target.first + self)..(target.last + self)
          end
        end
      end
      return temporal_addition( target ) unless target.class == Temporal::Shift
      target + self
    end

    begin
      alias :temporal_subtraction :-
    rescue
      def temporal_subtraction target
        raise NoMethodError.new( "undefined method `-' for #{self.to_s}:#{self.class.to_s}" )
      end
    end
    def - target
      return temporal_subtraction( target ) unless target.class == Temporal::Shift or target.class == Range
      (-target) + self
    end
  end
end
units(klass) click to toggle source
# File lib/temporal/mathematics.rb, line 4
def self.units klass
  klass.class_eval do
    Temporal::Shift.units.each do |unit|
      if klass == Range
        define_method unit do
          if exclude_end?
            return Temporal::Shift.new( first, unit )...Temporal::Shift.new( last, unit )
          else
            return Temporal::Shift.new( first, unit )..Temporal::Shift.new( last, unit )
          end
        end
      else
        define_method unit do
          Temporal::Shift.new( self, unit )
        end
      end
      define_method "#{unit}s" do
        send(unit)
      end
    end
  end
end

Public Instance Methods

+(target) click to toggle source
# File lib/temporal/mathematics.rb, line 56
def + target
  if target.class == Range
    if self.class == Range
      raise Temporal::Anomaly.new( "Missmatched Range end exclusions" ) if exclude_end? != target.exclude_end?
      if target.exclude_end?
        return (target.first + first)...(target.last + last)
      else
        return (target.first + first)..(target.last + last)
      end
    else
      if target.exclude_end?
        return (target.first + self)...(target.last + self)
      else
        return (target.first + self)..(target.last + self)
      end
    end
  end
  return temporal_addition( target ) unless target.class == Temporal::Shift
  target + self
end
-(target) click to toggle source
# File lib/temporal/mathematics.rb, line 84
def - target
  return temporal_subtraction( target ) unless target.class == Temporal::Shift or target.class == Range
  (-target) + self
end
-@() click to toggle source
# File lib/temporal/mathematics.rb, line 36
def -@
  return temporal_negation unless self.class == Temporal::Shift or self.class == Range
  if exclude_end?
    -first...-last
  else
    -first..-last
  end
end
temporal_addition(target) click to toggle source
# File lib/temporal/mathematics.rb, line 52
def temporal_addition target
  raise NoMethodError.new( "undefined method `+' for #{self.to_s}:#{self.class.to_s}" )
end
temporal_negation() click to toggle source
# File lib/temporal/mathematics.rb, line 32
def temporal_negation
  raise NoMethodError.new( "undefined method `-@' for #{self.to_s}:#{self.class.to_s}" )
end
temporal_subtraction(target) click to toggle source
# File lib/temporal/mathematics.rb, line 80
def temporal_subtraction target
  raise NoMethodError.new( "undefined method `-' for #{self.to_s}:#{self.class.to_s}" )
end