Class: Fortitude::RenderingContext

Inherits:
Object
  • Object
show all
Defined in:
lib/fortitude/rendering_context.rb

Defined Under Namespace

Classes: OutputBufferHolder

Constant Summary collapse

NEWLINE =
"\n"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RenderingContext

Returns a new instance of RenderingContext.


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fortitude/rendering_context.rb', line 14

def initialize(options)
  options.assert_valid_keys(:delegate_object, :output_buffer_holder, :helpers_object, :instance_variables_object,
    :yield_block, :render_yield_result)

  @output_buffer_holder = options[:output_buffer_holder]
  if (! @output_buffer_holder) && options[:delegate_object] && options[:delegate_object].respond_to?(:output_buffer)
    @output_buffer_holder = options[:delegate_object]
  end
  @output_buffer_holder ||= OutputBufferHolder.new
  @helpers_object = options[:helpers_object] || options[:delegate_object] || Object.new

  instance_variables_object = options[:instance_variables_object] || options[:delegate_object] || Object.new
  @instance_variable_set = Fortitude::Support::InstanceVariableSet.new(instance_variables_object)

  @render_yield_result = true unless options.has_key?(:render_yield_result) && (! options[:render_yield_result])

  @indent = 0
  @newline_needed = false
  @have_output = false
  @indenting_disabled = false

  @current_element_nesting = [ ]
  @current_widget_nesting = [ ]
  @ids_used = { }

  @yield_block = options[:yield_block]
end

Instance Attribute Details

#helpers_objectObject (readonly)

Returns the value of attribute helpers_object


6
7
8
# File 'lib/fortitude/rendering_context.rb', line 6

def helpers_object
  @helpers_object
end

#instance_variable_setObject (readonly)

Returns the value of attribute instance_variable_set


6
7
8
# File 'lib/fortitude/rendering_context.rb', line 6

def instance_variable_set
  @instance_variable_set
end

#output_buffer_holderObject (readonly)

Returns the value of attribute output_buffer_holder


6
7
8
# File 'lib/fortitude/rendering_context.rb', line 6

def output_buffer_holder
  @output_buffer_holder
end

Class Method Details

.default_rendering_contextObject


9
10
11
# File 'lib/fortitude/rendering_context.rb', line 9

def default_rendering_context
  new({ })
end

Instance Method Details

#about_to_output_non_whitespace!Object


172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/fortitude/rendering_context.rb', line 172

def about_to_output_non_whitespace!
  if @newline_needed && ((@suppress_formatting_level ||= 0) == 0)
    if @have_output
      o = @output_buffer_holder.output_buffer
      o.original_concat(NEWLINE)
      o.original_concat(current_indent) unless @indenting_disabled
    end

    @newline_needed = false
  end

  @have_output = true
end

#attribute_validation_disabled?Boolean

Returns:

  • (Boolean)

72
73
74
# File 'lib/fortitude/rendering_context.rb', line 72

def attribute_validation_disabled?
  !! @attribute_validation_disabled
end

#current_element_nestingObject


129
130
131
# File 'lib/fortitude/rendering_context.rb', line 129

def current_element_nesting
  @current_element_nesting
end

#current_indentObject


158
159
160
# File 'lib/fortitude/rendering_context.rb', line 158

def current_indent
  ("  " * @indent).freeze
end

#current_widget_depthObject


107
108
109
# File 'lib/fortitude/rendering_context.rb', line 107

def current_widget_depth
  @current_widget_nesting.length - 1
end

#decrease_indent!Object


141
142
143
# File 'lib/fortitude/rendering_context.rb', line 141

def decrease_indent!
  @indent -= 1
end

#desuppress_formatting!Object


154
155
156
# File 'lib/fortitude/rendering_context.rb', line 154

def desuppress_formatting!
  @suppress_formatting_level -= 1
end

#effective_yield_blockObject


197
198
199
200
201
202
203
204
205
# File 'lib/fortitude/rendering_context.rb', line 197

def effective_yield_block
  if @yield_block
    lambda do |*args|
      result = @yield_block.call(*args)
      @output_buffer_holder.output_buffer << result if @render_yield_result
      result
    end
  end
end

#emitting_tag!(widget, tag_object, content_or_attributes, attributes) ⇒ Object


115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fortitude/rendering_context.rb', line 115

def emitting_tag!(widget, tag_object, content_or_attributes, attributes)
  validate_element_for_rules(widget, tag_object) if widget.class.enforce_element_nesting_rules
  @current_element_nesting << tag_object

  begin
    yield
  ensure
    last = @current_element_nesting.pop
    unless last.equal?(tag_object)
      raise "Something horrible happened -- the last tag we started was #{last}, but now we're ending #{tag_object}?!?"
    end
  end
end

#end_widget!(widget) ⇒ Object


103
104
105
# File 'lib/fortitude/rendering_context.rb', line 103

def end_widget!(widget)
  # nothing here
end

#flush!Object


207
208
209
# File 'lib/fortitude/rendering_context.rb', line 207

def flush!
  # nothing here right now
end

#format_output?Boolean

Returns:

  • (Boolean)

133
134
135
# File 'lib/fortitude/rendering_context.rb', line 133

def format_output?
  true
end

#increase_indent!Object


137
138
139
# File 'lib/fortitude/rendering_context.rb', line 137

def increase_indent!
  @indent += 1
end

#needs_newline!Object


145
146
147
# File 'lib/fortitude/rendering_context.rb', line 145

def needs_newline!
  @newline_needed = true
end

#parent_widgetObject


111
112
113
# File 'lib/fortitude/rendering_context.rb', line 111

def parent_widget
  @current_widget_nesting[-2]
end

#record_render(args, &block) ⇒ Object


76
77
78
# File 'lib/fortitude/rendering_context.rb', line 76

def record_render(args, &block)
  record_widget(::Fortitude::Tags::RenderWidgetPlaceholder.new(args), &block)
end

#record_widget(widget) ⇒ Object


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fortitude/rendering_context.rb', line 80

def record_widget(widget)
  start_widget!(widget)
  @current_widget_nesting << widget
  @current_element_nesting << widget
  begin
    yield
  ensure
    last = @current_widget_nesting.pop
    unless last.equal?(widget)
      raise "Something horrible happened -- the last widget we started was #{last}, but now we're ending #{widget}?!?"
    end
    last = @current_element_nesting.pop
    unless last.equal?(widget)
      raise "Something horrible happened -- the last element we started was #{last}, but now we're ending #{widget}?!?"
    end
    end_widget!(widget)
  end
end

#start_widget!(widget) ⇒ Object


99
100
101
# File 'lib/fortitude/rendering_context.rb', line 99

def start_widget!(widget)
  # nothing here
end

#suppress_formatting!Object


149
150
151
152
# File 'lib/fortitude/rendering_context.rb', line 149

def suppress_formatting!
  @suppress_formatting_level ||= 0
  @suppress_formatting_level += 1
end

#validate_id_uniqueness(widget, tag_name, id) ⇒ Object


211
212
213
214
215
216
217
218
219
# File 'lib/fortitude/rendering_context.rb', line 211

def validate_id_uniqueness(widget, tag_name, id)
  id = id.to_s
  if @ids_used[id] && (! @id_uniqueness_disabled)
    (already_used_widget, already_used_tag_name) = @ids_used[id]
    raise Fortitude::Errors::DuplicateId.new(widget, id, already_used_widget, already_used_tag_name, tag_name)
  else
    @ids_used[id] = [ widget, tag_name ]
  end
end

#with_attribute_validation(value) ⇒ Object


52
53
54
55
56
57
58
59
60
# File 'lib/fortitude/rendering_context.rb', line 52

def with_attribute_validation(value)
  old_value = @attribute_validation_disabled
  @attribute_validation_disabled = !value
  begin
    yield
  ensure
    @attribute_validation_disabled = old_value
  end
end

#with_element_nesting_validation(value) ⇒ Object


42
43
44
45
46
47
48
49
50
# File 'lib/fortitude/rendering_context.rb', line 42

def with_element_nesting_validation(value)
  old_value = @element_nesting_validation_disabled
  @element_nesting_validation_disabled = !value
  begin
    yield
  ensure
    @element_nesting_validation_disabled = old_value
  end
end

#with_id_uniqueness(value) ⇒ Object


62
63
64
65
66
67
68
69
70
# File 'lib/fortitude/rendering_context.rb', line 62

def with_id_uniqueness(value)
  old_value = @id_uniqueness_disabled
  @id_uniqueness_disabled = !value
  begin
    yield
  ensure
    @id_uniqueness_disabled = old_value
  end
end

#with_indenting_disabledObject


162
163
164
165
166
167
168
169
170
# File 'lib/fortitude/rendering_context.rb', line 162

def with_indenting_disabled
  old_indenting_disabled = @indenting_disabled
  @indenting_disabled = true
  begin
    yield
  ensure
    @indenting_disabled = old_indenting_disabled
  end
end

#with_yield_block(new_yield_block) ⇒ Object


186
187
188
189
190
191
192
193
# File 'lib/fortitude/rendering_context.rb', line 186

def with_yield_block(new_yield_block)
  old_yield_block, @yield_block = @yield_block, new_yield_block
  begin
    yield
  ensure
    @yield_block = old_yield_block
  end
end