Sha256: 9912c4024744b0c8300bc388c059b7695363147fe0e0189cff7d8070c18ceed8

Contents?: true

Size: 699 Bytes

Versions: 4

Compression:

Stored size: 699 Bytes

Contents

module Sinatra
  module Decorator

    module DecorateHelpers
      def decorate(object, options = {})

        klass = options[:as]

        if object.respond_to?(:first)
          return [] if object.empty?
          klass_name = "#{object.first.class}Decorator"
          klass = klass_name.constantize if klass.nil?
          decorator = object.map{|o| klass.new(o, context: self)}
        else
          klass_name = "#{object.class}Decorator"
          klass = klass_name.constantize if klass.nil?
          decorator = klass.new(object, context: self)
        end

        yield decorator if block_given?
        decorator
      end
    end # DecorateHelpers

  end # Decorator
end # Sinatra

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sinatra-decorator-0.2.0 lib/sinatra/decorator/decorate_helpers.rb
sinatra-decorator-0.1.1 lib/sinatra/decorator/decorate_helpers.rb
sinatra-decorator-0.0.2 lib/sinatra/decorator/decorate_helpers.rb
sinatra-decorator-0.0.1 lib/sinatra-decorator/decorate_helpers.rb