Sha256: 710d7508ca8fcc9f798f83435cd7704271a37ddce0803331e2e23f60fe74ba4b

Contents?: true

Size: 1.88 KB

Versions: 24

Compression:

Stored size: 1.88 KB

Contents

module Riveter
  module Presenter
    module Object
      extend ActiveSupport::Concern

      def to_presenter(klass)
        raise ArgumentError, 'klass argument cannot be nil' if klass.nil?
        raise ArgumentError, "#{klass} is not a valid presenter class" unless klass <= Presenter::Base
        klass.new(self)
      end
    end

    module Enumerator
      extend ActiveSupport::Concern

      def with_presenter(klass, &block)
        raise ArgumentError, 'klass argument cannot be nil' if klass.nil?
        raise ArgumentError, "#{klass} is not a valid presenter class" unless klass <= Presenter::Base
        return to_enum(__method__, klass) unless block_given?

        ##
        # some magic!
        # assuming that this method is used within the view
        # the block which is supplied will be in the scope of the view
        # therefore, getting it's binding and evaluating self will
        # get us the view object
        ##
        view = block.binding.eval('self')
        is_a_view = view.is_a?(ActionView::Base)

        self.each do |item|
          yield klass.new(item, (is_a_view ? view : nil))
        end
      end
    end

    class Base < SimpleDelegator
      def item
        __getobj__
      end

      def initialize(item, view=nil)
        super(item)
        @view = view
      end

      def method_missing(method, *args, &block)
        return super if @view.nil?

        if respond_to_without_view?(method)
          super
        elsif @view.respond_to?(method)
          @view.send(method, *args, &block)
        else
          super
        end
      end

      def respond_to_with_view?(method)
        respond_to_without_view?(method) || (!@view.nil? && @view.respond_to?(method))
      end
      alias_method_chain :respond_to?, :view
    end
  end
end

class Object
  include Riveter::Presenter::Object
end

class Enumerator
  include Riveter::Presenter::Enumerator
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
riveter-0.8.3 lib/riveter/presenter.rb
riveter-0.8.2 lib/riveter/presenter.rb
riveter-0.8.1 lib/riveter/presenter.rb
riveter-0.8.0 lib/riveter/presenter.rb
riveter-0.7.0 lib/riveter/presenter.rb
riveter-0.6.0 lib/riveter/presenter.rb
riveter-0.3.0 lib/riveter/presenter.rb
riveter-0.2.1 lib/riveter/presenter.rb
riveter-0.2.0 lib/riveter/presenter.rb
riveter-0.1.1 lib/riveter/presenter.rb
riveter-0.0.15 lib/riveter/presenter.rb
riveter-0.0.14 lib/riveter/presenter.rb
riveter-0.0.13 lib/riveter/presenter.rb
riveter-0.0.12 lib/riveter/presenter.rb
riveter-0.0.11 lib/riveter/presenter.rb
riveter-0.0.10 lib/riveter/presenter.rb
riveter-0.0.9 lib/riveter/presenter.rb
riveter-0.0.8 lib/riveter/presenter.rb
riveter-0.0.7 lib/riveter/presenter.rb
riveter-0.0.6 lib/riveter/presenter.rb