Class: Deplate::Variables

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(deplate = nil) ⇒ Variables

Returns a new instance of Variables.



15
16
17
18
19
# File 'lib/deplate/variables.rb', line 15

def initialize(deplate=nil)
    super()
    @deplate  = deplate
    @priority = false
end

Instance Attribute Details

#deplateObject

Returns the value of attribute deplate.



13
14
15
# File 'lib/deplate/variables.rb', line 13

def deplate
  @deplate
end

Instance Method Details

#[](name) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/deplate/variables.rb', line 85

def [](name)
    begin
        if !name.kind_of?(String) or keys.include?(name)
            return super
        # elsif name =~ /^no(\S+)$/ and (keys.include?($1) or keys.include?(name))
        #     return !super($1)
        elsif name =~ /^\S+\(.*?\)$/
            m = /^(\S+)\((.*?)?\)$/.match(name)
            if m
                method = m[1]
                args   = m[2]
                args, text = @deplate.input.parse_args(args, nil, false)
                rv = @deplate.formatter.invoke_service(method, args, text)
                return rv
            else
                Deplate::Core.log(['Malformed variable name', name], :error)
            end
        elsif name =~ /^:(\S+)$/
            n = $1
            if @deplate.is_allowed?(':', :logger => self)
                return @deplate.options.send(n)
            end
        elsif (m = /^(\S+)\[(\S+)\]$/.match(name))
            key   = m[1]
            field = m[2]
            val   = self[key]
            return extract(val, key, field)
        elsif (m = /^(\S+)\.(\S+)$/.match(name))
            if @deplate.is_allowed?('.', :logger => self)
                key  = m[1]
                meth = m[2]
                val  = self[key]
                begin
                    return val.send(meth)
                rescue Exception => e
                    log(['Invoking method failed', meth, val.class], :error)
                end
            else
                log(['No permission', name], :anyway)
            end
        end
    rescue Exception => e
        Deplate::Core.log(['Retrieving doc variable failed', name, e], :error)
    end
    return nil
end

#[]=(name, value) ⇒ Object



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
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/deplate/variables.rb', line 40

def []=(name, value)
    if name =~ /^[[:upper:]]+?([@\[]|$)/ and !@priority
        Deplate::Core.log(['No permisson', name, value], :error)
        return
    end
    if name.kind_of?(String) and (m = /^(\S+)\[(\S+)?\]$/.match(name))
        key   = m[1]
        field = m[2]
        var   = self[key]
        if !var
            if !field
                self[key] = [value]
            elsif field =~ /^[0-9]$/
                self[key] = []
                self[key][field.to_i] = value
            else
                self[key] = {field => value}
            end
        elsif var.kind_of?(Struct) || var.kind_of?(OpenStruct)
            var.send("#{field}=", value)
        elsif var.kind_of?(Hash)
            var[field] = value
        elsif var.kind_of?(Array)
            if !field
                var << value
            elsif field =~ /^[0-9]+$/
                field = field.to_i
                var[field] = value
            else
                Deplate::Core.log(['Wrong index', field, name, var.class], :error)
            end
        elsif var
            Deplate::Core.log(['Doc variable has wrong type', key, var.class], :error)
        end
    else
        case name
        when 'encoding'
            if RUBY_VERSION >= '1.9.1'
                Encoding.default_external = Deplate::Encoding.ruby_enc_name(value)
            end
        end
        super(name, value)
    end
end

#extract(val, key, field) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/deplate/variables.rb', line 132

def extract(val, key, field)
    if val.nil?
        Deplate::Core.log(['Unknown variable', key], :error)
    elsif val.kind_of?(Struct) || val.kind_of?(OpenStruct)
        return val.send(field)
    elsif val.kind_of?(Hash)
        return val[field]
    elsif val.kind_of?(Array)
        if field =~ /^[0-9]+$/
            field = field.to_i
            return val[field]
        else
            Deplate::Core.log(['Wrong index', field, name, val.class], :error)
        end
    elsif val.respond_to?('[]')
        return val[field]
    else
        Deplate::Core.log(['Variable has wrong type', key, val.class], :warning)
    end
end

#log(*args) ⇒ Object



21
22
23
# File 'lib/deplate/variables.rb', line 21

def log(*args)
    Deplate::Core.log(*args)
end

#set_value(name, value) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/deplate/variables.rb', line 31

def set_value(name, value)
    begin
        @priority = true
        self[name] = value
    ensure
        @priority = false
    end
end

#update(hash) ⇒ Object



25
26
27
28
29
# File 'lib/deplate/variables.rb', line 25

def update(hash)
    for key, val in hash
        self[key] = val
    end
end