Class: Padrino::SafeBuffer

Inherits:
String show all
Defined in:
padrino-helpers/lib/padrino/safe_buffer.rb

Overview

Padrino::SafeBuffer is based on ActiveSupport::SafeBuffer

Defined Under Namespace

Classes: SafeConcatError

Constant Summary collapse

UNSAFE_STRING_METHODS =
%w(
  capitalize chomp chop delete downcase gsub lstrip next reverse rstrip
  slice squeeze strip sub succ swapcase tr tr_s upcase
)

Instance Method Summary collapse

Methods inherited from String

#camelize, #classify, #colorize, #constantize, #html_safe, #pluralize, #underscore

Constructor Details

#initialize(str = "") ⇒ SafeBuffer

Returns a new instance of SafeBuffer



43
44
45
46
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 43

def initialize(str = "")
  @html_safe = true
  super
end

Instance Method Details

#%(args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 70

def %(args)
  case args
  when Hash
    escaped_args = Hash[args.map { |k,arg| [k, html_escape_interpolated_argument(arg)] }]
  else
    escaped_args = Array(args).map { |arg| html_escape_interpolated_argument(arg) }
  end

  self.class.new(super(escaped_args))
end

#+(other) ⇒ Object



66
67
68
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 66

def +(other)
  dup.concat(other)
end

#[](*args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 20

def [](*args)
  if args.size < 2
    super
  else
    if html_safe?
      new_safe_buffer = super

      if new_safe_buffer
        new_safe_buffer.instance_variable_set :@html_safe, true
      end

      new_safe_buffer
    else
      to_str[*args]
    end
  end
end

#clone_emptyObject



53
54
55
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 53

def clone_empty
  self[0, 0]
end

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



57
58
59
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 57

def concat(value)
  super(html_escape_interpolated_argument(value))
end

#encode_with(coder) ⇒ Object



93
94
95
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 93

def encode_with(coder)
  coder.represent_object nil, to_str
end

#html_safe?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 81

def html_safe?
  defined?(@html_safe) && @html_safe
end

#initialize_copy(other) ⇒ Object



48
49
50
51
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 48

def initialize_copy(other)
  super
  @html_safe = other.html_safe?
end

#prepend(value) ⇒ Object



62
63
64
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 62

def prepend(value)
  super(html_escape_interpolated_argument(value))
end

#safe_concat(value) ⇒ Object

Raises:



38
39
40
41
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 38

def safe_concat(value)
  raise SafeConcatError unless html_safe?
  original_concat(value)
end

#to_paramObject



89
90
91
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 89

def to_param
  to_str
end

#to_sObject



85
86
87
# File 'padrino-helpers/lib/padrino/safe_buffer.rb', line 85

def to_s
  self
end