Sha256: 30955179ae713a5f0a16166603e8f18135cd80bc9d4d68d9c59359fb885bc4f4

Contents?: true

Size: 864 Bytes

Versions: 2

Compression:

Stored size: 864 Bytes

Contents

require "mystique/version"

require "callable"
require "string_plus"

require "mystique/undefined"
require "mystique/presenter"

module Mystique
  module_function

  def present(object, with: nil, context: nil, &block)
    begin
      presenter_class = presenter_class_for(object, with)
      presenter_class.present(object, context, &block)
    rescue NameError
      return object
    end
  end

  def present_collection(collection, context: nil, &block)
    return to_enum(:present_collection, collection, context: context, &block) unless block_given?

    collection.each do |element|
      block.call present(element, context: context)
    end
  end

  def presenter_class_for(object, with)
    if with.respond_to?(:present)
      with
    else
      StringPlus.constantize("#{with || object.class}Presenter")
    end
  end
  private :presenter_class_for
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mystique-0.7.0 lib/mystique.rb
mystique-0.6.0 lib/mystique.rb