Module: Date::Memoize

Defined in:
lib/date/memoize.rb

Overview

Adds memoization to Date. This can speed things up significantly in cases where a lot of the same Date objects are created.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extend_object(base) ⇒ Object

Overridden to move the existing methods out of the way before copying this module’s methods.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/date/memoize.rb', line 38

def self.extend_object(base)
  singleton = (class<<base;self;end)
  methods_replaced.each do |method|
    singleton.send :alias_method, "#{method}_without_memoization", method
    singleton.send :remove_method, method
  end
  base.send :instance_variable_set, :@__memoized_civil_dates, 
    Hash.new{|h,key| h[key]=Date.new_without_memoization(*key)}
  base.send :instance_variable_set, :@__memoized_strptime_dates, 
    Hash.new{|h,key| h[key]=Date.strptime_without_memoization(*key)}
  base.send :instance_variable_set, :@__memoized_parse_dates, 
    Hash.new{|h,key| h[key]=Date.parse_without_memoization(*key)}
  super
end

.install!Object

Extend the Date class with memoized versions of new and civil but only if memoization has not yet been installed.



73
74
75
# File 'lib/date/memoize.rb', line 73

def self.install!
  Date.extend self unless installed?
end

.installed?Boolean

Is Date memoization currently installed and active?

Returns:

  • (Boolean)


67
68
69
# File 'lib/date/memoize.rb', line 67

def self.installed?
  Date.respond_to? :civil_without_memoization
end

.methods_replacedObject

The methods we’ll be replacing on the Date singleton.



32
33
34
# File 'lib/date/memoize.rb', line 32

def self.methods_replaced
  [ :new, :civil, :strptime, :parse ]
end

.unextend_object(base) ⇒ Object

Removes memoization methods from singleton of the class provided.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/date/memoize.rb', line 54

def self.unextend_object(base)
  singleton = (class<<base;self;end)
  methods_replaced.each do |method|
    singleton.send :alias_method,  method, "#{method}_without_memoization"
    singleton.send :remove_method, "#{method}_without_memoization"
  end
  base.send :remove_instance_variable, :@__memoized_civil_dates
  base.send :remove_instance_variable, :@__memoized_strptime_dates
  base.send :remove_instance_variable, :@__memoized_parse_dates
  base
end

.uninstall!Object

Remove memoized methods and free up memo cache. This method is idempotent.



78
79
80
# File 'lib/date/memoize.rb', line 78

def self.uninstall!
  unextend_object Date if installed?
end

Instance Method Details

#civil(y = -4712,, m = 1, d = 1, sg = ITALY) ⇒ Object Also known as: new

Memoized version of Date::civil.



23
24
25
# File 'lib/date/memoize.rb', line 23

def civil(y=-4712, m=1, d=1, sg=ITALY)
  @__memoized_civil_dates[ [ y, m, d, sg ] ]
end

#parse(str = '-4712-01-01', comp = false, sg = ITALY) ⇒ Object

Memoized version Date::parse.



18
19
20
# File 'lib/date/memoize.rb', line 18

def parse(str='-4712-01-01', comp=false, sg=ITALY)
  @__memoized_parse_dates[ [ str, comp, sg ] ]
end

#strptime(str = '-4712-01-01', fmt = '%F', sg = ITALY) ⇒ Object

Memoized version of Date::strptime.



13
14
15
# File 'lib/date/memoize.rb', line 13

def strptime(str='-4712-01-01', fmt='%F', sg=ITALY)
  @__memoized_strptime_dates[ [ str, fmt, sg ] ]
end