Module: Temporal::Mathematics

Defined in:
lib/temporal/mathematics.rb

Class Method Summary collapse

Class Method Details

.negation(klass) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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