Class: Mullet::RenderContext
- Inherits:
-
Object
- Object
- Mullet::RenderContext
- 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
-
#escape_xml_enabled ⇒ Object
Returns the value of attribute escape_xml_enabled.
Instance Method Summary collapse
-
#<<(str) ⇒ Object
Writes rendered output.
-
#escape_xml(input) ⇒ Object
Escapes characters that could be interpreted as XML markup if enabled.
-
#get_display_value(key) ⇒ Object
Gets scope value that is intended for display in the rendered output.
-
#get_variable_value(name) ⇒ Object
Resolves variable name to value.
-
#initialize(data, missing_value_strategy, nil_value_strategy, output) ⇒ RenderContext
constructor
Constructor.
-
#pop_scope ⇒ Object
Removes innermost nested scope.
-
#push_scope(data) ⇒ Object
Adds a nested scope to search in subsequent lookups.
Constructor Details
#initialize(data, missing_value_strategy, nil_value_strategy, output) ⇒ RenderContext
Constructor.
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_enabled ⇒ Object
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.
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.
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.
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.
45 46 47 |
# File 'lib/mullet/render_context.rb', line 45 def get_variable_value(name) return @scope.get_variable_value(name) end |
#pop_scope ⇒ Object
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.
53 54 55 |
# File 'lib/mullet/render_context.rb', line 53 def push_scope(data) @scope.push_scope(data) end |