Sha256: 3fd93519d4c6973b9e6041121acf0bd457966674cc60e237936b00d8800b6f29

Contents?: true

Size: 715 Bytes

Versions: 8

Compression:

Stored size: 715 Bytes

Contents

class RubyStringTemplate
	class NoValueForTemplatePlaceholderError < ArgumentError
		def initialize(name, template)
			super "no value for '\#{#{name}}' in template '#{template}'"
		end
	end
	
	def initialize(template, &resolver)
		@template = template.to_s
		@resolver = resolver ? resolver : ->(locals, name){locals[name]}
	end

	def render(locals = {})
		template = @template
		while tag = template.match(/(#\{[^\}]+\})/m)
			tag = tag.captures.first
			name = tag.match(/#\{([^\}]*)\}/).captures.first.to_sym
			value = @resolver.call(locals, name)
			value or fail NoValueForTemplatePlaceholderError.new(name, @template)
			value = value.to_s
			template = template.gsub(tag, value)
		end
		template
	end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
httpimagestore-1.6.0 lib/httpimagestore/ruby_string_template.rb
httpimagestore-1.5.0 lib/httpimagestore/ruby_string_template.rb
httpimagestore-1.4.1 lib/httpimagestore/ruby_string_template.rb
httpimagestore-1.4.0 lib/httpimagestore/ruby_string_template.rb
httpimagestore-1.3.0 lib/httpimagestore/ruby_string_template.rb
httpimagestore-1.2.0 lib/httpimagestore/ruby_string_template.rb
httpimagestore-1.1.0 lib/httpimagestore/ruby_string_template.rb
httpimagestore-1.0.0 lib/httpimagestore/ruby_string_template.rb