Class: RubyStringTemplate
- Inherits:
-
String
- Object
- String
- RubyStringTemplate
show all
- Defined in:
- lib/httpimagestore/ruby_string_template.rb
Defined Under Namespace
Classes: NoValueForTemplatePlaceholderError
Instance Method Summary
collapse
Constructor Details
Returns a new instance of RubyStringTemplate.
8
9
10
11
12
13
|
# File 'lib/httpimagestore/ruby_string_template.rb', line 8
def initialize(template, &resolver)
super(template.to_s)
@resolvers = []
@resolvers << resolver if resolver
@resolvers << ->(locals, name){locals[name]}
end
|
Instance Method Details
#add_missing_resolver(&resolver) ⇒ Object
40
41
42
|
# File 'lib/httpimagestore/ruby_string_template.rb', line 40
def add_missing_resolver(&resolver)
@resolvers << resolver
end
|
#initialize_copy(source) ⇒ Object
15
16
17
18
19
20
21
22
23
|
# File 'lib/httpimagestore/ruby_string_template.rb', line 15
def initialize_copy(source)
super
resolvers =
source.instance_eval do
resolvers = @resolvers
end
@resolvers = resolvers.dup
end
|
#inspect ⇒ Object
50
51
52
|
# File 'lib/httpimagestore/ruby_string_template.rb', line 50
def inspect
"T<#{to_s}>"
end
|
#render(locals = {}) ⇒ Object
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/httpimagestore/ruby_string_template.rb', line 25
def render(locals = {})
self.gsub(/#\{[^\}]+\}/um) do |placeholder|
name = placeholder.match(/#\{([^\}]*)\}/u).captures.first.to_sym
@resolvers.find do |resolver|
value = resolver.call(locals, name)
value and break value.to_s
end or fail NoValueForTemplatePlaceholderError.new(name, self)
end.to_s
end
|
#to_template ⇒ Object
36
37
38
|
# File 'lib/httpimagestore/ruby_string_template.rb', line 36
def to_template
self
end
|
#with_missing_resolver(&resolver) ⇒ Object
44
45
46
47
48
|
# File 'lib/httpimagestore/ruby_string_template.rb', line 44
def with_missing_resolver(&resolver)
new_template = self.dup
new_template.add_missing_resolver(&resolver)
new_template
end
|