Sha256: 0f979e09e2f306bb54db83247efb9e41ae0bd3ed6edce0bd7e6e471dcdcee91e

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

module Fluent
  module Factory

    # Creates a definition context for actions. If an existing context
    # exists, that context will be re-used.
    #
    # @param definition [Class] the name of a definition class
    # @param visit [Boolean] true if the context needs to be called into view
    # @param block [Proc] logic to execute within the context of the definition
    # @return [Object] instance of the definition
    def on(definition, visit=false, &block)
      definition = get_object_for(definition) if definition.is_a? String
      
      return @active if @active.kind_of?(definition)
      @active = definition.new(@driver, visit)
      block.call @active if block
      
      @active
    end

    alias_method :on_page, :on
    alias_method :while_on, :on
    
    # Creates a definition context for actions. Unlike the on() factory,
    # on_new will always create a new context and will never re-use an
    # existing one.
    #
    # @param definition [Class] the name of a definition class
    # @param visit [Boolean] true if the context needs to be called into view
    # @param block [Proc] logic to execute within the context of the definition
    # @return [Object] instance of the definition
    def on_new(definition, visit=false, &block)
      definition = get_object_for(definition) if definition.is_a? String

      @active = definition.new(@driver, visit)
      block.call @active if block

      @active
    end
    
    # Creates a definition context for actions and establishes the
    # context for display.
    #
    # @param definition [Class] the name of a definition class
    # @param block [Proc] logic to execute within the context of the definition
    def on_view(definition, &block)
      on(definition, true, &block)
    end

    alias_method :on_visit, :on_view

    def get_object_for(definition)
      definition.split('::').inject(Object) do |obj, name|
        obj.const_get(name)
      end
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fluent-0.6.0 lib/fluent/factory.rb
fluent-0.5.0 lib/fluent/factory.rb