Module: Lotus::Presenter

Defined in:
lib/lotus/presenter.rb

Overview

Presenter pattern implementation

Examples:

require 'lotus/view'

class Map
  attr_reader :locations

  def initialize(locations)
    @locations = locations
  end

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

class MapPresenter
  include Lotus::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"]>

Since:

  • 0.1.0

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &blk) ⇒ Object (protected)

Since:

  • 0.1.0



62
63
64
65
66
67
68
# File 'lib/lotus/presenter.rb', line 62

def method_missing(m, *args, &blk)
  if @object.respond_to?(m)
    @object.__send__ m, *args, &blk
  else
    super
  end
end

Instance Method Details

#initialize(object) ⇒ Object

Initialize the presenter

Parameters:

  • object (Object)

    the object to present

Since:

  • 0.1.0



57
58
59
# File 'lib/lotus/presenter.rb', line 57

def initialize(object)
  @object = object
end