Class: ErbPP
- Inherits:
-
Object
show all
- Defined in:
- lib/erbpp.rb
Defined Under Namespace
Classes: ParamNotSetError
Constant Summary
collapse
- ATTRS =
[]
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(parent, erb_path, opts = {}) ⇒ ErbPP
Returns a new instance of ErbPP.
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/erbpp.rb', line 23
def initialize(parent,erb_path,opts={})
parents.push(parent) if parent
@erb_path = erb_path
@tmpl = @erb_path
@opts = opts
if @opts.class != Hash
raise ArgumentError, "option is not Hash"
end
@opts.each do |k,v|
ivar = ("@"+k.to_s).to_sym
instance_variable_set(ivar,v)
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(_meth_id, *args, &block) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/erbpp.rb', line 83
def method_missing(_meth_id, *args, &block)
ivar = "@"+_meth_id.to_s
if args.empty? and instance_variable_defined?(ivar)
parm = instance_variable_get(ivar)
if parm.nil?
raise ParamNotSetError,"parameter #{_meth_id.to_s} is not set"
end
parm
elsif args.size == 1 and attrs.include?(_meth_id.to_s)
instance_variable_set(ivar,args.first)
elsif x = search_method_in_parents(_meth_id)
x.send(_meth_id, *args, &block)
else
method_missing_alias(_meth_id, *args)
end
end
|
Class Method Details
.define_attrs(attrs) ⇒ Object
8
9
10
11
12
13
|
# File 'lib/erbpp.rb', line 8
def self.define_attrs(attrs)
attrs.each do |attr|
ivar = ("@"+attr).to_sym
define_method(attr){|*a| attr_method(ivar,*a)}
end
end
|
Instance Method Details
#attr_method(ivar, arg = nil) ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/erbpp.rb', line 15
def attr_method(ivar,arg=nil)
if arg.nil?
instance_variable_get(ivar)
else
instance_variable_set(ivar,arg)
end
end
|
#attrs ⇒ Object
64
65
66
|
# File 'lib/erbpp.rb', line 64
def attrs
self.class::ATTRS
end
|
#check_params(*params) ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'lib/erbpp.rb', line 72
def check_params(*params)
params.each do |x|
val = send(x)
if !val
raise ParamNotSetError,"parameter #{x.to_s} is not set"
end
end
end
|
#has_attr?(_meth_id) ⇒ Boolean
68
69
70
|
# File 'lib/erbpp.rb', line 68
def has_attr?(_meth_id)
respond_to?(_meth_id) or attrs.include?(_meth_id.to_s)
end
|
#load_erb ⇒ Object
39
40
41
42
43
44
|
# File 'lib/erbpp.rb', line 39
def load_erb
safe_level = nil
trim_mode = '%<>'
@erb = ERB.new(File.read(@erb_path),safe_level,trim_mode)
@erb.filename = @erb_path
end
|
#method_missing_alias ⇒ Object
81
|
# File 'lib/erbpp.rb', line 81
alias method_missing_alias method_missing
|
#parents ⇒ Object
46
47
48
|
# File 'lib/erbpp.rb', line 46
def parents
@parents ||= []
end
|
#result ⇒ Object
105
106
107
108
|
# File 'lib/erbpp.rb', line 105
def result
load_erb unless @erb
@erb.result(binding)
end
|
#run ⇒ Object
100
101
102
103
|
# File 'lib/erbpp.rb', line 100
def run
load_erb unless @erb
@erb.run(binding)
end
|
#search_method_in_parents(_meth_id) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/erbpp.rb', line 50
def search_method_in_parents(_meth_id)
parents.each do |x|
if x.has_attr? _meth_id
return x
end
end
parents.each do |x|
if f = x.search_method_in_parents(_meth_id)
return f
end
end
nil
end
|