Module: HasTemplates
- Included in:
- GeoEngineer::Environment, GeoEngineer::Project
- Defined in:
- lib/geoengineer/utils/has_templates.rb
Overview
HasTemplates provides methods for a class to contain and query a set of templates
Instance Method Summary collapse
- #all_template_resources ⇒ Object
-
#find_template(type) ⇒ Object
Templating Methods.
- #from_template(type, name, parameters = {}, &block) ⇒ Object
- #templates ⇒ Object
Instance Method Details
#all_template_resources ⇒ Object
28 29 30 |
# File 'lib/geoengineer/utils/has_templates.rb', line 28 def all_template_resources templates.values.map(&:all_resources).flatten end |
#find_template(type) ⇒ Object
Templating Methods
10 11 12 13 14 15 16 17 18 |
# File 'lib/geoengineer/utils/has_templates.rb', line 10 def find_template(type) clazz_name = type.split('_').collect(&:capitalize).join return Object.const_get(clazz_name) if Object.const_defined? clazz_name module_clazz = "GeoEngineer::Templates::#{clazz_name}" return Object.const_get(module_clazz) if Object.const_defined? module_clazz throw "undefined template '#{type}' for '#{clazz_name}' or '#{module_clazz}'" end |
#from_template(type, name, parameters = {}, &block) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/geoengineer/utils/has_templates.rb', line 20 def from_template(type, name, parameters = {}, &block) throw "Template '#{name}' already defined" if templates[name] clazz = find_template(type) template = clazz.new(name, self, parameters) template.instance_exec(*template.template_resources, &block) if block_given? templates[name] = template end |
#templates ⇒ Object
5 6 7 |
# File 'lib/geoengineer/utils/has_templates.rb', line 5 def templates @_templates ||= {} end |