Sha256: 824bef6fa3f553fa312d5aa51d1b4ba9f69190b316d24e7da63b5d8483536739

Contents?: true

Size: 765 Bytes

Versions: 3

Compression:

Stored size: 765 Bytes

Contents

module Bourgeois
  module ViewHelper
    # Wrap a resource or a collection into its related presenter
    #
    # @example
    #   present User.new(name: 'Remi') do |user|
    #     puts user.inspect # => #<UserPresenter object=#<User name="Remi>>
    #     puts user.name # => Remi
    #   end
    def present(object, klass = nil, &blk)
      return object.map { |o| present(o, klass, &blk) } if object.respond_to?(:to_a)

      if klass.blank?
        begin
          klass_name = "#{object.class}Presenter"
          klass = klass_name.constantize
        rescue ::NameError
          raise UnknownPresenter.new(klass_name)
        end
      end

      presenter = klass.new(object, self)
      yield presenter if block_given?

      presenter
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bourgeois-0.1.5 lib/bourgeois/view_helper.rb
bourgeois-0.1.3 lib/bourgeois/view_helper.rb
bourgeois-0.1.2 lib/bourgeois/view_helper.rb