Class: Sho::Configurator

Inherits:
Object
  • Object
show all
Defined in:
lib/sho/configurator.rb

Overview

Main Sho object providing rendering method creation API.

There are three ways to create rendering methods:

Examples:

class AnyClass
  include Sho

  # `sho` returns an instance of Configurator
  sho.template :rendering_method_name, 'path/to/template.slim', :param1, param2: default_value
end

Instance Attribute Summary collapse

Instance Method Summary collapse

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_folderString?

Returns folder to look templates at for #template method. ‘nil` by default, meaning application’s current folder (‘Dir.pwd`).

Returns:

  • (String, nil)

    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

#hostObject (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.

Examples:

# generates method with signature #profile()
sho.template :profile, 'app/views/users/profile.slim'

# generates method with signature #profile(context:, detailed: false)
# `context` and `detailed` vairables are accessible inside template.
sho.template :profile, 'app/views/users/profile.slim', :context, detailed: false

Parameters:

  • name (Symbol)

    name of method to generate;

  • template (String)

    path to template to render;

  • _layout (Symbol, nil) (defaults to: nil)

    name of method which provides layout (wraps results of current method);

  • mandatory (Array<Symbol>)

    list of mandatory params;

  • optional (Hash{Symbol => Object})

    list of optional params and their default values



55
56
57
58
# File 'lib/sho/configurator.rb', line 55

def template(name, template, *mandatory, _layout: nil, **optional)
  tpl = Tilt.new(File.expand_path(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.

Examples:

sho.inline_template :badge,
  slim: <<~SLIM
    span.badge
      span.name = user.name
      i.role(class: user.role)
  SLIM

Parameters:

  • name (Symbol)

    name of method to generate;

  • _layout (Symbol, nil) (defaults to: nil)

    name of method which provides layout (wraps results of current method);

  • mandatory (Array<Symbol>)

    list of mandatory params;

  • options (Hash{Symbol => Object})

    list of optional params and their default values + template to render (passed in a key named ‘slim:` or `erb:` or `haml:`, and so on).



101
102
103
104
105
106
107
108
# File 'lib/sho/configurator.rb', line 101

def template_inline(name, *mandatory, _layout: nil, **options)
  kind, template = options.detect { |key,| Tilt.registered?(key.to_s) }
  template or fail ArgumentError, "No known templates found in #{options.keys}"
  optional = options.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

“‘

Parameters:

  • name (Symbol)

    name of method to generate;

  • template (String)

    path to template to render;

  • _layout (Symbol, nil) (defaults to: nil)

    name of method which provides layout (wraps results of current method);

  • mandatory (Array<Symbol>)

    list of mandatory params;

  • optional (Hash{Symbol => Object})

    list of optional params and their default values



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.expand_path(template, base))

  define_template_method(name, tpl, mandatory, optional, _layout)
end