Class: Sho::Configurator
- Inherits:
-
Object
- Object
- Sho::Configurator
- Defined in:
- lib/sho/configurator.rb
Overview
Main Sho object providing rendering method creation API.
There are three ways to create rendering methods:
-
#template: template is looked up relative to main folder, or #base_folder;
-
#template_relative: template is looked up relative to current class’ folder;
-
#inline_template: template is provided inline as a Ruby string/heredoc.
Instance Attribute Summary collapse
-
#base_folder ⇒ String?
Folder to look templates at for #template method.
- #host ⇒ Object readonly
Instance Method Summary collapse
-
#initialize(host) ⇒ Configurator
constructor
A new instance of Configurator.
-
#template(name, template, *mandatory, _layout: nil, **optional) ⇒ Object
Generates instance method named ‘name` in a host module, which renders template from `template`.
-
#template_inline(name, *mandatory, _layout: nil, **options) ⇒ Object
(also: #inline_template)
Inline rendering method definition, useful in decorators and other contexts with small templates.
-
#template_relative(name, template, *mandatory, _layout: nil, **optional) ⇒ Object
Like #template, but looks up template relative to host module path.
Constructor Details
#initialize(host) ⇒ Configurator
Returns a new instance of Configurator.
31 32 33 |
# File 'lib/sho/configurator.rb', line 31 def initialize(host) @host = host end |
Instance Attribute Details
#base_folder ⇒ String?
Returns folder to look templates at for #template method. ‘nil` by default, meaning application’s current folder (‘Dir.pwd`).
28 29 30 |
# File 'lib/sho/configurator.rb', line 28 def base_folder @base_folder end |
#host ⇒ Object (readonly)
24 25 26 |
# File 'lib/sho/configurator.rb', line 24 def host @host end |
Instance Method Details
#template(name, template, *mandatory, _layout: nil, **optional) ⇒ Object
Generates instance method named ‘name` in a host module, which renders template from `template`. Instance of the host class is passed as a template scope on rendering.
Template is looked up relative to application’s main folder, or #base_folder.
55 56 57 58 |
# File 'lib/sho/configurator.rb', line 55 def template(name, template, *mandatory, _layout: nil, **optional) tpl = Tilt.new(File.(template, base_folder || Dir.pwd)) define_template_method(name, tpl, mandatory, optional, _layout) end |
#template_inline(name, *mandatory, _layout: nil, **options) ⇒ Object Also known as: inline_template
Inline rendering method definition, useful in decorators and other contexts with small templates.
101 102 103 104 105 106 107 108 |
# File 'lib/sho/configurator.rb', line 101 def template_inline(name, *mandatory, _layout: nil, **) kind, template = .detect { |key,| Tilt.registered?(key.to_s) } template or fail ArgumentError, "No known templates found in #{options.keys}" optional = .reject { |key,| key == kind } tpl = Tilt.default_mapping[kind].new { template } define_template_method(name, tpl, mandatory, optional, _layout) end |
#template_relative(name, template, *mandatory, _layout: nil, **optional) ⇒ Object
Like #template, but looks up template relative to host module path. Allows to structure views like:
“‘ app/ +- view_models/
+- users.rb # calls sho.template_relative :profile, 'users/profile.slim'
+- users/
+- profile.slim
“‘
77 78 79 80 81 82 |
# File 'lib/sho/configurator.rb', line 77 def template_relative(name, template, *mandatory, _layout: nil, **optional) base = File.dirname(caller(1..1).first.split(':').first) tpl = Tilt.new(File.(template, base)) define_template_method(name, tpl, mandatory, optional, _layout) end |