Class: HighLine::TemplateRenderer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/highline/template_renderer.rb

Overview

Renders an erb template taking a Question and a HighLine instance as context.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, source, highline) ⇒ TemplateRenderer

Initializes the TemplateRenderer object with its template and HighLine and Question contexts.

Parameters:

  • template (ERB)

    ERB template.

  • source (Question)

    question object.

  • highline (HighLine)

    HighLine instance.



30
31
32
33
34
# File 'lib/highline/template_renderer.rb', line 30

def initialize(template, source, highline)
  @template = template
  @source   = source
  @highline = highline
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ String

Returns an error message when the called method is not available.

Returns:



44
45
46
47
48
# File 'lib/highline/template_renderer.rb', line 44

def method_missing(method, *args)
  "Method #{method} with args #{args.inspect} " \
    "is not available on #{inspect}. " \
    "Try #{methods(false).sort.inspect}"
end

Instance Attribute Details

#highlineHighLine (readonly)

Returns HighLine instance used as context.

Returns:

  • (HighLine)

    HighLine instance used as context



21
22
23
# File 'lib/highline/template_renderer.rb', line 21

def highline
  @highline
end

#sourceQuestion, Menu (readonly)

Returns Question instance used as context.

Returns:



18
19
20
# File 'lib/highline/template_renderer.rb', line 18

def source
  @source
end

#templateERB (readonly)

Returns ERB template being rendered.

Returns:

  • (ERB)

    ERB template being rendered



15
16
17
# File 'lib/highline/template_renderer.rb', line 15

def template
  @template
end

Class Method Details

.const_missing(name) ⇒ Object

If some constant is missing at this TemplateRenderer instance, get it from HighLine. Useful to get color and style contants.

Parameters:

  • name (Symbol)

    automatically passed constant’s name as Symbol



58
59
60
# File 'lib/highline/template_renderer.rb', line 58

def self.const_missing(name)
  HighLine.const_get(name)
end

Instance Method Details

Returns #source attribute.

Returns:



51
52
53
# File 'lib/highline/template_renderer.rb', line 51

def menu
  source
end

#renderString

Returns rendered template.

Returns:

  • (String)

    rendered template



37
38
39
# File 'lib/highline/template_renderer.rb', line 37

def render
  template.result(binding)
end