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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RenderingContext

Returns a new instance of RenderingContext.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fortitude/rendering_context.rb', line 8

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

Instance Method Details

#about_to_output_non_whitespace!Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/fortitude/rendering_context.rb', line 157

def about_to_output_non_whitespace!
  if @newline_needed
    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)


66
67
68
# File 'lib/fortitude/rendering_context.rb', line 66

def attribute_validation_disabled?
  !! @attribute_validation_disabled
end

#current_element_nestingObject



123
124
125
# File 'lib/fortitude/rendering_context.rb', line 123

def current_element_nesting
  @current_element_nesting
end

#current_indentObject



143
144
145
# File 'lib/fortitude/rendering_context.rb', line 143

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

#current_widget_depthObject



101
102
103
# File 'lib/fortitude/rendering_context.rb', line 101

def current_widget_depth
  @current_widget_nesting.length - 1
end

#decrease_indent!Object



135
136
137
# File 'lib/fortitude/rendering_context.rb', line 135

def decrease_indent!
  @indent -= 1
end

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



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fortitude/rendering_context.rb', line 109

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



97
98
99
# File 'lib/fortitude/rendering_context.rb', line 97

def end_widget!(widget)
  # nothing here
end

#flush!Object



189
190
191
# File 'lib/fortitude/rendering_context.rb', line 189

def flush!
  # nothing here right now
end

#format_output?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/fortitude/rendering_context.rb', line 127

def format_output?
  true
end

#increase_indent!Object



131
132
133
# File 'lib/fortitude/rendering_context.rb', line 131

def increase_indent!
  @indent += 1
end

#needs_newline!Object



139
140
141
# File 'lib/fortitude/rendering_context.rb', line 139

def needs_newline!
  @newline_needed = true
end

#parent_widgetObject



105
106
107
# File 'lib/fortitude/rendering_context.rb', line 105

def parent_widget
  @current_widget_nesting[-2]
end

#record_render(args, &block) ⇒ Object



70
71
72
# File 'lib/fortitude/rendering_context.rb', line 70

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

#record_widget(widget) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fortitude/rendering_context.rb', line 74

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



93
94
95
# File 'lib/fortitude/rendering_context.rb', line 93

def start_widget!(widget)
  # nothing here
end

#validate_id_uniqueness(widget, tag_name, id) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/fortitude/rendering_context.rb', line 193

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



46
47
48
49
50
51
52
53
54
# File 'lib/fortitude/rendering_context.rb', line 46

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



36
37
38
39
40
41
42
43
44
# File 'lib/fortitude/rendering_context.rb', line 36

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



56
57
58
59
60
61
62
63
64
# File 'lib/fortitude/rendering_context.rb', line 56

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



147
148
149
150
151
152
153
154
155
# File 'lib/fortitude/rendering_context.rb', line 147

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



171
172
173
174
175
176
177
178
# File 'lib/fortitude/rendering_context.rb', line 171

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

#yield_from_widget(widget, *args) ⇒ Object



182
183
184
185
186
187
# File 'lib/fortitude/rendering_context.rb', line 182

def yield_from_widget(widget, *args)
  raise Fortitude::Errors::NoBlockToYieldTo.new(widget) unless @yield_block
  result = @yield_block.call(*args)
  @output_buffer_holder.output_buffer << result if @render_yield_result
  result
end