Class: Mullet::RenderContext

Inherits:
Object
  • Object
show all
Defined in:
lib/mullet/render_context.rb

Overview

Holds the rendering context to reduce the number of parameters passed to render methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, missing_value_strategy, nil_value_strategy, output) ⇒ RenderContext

Constructor.

Parameters:

  • data (Object)

    provides data to render

  • missing_value_strategy (Proc)

    executed on attempt to render a variable that was not found

  • nil_value_strategy (Proc)

    executed on attempt to render nil value

  • output (#<<)

    where to write rendered output



22
23
24
25
26
27
28
29
# File 'lib/mullet/render_context.rb', line 22

def initialize(data, missing_value_strategy, nil_value_strategy, output)
  @scope = data.is_a?(DefaultNestedScope) ?
      data : DefaultNestedScope.new(data)
  @on_missing = missing_value_strategy
  @on_nil = nil_value_strategy
  @output = output
  @escape_xml_enabled = true
end

Instance Attribute Details

#escape_xml_enabledObject

Returns the value of attribute escape_xml_enabled.



10
11
12
# File 'lib/mullet/render_context.rb', line 10

def escape_xml_enabled
  @escape_xml_enabled
end

Instance Method Details

#<<(str) ⇒ Object

Writes rendered output.

Parameters:

  • str (String)

    string to write

Returns:

  • this object



84
85
86
87
# File 'lib/mullet/render_context.rb', line 84

def <<(str)
  @output << str
  return self
end

#escape_xml(input) ⇒ Object

Escapes characters that could be interpreted as XML markup if enabled.

Parameters:

  • input (String)

    input string

Returns:

  • escaped string, or the input string if escaping is disabled.



36
37
38
# File 'lib/mullet/render_context.rb', line 36

def escape_xml(input)
  return @escape_xml_enabled ? CGI.escape_html(input) : input
end

#get_display_value(key) ⇒ Object

Gets scope value that is intended for display in the rendered output. Applies configured strategies for handling missing and nil values.

Parameters:

  • key (Symbol)

    variable name

Returns:

  • value



68
69
70
71
72
73
74
75
76
77
# File 'lib/mullet/render_context.rb', line 68

def get_display_value(key)
  value = @scope.get_variable_value(key)
  if value == Scope::NOT_FOUND
    value = @on_missing.call(key)
  end
  if value == nil
    value = @on_nil.call(key)
  end
  return value
end

#get_variable_value(name) ⇒ Object

Resolves variable name to value.

Parameters:

  • name (Symbol)

    variable name

Returns:

  • value



45
46
47
# File 'lib/mullet/render_context.rb', line 45

def get_variable_value(name)
  return @scope.get_variable_value(name)
end

#pop_scopeObject

Removes innermost nested scope.



58
59
60
# File 'lib/mullet/render_context.rb', line 58

def pop_scope()
  @scope.pop_scope()
end

#push_scope(data) ⇒ Object

Adds a nested scope to search in subsequent lookups.

Parameters:

  • data

    data object



53
54
55
# File 'lib/mullet/render_context.rb', line 53

def push_scope(data)
  @scope.push_scope(data)
end