Module: Hanami::Presenter

Defined in:
gems/gems/hanami-view-1.3.1/lib/hanami/presenter.rb

Overview

Presenter pattern implementation

It delegates to the wrapped object the missing method invocations.

The output of concrete and delegated methods is escaped as XSS prevention.

Examples:

Basic usage

require 'hanami/view'

class Map
  attr_reader :locations

  def initialize(locations)
    @locations = locations
  end

  def location_names
    @locations.join(', ')
  end
end

class MapPresenter
  include Hanami::Presenter

  def count
    locations.count
  end

  def location_names
    super.upcase
  end

  def inspect_object
    @object.inspect
  end
end

map = Map.new(['Rome', 'Boston'])
presenter = MapPresenter.new(map)

# access a map method
puts presenter.locations # => ['Rome', 'Boston']

# access presenter concrete methods
puts presenter.count # => 1

# uses super to access original object implementation
puts presenter.location_names # => 'ROME, BOSTON'

# it has private access to the original object
puts presenter.inspect_object # => #<Map:0x007fdeada0b2f0 @locations=["Rome", "Boston"]>

Escape

require 'hanami/view'

User = Struct.new(:first_name, :last_name)

class UserPresenter
  include Hanami::Presenter

  def full_name
    [first_name, last_name].join(' ')
  end

  def raw_first_name
    _raw first_name
  end
end

first_name = '<script>alert('xss')</script>'

user = User.new(first_name, nil)
presenter = UserPresenter.new(user)

presenter.full_name
  # => "&lt;script&gt;alert(&apos;xss&apos;)&lt;&#x2F;script&gt;"

presenter.raw_full_name
   # => "<script>alert('xss')</script>"

Since:

  • 0.1.0