Class: ActionView::OutputBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/action_view/buffers.rb

Overview

Used as a buffer for views

The main difference between this and ActiveSupport::SafeBuffer is for the methods ‘<<` and `safe_expr_append=` the inputs are checked for nil before they are assigned and `to_s` is called on the input. For example:

obuf = ActionView::OutputBuffer.new "hello"
obuf << 5
puts obuf # => "hello5"

sbuf = ActiveSupport::SafeBuffer.new "hello"
sbuf << 5
puts sbuf # => "hello\u0005"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer = "") ⇒ OutputBuffer

:nodoc:



22
23
24
25
# File 'lib/action_view/buffers.rb', line 22

def initialize(buffer = "")
  @raw_buffer = String.new(buffer)
  @raw_buffer.encode!
end

Instance Attribute Details

#raw_bufferObject (readonly)

Returns the value of attribute raw_buffer.



89
90
91
# File 'lib/action_view/buffers.rb', line 89

def raw_buffer
  @raw_buffer
end

Instance Method Details

#<<(value) ⇒ Object Also known as: concat, append=



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/action_view/buffers.rb', line 42

def <<(value)
  unless value.nil?
    value = value.to_s
    @raw_buffer << if value.html_safe?
      value
    else
      CGI.escapeHTML(value)
    end
  end
  self
end

#==(other) ⇒ Object



81
82
83
# File 'lib/action_view/buffers.rb', line 81

def ==(other)
  other.class == self.class && @raw_buffer == other.to_str
end

#capture(*args) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/action_view/buffers.rb', line 72

def capture(*args)
  new_buffer = +""
  old_buffer, @raw_buffer = @raw_buffer, new_buffer
  yield(*args)
  new_buffer.html_safe
ensure
  @raw_buffer = old_buffer
end

#html_safe?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/action_view/buffers.rb', line 38

def html_safe?
  true
end

#initialize_copy(other) ⇒ Object



68
69
70
# File 'lib/action_view/buffers.rb', line 68

def initialize_copy(other)
  @raw_buffer = other.to_str
end

#rawObject



85
86
87
# File 'lib/action_view/buffers.rb', line 85

def raw
  RawOutputBuffer.new(self)
end

#safe_concat(value) ⇒ Object Also known as: safe_append=



56
57
58
59
# File 'lib/action_view/buffers.rb', line 56

def safe_concat(value)
  @raw_buffer << value
  self
end

#safe_expr_append=(val) ⇒ Object



62
63
64
65
66
# File 'lib/action_view/buffers.rb', line 62

def safe_expr_append=(val)
  return self if val.nil?
  @raw_buffer << val.to_s
  self
end

#to_sObject Also known as: html_safe



29
30
31
# File 'lib/action_view/buffers.rb', line 29

def to_s
  @raw_buffer.html_safe
end

#to_strObject



34
35
36
# File 'lib/action_view/buffers.rb', line 34

def to_str
  @raw_buffer.dup
end