Sha256: af1fd8ab8025e7ebb86bde9b561ef9cf599c20ffc02adc4c46104f029277f9ee

Contents?: true

Size: 1.77 KB

Versions: 181

Compression:

Stored size: 1.77 KB

Contents

require 'active_support/core_ext/string/output_safety'

module ActionView
  class OutputFlow #:nodoc:
    attr_reader :content

    def initialize
      @content = Hash.new { |h,k| h[k] = ActiveSupport::SafeBuffer.new }
    end

    # Called by _layout_for to read stored values.
    def get(key)
      @content[key]
    end

    # Called by each renderer object to set the layout contents.
    def set(key, value)
      @content[key] = value
    end

    # Called by content_for
    def append(key, value)
      @content[key] << value
    end

    # Called by provide
    def append!(key, value)
      @content[key] << value
    end
  end

  class StreamingFlow < OutputFlow #:nodoc:
    def initialize(view, fiber)
      @view    = view
      @parent  = nil
      @child   = view.output_buffer
      @content = view.view_flow.content
      @fiber   = fiber
      @root    = Fiber.current.object_id
    end

    # Try to get an stored content. If the content
    # is not available and we are inside the layout
    # fiber, we set that we are waiting for the given
    # key and yield.
    def get(key)
      return super if @content.key?(key)

      if inside_fiber?
        view = @view

        begin
          @waiting_for = key
          view.output_buffer, @parent = @child, view.output_buffer
          Fiber.yield
        ensure
          @waiting_for = nil
          view.output_buffer, @child = @parent, view.output_buffer
        end
      end

      super
    end

    # Appends the contents for the given key. This is called
    # by provides and resumes back to the fiber if it is
    # the key it is waiting for.
    def append!(key, value)
      super
      @fiber.resume if @waiting_for == key
    end

    private

    def inside_fiber?
      Fiber.current.object_id != @root
    end
  end
end

Version data entries

181 entries across 142 versions & 18 rubygems

Version Path
actionpack-3.2.14.rc1 lib/action_view/flows.rb
swipe-rails-0.0.5 vendor/bundle/gems/actionpack-3.2.13/lib/action_view/flows.rb
active_mailer-0.0.9 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_view/flows.rb
active_mailer-0.0.8 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_view/flows.rb
active_mailer-0.0.7 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_view/flows.rb
active_mailer-0.0.6 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_view/flows.rb
font-awesome-rails-3.1.1.2 vendor/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_view/flows.rb
font-awesome-rails-3.1.1.2 vendor/ruby/2.0.0/gems/actionpack-3.2.12/lib/action_view/flows.rb
font-awesome-rails-3.1.1.2 vendor/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_view/flows.rb
font-awesome-rails-3.1.1.1 vendor/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_view/flows.rb
font-awesome-rails-3.1.1.1 vendor/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_view/flows.rb
font-awesome-rails-3.1.1.1 vendor/ruby/2.0.0/gems/actionpack-3.2.12/lib/action_view/flows.rb
challah-1.0.0.beta3 vendor/bundle/gems/actionpack-3.2.13/lib/action_view/flows.rb
fc-webicons-0.0.4 vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_view/flows.rb
challah-1.0.0.beta2 vendor/bundle/gems/actionpack-3.2.13/lib/action_view/flows.rb
challah-1.0.0.beta vendor/bundle/gems/actionpack-3.2.13/lib/action_view/flows.rb
challah-1.0.0.beta vendor/bundle/gems/actionpack-3.2.11/lib/action_view/flows.rb
fc-webicons-0.0.3 vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_view/flows.rb
fc-webicons-0.0.2 vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_view/flows.rb
fc-webicons-0.0.1 vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_view/flows.rb