Module: Memoize

Overview

Module Memoize

Instance Method Summary collapse

Instance Method Details

#[](property) ⇒ Object



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
# File 'lib/memoize.rb', line 22

def [](property)
    @memoize = {} unless defined?(@memoize)

    if (value = @memoize[property])
        return value
    end

    current_item = @item

    property_path = ''

    property.split('.').each do |elem|
        if property_path.empty?
            property_path << elem
        else
            property_path << '.' << elem
        end

        if (val = @memoize[property_path])
            current_item = val
        else
            begin
                current_item = current_item.send(elem)
            rescue StandardError
                current_item = nil
            end
        end

        break if current_item.nil?

        @memoize[property_path] = current_item
    end

    @memoize[property] = current_item
end

#[]=(property, value) ⇒ Object



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

def []=(property, value)
    @memoize = {} unless defined?(@memoize)

    @memoize[property] = value
end

#clear(property) ⇒ Object



58
59
60
61
# File 'lib/memoize.rb', line 58

def clear(property)
    @memoize = {} unless defined?(@memoize)
    @memoize.clear[property] if @memoize[property]
end

#clear_allObject



63
64
65
# File 'lib/memoize.rb', line 63

def clear_all
    @memoize = {}
end