Sha256: 02671a9ae4de61f7135974f5fe748b7792c6ae8263c9b25ef38deae40868bed6

Contents?: true

Size: 969 Bytes

Versions: 1

Compression:

Stored size: 969 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.for(object, context, &block)
    rescue NameError
      return object
    end
  end

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

    collection.each do |element|
      case block.arity
      when 2
        block.call( present(element, context: context, with: with), element )
      else
        block.call(present(element, context: context, with: with))
      end
    end
  end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mystique-0.10.0 lib/mystique.rb