Class: TimeInterval

Inherits:
Object
  • Object
show all
Defined in:
lib/time_interval.rb

Constant Summary collapse

MASK =

Constants ============================================================

(0..30).to_a.collect { |i| (1 << 31) - (1 << (31 - i)) }.freeze
DEFAULT_SCALE =
(0..30).to_a.collect { |i| [ MASK[i], 2 ** i ] }.freeze
DEFAULT_SCALE_NAME =
(0..30).to_a.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ TimeInterval

Instance Methods =====================================================


108
109
110
# File 'lib/time_interval.rb', line 108

def initialize(value = nil)
  @time = self.class.interval_to_i(value || Time.now.utc)
end

Class Method Details

.interval_to_i(value) ⇒ Object


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/time_interval.rb', line 72

def self.interval_to_i(value)
  value = value.to_i

  if (value < 0)
    value = -value
    
    index = (0..30).to_a.reverse.detect do |i|
      MASK[i] == value & MASK[i]
    end

    value = (value ^ MASK[index]) * scale[scale_name[index]][1]
  end
  
  value
end

.nowObject


102
103
104
# File 'lib/time_interval.rb', line 102

def self.now
  new(nil)
end

.scaleObject


88
89
90
# File 'lib/time_interval.rb', line 88

def self.scale
  DEFAULT_SCALE
end

.scale_nameObject


92
93
94
# File 'lib/time_interval.rb', line 92

def self.scale_name
  DEFAULT_SCALE_NAME
end

.size(at_scale = nil) ⇒ Object


96
97
98
99
100
# File 'lib/time_interval.rb', line 96

def self.size(at_scale = nil)
  scale_details = scale[at_scale || scale_name[0]]
  
  scale_details and scale_details[1]
end

.with_definition(definition) ⇒ Object

Class Methods ========================================================


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/time_interval.rb', line 11

def self.with_definition(definition)
  scale_defn = nil
  scale_name_defn = [ ]
  
  case (definition)
  when Array
    case (definition[0])
    when String, Symbol
      definition = [ 1 ] + definition
    end
    
    scale_defn = { }
    scale_factor = 1
    mask_offset = 0

    definition.each_with_index do |element, i|
      case (i % 2)
      when 0
        scale_factor *= element
      when 1
        unless (scale_factor == 1)
          mask_offset += 1
        end

        scale_defn[element] = [ MASK[mask_offset], scale_factor ]
        scale_name_defn << element
      end
    end
  else
    scale = [ ]

    31.times do |i|
      scale_factor = i ** definition
      if (scale_factor < (1 << 31))
        scale_defn.push([ MASK[i], scale_factor ])
        scale_name_defn << i
      end
    end
  end

  subclass = nil
  
  if (scale_defn)
    subclass = Class.new(self)

    methods = Module.new do
      define_method(:scale) do
        scale_defn
      end

      define_method(:scale_name) do
        scale_name_defn
      end
    end
    
    subclass.send(:extend, methods)
  end
  
  subclass or self
end

Instance Method Details

#to_i(at_scale = nil) ⇒ Object


112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/time_interval.rb', line 112

def to_i(at_scale = nil)
  case (at_scale)
  when nil
    @time
  else
    scale_details = self.class.scale[at_scale]
    
    if (scale_details[1] == 1)
      @time
    else
      -(scale_details[0] | (@time / scale_details[1]))
    end
  end
end