Module: Hanami::View::Escape

Defined in:
lib/hanami/view/escape.rb

Overview

Auto escape logic for views and presenters.

Since:

  • 0.4.0

Defined Under Namespace

Modules: InstanceMethods, Presentable Classes: Presenter

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Module extended override

Since:

  • 0.4.0



194
195
196
197
198
199
200
201
202
# File 'lib/hanami/view/escape.rb', line 194

def self.extended(base)
  base.class_eval do
    include ::Hanami::Utils::ClassAttribute
    include ::Hanami::View::Escape::InstanceMethods

    class_attribute :autoescape_methods
    self.autoescape_methods = {}
  end
end

.html(input) ⇒ Object, String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Escape the given input if it’s a string, otherwise return the oject as it is.

Parameters:

  • input (Object)

    the input

Returns:

  • (Object, String)

    the escaped string or the given object

Since:

  • 0.4.0



181
182
183
184
185
186
187
188
# File 'lib/hanami/view/escape.rb', line 181

def self.html(input)
  case input
  when String
    Utils::Escape.html(input)
  else
    input
  end
end

Instance Method Details

#method_added(method_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wraps concrete view methods with escape logic.

Since:

  • 0.4.0



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/hanami/view/escape.rb', line 208

def method_added(method_name)
  visibility = :public
  visibility = :private if private_method_defined? method_name
  visibility = :protected if protected_method_defined? method_name

  unless autoescape_methods[method_name]
    prepend Module.new {
      module_eval %{
        #{ visibility } def #{ method_name }(*args, &blk); ::Hanami::View::Escape.html super; end
      }
    }

    autoescape_methods[method_name] = true
  end
end