Sha256: 1a9e1e657b459872adbc7ba57f7489f039d8a41a35550abd3475c5e938e10814

Contents?: true

Size: 1010 Bytes

Versions: 1

Compression:

Stored size: 1010 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)
    presenter_class = case with
                      when nil
                        begin
                          StringPlus.constantize("#{object.class}Presenter")
                        rescue NameError
                          return object
                        end
                      when Symbol, String
                        StringPlus.constantize("#{with}Presenter")
                      else
                        with
                      end
    presenter_class.present(object, context, &block)
  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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mystique-0.5.6 lib/mystique.rb