Class: Deplate::Cache

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

Overview

Description

This class provides a cache for dynamically generated classes.

Constant Summary collapse

@@custom_particles =
{}
@@custom_macros =
{}
@@custom_elements =
{}
@@custom_regions =
{}
@@custom_commands =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache, super_class, deplate, body, args = {}) ⇒ Cache

Returns a new instance of Cache.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/deplate/cache.rb', line 75

def initialize(cache, super_class, deplate, body, args={})
    @deplate = deplate
    @cache   = cache
    specific = args[:specific]
    retrieve_particle(body, specific)
    unless @cls
        @cls = Class.new(super_class)
        if body.kind_of?(Proc)
            @cls.class_eval(&body)
        else
            @cls.class_eval(body)
        end
        store_particle(body, specific)
    end
    if @cls and block_given?
        yield(@cls)
    end
end

Instance Attribute Details

#clsObject (readonly)

Returns the value of attribute cls.



19
20
21
# File 'lib/deplate/cache.rb', line 19

def cls
  @cls
end

Class Method Details

.command(deplate, body, args) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/deplate/cache.rb', line 44

def command(deplate, body, args)
    register = args[:register]
    parent   = args[:super] || Deplate::Command
    new(@@custom_commands, parent, deplate, body, args) do |cls|
        if register
            deplate.input.register_command(cls, args)
        end
    end
end

.element(deplate, body, args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/deplate/cache.rb', line 33

def element(deplate, body, args)
    register = args[:register]
    parent   = args[:super] || Deplate::Element
    new(@@custom_elements, parent, deplate, body, args) do |cls|
        if register
            args[:id] = body
            deplate.input.register_element(cls, args)
        end
    end
end

.macro(deplate, body, args) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/deplate/cache.rb', line 64

def macro(deplate, body, args)
    register = args[:register]
    parent   = args[:super] || Deplate::Region
    new(@@custom_macros, parent, deplate, body, args) do |cls|
        if register
            deplate.input.register_macro(cls, args)
        end
    end
end

.particle(deplate, body, args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/deplate/cache.rb', line 22

def particle(deplate, body, args)
    register = args[:register]
    parent   = args[:super] || Deplate::Particle
    new(@@custom_particles, parent, deplate, body, args) do |cls|
        if register
            args[:id] = body
            deplate.input.register_particle(cls, args)
        end
    end
end

.region(deplate, body, args) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/deplate/cache.rb', line 54

def region(deplate, body, args)
    register = args[:register]
    parent   = args[:super] || Deplate::Region
    new(@@custom_regions, parent, deplate, body, args) do |cls|
        if register
            deplate.input.register_region(cls, args)
        end
    end
end

Instance Method Details

#get_formatter_name(specific) ⇒ Object



106
107
108
# File 'lib/deplate/cache.rb', line 106

def get_formatter_name(specific)
    return specific ? @deplate.formatter.formatter_name : :general
end

#retrieve_particle(body, specific = false) ⇒ Object



94
95
96
97
98
# File 'lib/deplate/cache.rb', line 94

def retrieve_particle(body, specific=false)
    fmt       = get_formatter_name(specific)
    particles = @cache[fmt] ||= {}
    @cls = particles[body]
end

#store_particle(body, specific = false) ⇒ Object



100
101
102
103
104
# File 'lib/deplate/cache.rb', line 100

def store_particle(body, specific=false)
    fmt       = get_formatter_name(specific)
    particles = @cache[fmt] ||= {}
    particles[body] = @cls
end